【问题标题】:How to get places on google map route如何在谷歌地图路线上获取地点
【发布时间】:2015-10-19 14:39:08
【问题描述】:

如何在谷歌地图上绘制从源到目的地的路线,仅在不在附近的路线上获取所有地方,如娱乐、食品、加油站等。

【问题讨论】:

标签: google-maps google-maps-api-3


【解决方案1】:

要以@geocodezip 提供的link 为基础,解决查询限制,最好根据步骤制作边界框,而不是使用 Google 地图实用程序,然后按实际距离过滤位置步骤中的一个节点。这可以使用以下代码完成:

initialize(),输入:

var placeserv = null;
function initialize(){
  //setup map
  placeserv = new google.maps.PlacesService(map); //use your map variable
}

在路线服务的回调函数中(使用参数resultsstatus),您可以包含如下内容:

var route = results[0];
var steps = route.legs[0].steps;
for(var i=0; i<steps.length; i++){
  var lats = steps[i].path.map(function(a){return a.lat()});
  var lngs = steps[i].path.map(function(a){return a.lng()});
  var box = new google.maps.LatLngBounds(
    new google.maps.LatLng(Math.max.apply(null, lats), Math.max.apply(null, lngs)),
    new google.maps.LatLng(Math.min.apply(null, lats), Math.min.apply(null, lngs))
  );
  placeserv.radarSearch(yourRequest_seeDocumentation_or_geocodezipsCode,
    function(r,s){callback(r,s,steps[i].path)}
  );
  //depending on the number of queries you need to make, you may to add in
  //some setTimeouts
}

然后为您的调用添加一个回调函数,检查路由是否在指定的度数内。如果是这样,它将对该位置做一些事情。 (顺便说一下,这需要谷歌地图的几何库。请查一下。)

var minimumDist = 300 //within 300 meters of a point on the route
function callback(results, status, stepPts){
  if(results == 'OK'){
    for(var j=0; j<results.length; j++){
      for(var k=0; k<stepPts.length; k++){
        var dist = google.maps.geometry.spherical.computeDistanceBetween(stepPts[k], results[j].geometry.location);
        if(dist < minimumDist){
          //do something with the location
        }
      }
    }
  }
}

再一次,geocodezip 在他给你的链接中有一个非常完整和优秀的帖子。这只是我用来减少 Places Service 调用的实现。

【讨论】:

    猜你喜欢
    • 2015-01-24
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多