Those who have worked with Google Maps have faced the problem of plotting markers on the map for places which are close to the International Date line (IDL from now on). This problem is especially visible when the map is displayed with the IDL near the middle of the map and you have markers on both sides of the IDL. This is because Google interprets a GLatLng() position differently according to which side of the IDL a marker is.
Assume the following example where according to the boundaries of the visible map, you need to retrieve markers from a database and show them on the map. The bounds of a map are obtained by making calls to getSouthWest() and getNorthEast() . From the results obtained from these two calls it holds that the north latitude value is more than the south latitude and the west longitude is lesser than the east longitude. So a valid boundary could be : north:40.00, south:-20.50, west:-120.678, east:15.923 . With these values, you would use code to retrieve markers from your database which fall within these boundaries.
Things work fine as long as the west longitude is less than the east longitude. The trouble starts when your map boundary traverses the IDL. The westernmost longitude value is -179.99999 . If you go further west then it becomes 0.000 since Google Maps automatically wraps the world coordinates around the IDL. So now you have a boundary whose values are: north:40.00, south:-20.50, west:12.678, east:-150.923 . Now the assumption that “west longitude will be less than the east longitude” is not true anymore. Your marker retrieval code breaks because it wont get any marker whose longitude is > 12.678 and yet be < -150.923 .
How to get around this irritation. A knee jerk reaction would be to check for cases where west < east and then reverse the retrieval code to check for markers where longitude is less than east and greater than west (longitude is < 12.678 and > -150.923). This would certainly fetch the correct markers but it wouldnt display the marker on the map.
Why is that? Because then you are trying to plot the marker on the other side of the world and not the visible side. See figure below to understand the difference between the visible side of the map and invisible side of the map. Visualise a globe in front of you. At any given time only one half of the globe is visible to you; the other half is hidden from view. This is the difference between the visible and the invisible side of a google map.
Very frustrating. I tried a lot of tricks to get this thing to work until I finally hit upon a simple solution: Plot the markers in two passes instead of the usual one pass.
The javascript pseudocode goes something like this :
var lat1, lat2, lng1, lng2; var bDateLineHandle; getBounds(); getData(); function getBounds() { var bounds = map.getBounds(); var swBounds = bounds.getSouthWest(); var neBounds = bounds.getNorthEast(); lat1 = swBounds.lat(); lat2 = neBounds.lat(); lng1 = swBounds.lng(); lng2 = neBounds.lng(); if (lng1 >= 0 && lng2 <0) { bDateLineHandle = true; } else bDateLineHandle = false; } function getData() { var saveLng1 = lng1; var saveLng2 = lng2; getBounds(); map.setCenter(map.getCenter(), map.getZoom()); var pars = ''; if (bDateLineHandle) { lng1 = -179.999; } pars = "lat1=" + lat1 + "&lng1=" + lng1 + "&lat2=" + lat2 + "&lng2=" + lng2 + "&pg=" + p; /*** call data retrieval code here****/ if (bDateLineHandle) { lng1 = saveLng1; lng2 = 180.00; pars = "lat1=" + lat1 + "&lng1=" + lng1 + "&lat2=" + lat2 + "&lng2=" + lng2 + "&pg=" + p; /*** call data retrieval code here for a second time****/ } }
In the above code, getBounds() is called before calling getData(). The bDateLineHandle variable determines whether the map is traversing the IDL or not. For non IDL boundaries, the default map boundaries are passed as parameters to the data retrieval code (not part of this pseudocode). If IDL is detected then the bDateLineHandle var is set to true.
Then the map is broken into two parts: 1.left boundary to the IDL and 2.IDL to the right boundary. (See figure below)
Thus the first case retrieves all markers to the left of the IDL and the second call retrieves the markers to the right of the IDL. Then its just a matter of displaying both set of markers on the map.
Just wanted say thank you (very much). This was exactly what I needed to read in order to find a solution. I ended up handling it through mysql, but the logic was the same.
Kudos.
You are welcome.
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru
cool site u have ghere