3

The Google Ajax API playground (http://code.google.com/apis/ajax/playground/?exp=maps#map_markers) 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 = northEast.lat() - southWest.lat();
    for (var i = 0; i < 10; i++) {
      var point = new GLatLng(southWest.lat() + 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.

Hubert

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Hubert
  • 16,012
  • 18
  • 45
  • 51

3 Answers3

5

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.

Chris B
  • 15,524
  • 5
  • 33
  • 40
2

I don't know about the Google Earth API but you can achieve this another way. Download the Nasa blue marble bathymetry map and create a bitmap of sea areas. Then map these coords to long/lat and you have a way to test for seas and lakes that you can use in conjunction with the google maps api.

SpliFF
  • 38,186
  • 16
  • 91
  • 120
  • Thanks for your answer but I would be surprised if there was not an easier way than this ? a sort of "property" or function on LatLng than returns a boolean (YES) if on land ? I've searched for "land" "sea" etc in the Google reference (http://code.google.com/intl/fr-FR/apis/maps/documentation/reference.html) but can't find anything... still searching further. – Hubert Jun 13 '09 at 07:03
  • Perhaps there is but since the primary reason for writing the google maps API is to mark businesses on a map I'd have to say the concept of randomly tagging areas based on elevation may be outside the scope of the API's features. On that note there may be a way to query elevation based on a point and if so your answer lies there. As far as I know sea-level is generally (but not always) pretty close to the "mean sea level" (the average height of all oceans). If you establish a location is lower than "mean sea level" that may be good enough. – SpliFF Jun 13 '09 at 07:59
  • and by "good enough" I mean for your purpose. It is NOT a fact that all land below sea level is actually inundated by sea. – SpliFF Jun 13 '09 at 08:01
  • I see your point, txs. I will investigate further and return here if I find something interesting that I can share with the community. Cheers everyone. H. – Hubert Jun 13 '09 at 09:42
  • maybe I should try the following : http://www.geonames.org/export/web-services.html#countrycode If I dont get a country code its on water ... i'll give it a try ... ? – Hubert Jun 13 '09 at 09:51
  • Thanks for this answer! This was the perfect solution for my needs; I ended up writing a non-published Node.js module: https://github.com/mhulse/rand-land-coords ... It’s more of a proof of concept, and I think I could improve the logic to find black pixels. I like this solution as I’m not hitting Google’s API(s) to do the work. Additionally, one could provide a higher or lower resolution map image and/or a map image that is modified (with the antarctic removed, for example). Anyway, +1 and thanks for the idea/inspiration! – mhulse May 08 '19 at 16:54
0

I'm creating a C# program that takes the users input as where they want to go and then based on that, it points them to a random location that matches their specifications.

"http://maps.google.com/maps/geo?q=29,-55" is the webpage that i'll use to determine if it's on land or not.. take a look at the specific "Accuracy: 0" string of the results of a page. If it's accuracy 0 then it's in the ocean. If it's 1 or higher then it's on land.

josh o
  • 1