Google-maps – HOW TO add random markers to a map BUT avoiding the sea


The Google Ajax API playground ( provides a nice exemple to add random markers to any map:

function initialize() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 9);

    // Add 10 markers to the map at random locations
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = -;
    for (var i = 0; i < 10; i++) {
      var point = new GLatLng( + latSpan * Math.random(),
                              southWest.lng() + lngSpan * Math.random());
      map.addOverlay(new GMarker(point));

BUT if your LatLng are next to the sea (or if you use another Zoomlevel than the exemple provided), How do you make sure your marker will avoid being placed on the sea ? Is there a flag that I can use to test if the potential LatLng of the random point is in the sea or on land ???

Txs in advance.


Best Solution

One possibility would be to send a reverse geocode request for every point you generate. Points over water would return a "G_GEO_UNKNOWN_ADDRESS" (602) status code. However, you'll need to test it thoroughly to make sure that it will work. It's possible that a few locations on land might be returned as unknown.