本文介绍了如何将随机标记添加到地图,但避免海?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Ajax API操场()为向任何地图添加随机标记提供了一个很好的例子:

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

//在地图上随机添加10个标记
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 var point = new GLatLng(southWest.lat()+ latSpan * Math.random(),
southWest.lng ()+ lngSpan * Math.random());
map.addOverlay(new GMarker(point));
}
}
}

但是如果您的LatLng是下一个(或者如果您使用另一个Zoomlevel而不是提供的示例),您如何确保您的标记避免被放置在海中?有没有一个标志可以用来测试随机点的潜在LatLng是在海里还是在陆地?



提前发送。

Hubert

解决方案

一种可能性是发送为您生成的每个点。水上的点将返回一个G_GEO_UNKNOWN_ADDRESS(602)状态码。但是,您需要彻底测试以确保它能正常工作。可能有一些地方的土地可能会以未知的方式返还。

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

解决方案

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.

这篇关于如何将随机标记添加到地图,但避免海?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:09