Reloading markers from MySQL database without refreshing map

1,899 views
Skip to first unread message

Alexander Antonov

unread,
Apr 7, 2011, 9:06:22 PM4/7/11
to google-map...@googlegroups.com
Hi,

I was wondering if you guys can give me some advice on how to proceed with this.

I have used these instructions here, to load my markers from a MySQL database:
http://code.google.com/apis/maps/articles/phpsqlajax_v3.html (no point linking to my code as it's the same as the one in the example)

What I would like to do is to update the markers dynamically whenever a new entry is added to the database in the 'markers' table, so whoever is viewing the map can see the new markers appear as soon as they are added to the database, without refreshing the entire map. Is that possible?

If not, another solution would be to reload the markers in a set interval of time (again without refreshing the map), but I'm not sure what the best way to do this would be either?

JKurtock

unread,
Apr 8, 2011, 7:16:32 PM4/8/11
to Google Maps JavaScript API v3
There may be two use cases. First, your user clicks on a link "Is
there anything new?" Without refreshing the map, you make an AJAX
call to your database, and then using the Maps API add (or subtract)
the relevant markers.

Second, your user just stares at the screen. Every 30 seconds, your
javascript times out and sends an AJAX request (as above).
(Refreshing the entire page is wasteful; you only need to update the
markers that have changed.)

The third case is your user stares at the screen, but it ONLY updates
when the database has changed. That's much more difficult (how is the
user's browser going to know that something is new?) And if it can be
done, I can't do it.

Clearly, the first case is the most efficient. The second case wastes
Google's servers and your user's computer.

- Jeff

On Apr 7, 6:06 pm, Alexander Antonov <cont...@a-antonov.com> wrote:
> Hi,
>
> I was wondering if you guys can give me some advice on how to proceed with
> this.
>
> I have used these instructions here, to load my markers from a MySQL
> database:http://code.google.com/apis/maps/articles/phpsqlajax_v3.html(no point

geoco...@gmail.com

unread,
Apr 8, 2011, 7:21:46 PM4/8/11
to Google Maps JavaScript API v3
On Apr 8, 4:16 pm, JKurtock <jkurt...@gmail.com> wrote:
> There may be two use cases.  First, your user clicks on a link "Is
> there anything new?"  Without refreshing the map, you make an AJAX
> call to your database, and then using the Maps API add (or subtract)
> the relevant markers.
>
> Second, your user just stares at the screen.  Every 30 seconds, your
> javascript times out and sends an AJAX request (as above).
> (Refreshing the entire page is wasteful; you only need to update the
> markers that have changed.)
>
> The third case is your user stares at the screen, but it ONLY updates
> when the database has changed.  That's much more difficult (how is the
> user's browser going to know that something is new?)  And if it can be
> done, I can't do it.
>
> Clearly, the first case is the most efficient.  The second case wastes
> Google's servers and your user's computer.

I don't see how the second case "wastes" any of Google's resources.
All the icons (unless new ones are required) are already in the cache,
the only traffic would be the periodic AJAX request to the OP's host,
that would use his bandwidth and resources not google's.

Perhaps you could enlighten us as to what you were thinking would use
Google's servers? (unless you are refreshing the whole page/map,
avoiding which would be the point of AJAX).

-- Larry

Nikolaj Pedersen

unread,
Apr 9, 2011, 3:05:10 AM4/9/11
to Google Maps JavaScript API v3
You could be stressing the Google Servers if using KML layers and
such.
But for simple marker retrieveval, it is recommended as mentioned.

There are many ways to optimize it.

It could be smart to avoid too much database query for getting the
data, as each request will likely contain nothing new.
So - when updating a marker it could be possible to renew a timestamp
value somewhere in the database.
This is then checked first by the server script, and returned if
nothing new. If something is changed, then the more expensive query is
done.

It is also possible to generate a server file with a new timestamp
name (for example), and the clients could read that instead to
determine if a new data call should be made.
This is helpful if many are querying for changes at the same time, as
the file is easily cached.
But it is not smart if many markers are changed alot (which seem
unlikely).

.nikolaj

On Apr 9, 1:21 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:

JKurtock

unread,
Apr 9, 2011, 8:07:12 AM4/9/11
to Google Maps JavaScript API v3
Larry is correct, if the server-side application used logic as
described by Nikolaj then the return from the AJAX call would say
"there is nothing new" and the browser side application would not make
any inquiry to Google. (Assuming that the application is written
sensibly, which is the whole point of coming to this forum.) As
Nikolaj points out, the server-side logic needs to preserve the
"state" of the map so that it can respond correctly to each user with
only what is new for that user. A timestamp would be good.
Reply all
Reply to author
Forward
0 new messages