views:

49

answers:

2

When a user moves the Google map, I would like to display items in the viewing range of the user automatically. How can I efficiently and quickly display the items?

I have basic understanding of calling getBounds() method every time the user moves the map, but I am not sure how I can efficiently search and get from my database the items within the lat/lng of the bounds of the current viewport. Is there easier and faster way of doing this?

+1  A: 

I've had the same issue. The way I tried to mitigate the problem is this.

  • store the long/lat in the database as decimals
  • put indexes on the long and lat columns
  • use a query like SELECT * FROM table WHERE long > $long_left AND long < $long_right AND lat > $lat_bottom AND lat < $lat_top

I suggest you also use MarkerManager to handle the markers if you are expecting a large number of markers as you can set the markers to only display at certain zoom levels.

storing the long/lat as decimals lets the database compare the values correctly instead of having to convert the values before comparing them

indexes are key with databases (ha pun :-)) use them correctly but don't overuse them

Geek Num 88
+1  A: 

In addition to Geek's answer, you may also want to consider creating an index on a Geohash instead. You could also use a database with spatial indexing capabilities. MySQL, PostgreSQL and SQL Server 2008 all have geo-spatial features (either natively or via extensions), which include spatial indexing.

Daniel Vassallo
Spatial indexing capabilities seems interesting, any idea how much the performance is improved by using this type of indexing?
Cory
@Cory: It depends on how many items you'll have on the map. If you have just a hundred or so points, than I don't think it will matter much. You could still use an index on the latitude and longitude fields as Geek suggested in the other answer to, reduce the lookup time and avoid a full table scan. If on the other hand you will be having many thousands or millions of points, or if you will also be displaying trails, lines and polygons, then spatial indexes could become handy.
Daniel Vassallo