Google Maps API - 获取最接近邮政编码的点数

时间:2013-06-24 16:50:02

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

我正在使用Google Maps API v3和javascript将地图添加到网站。

我有一个地址列表,并已成功将它们绘制在地图上。当用户键入邮政编码时,地图会重新定位其位置,显示最接近该点的标记。现在,我需要创建一个最接近3或5个位置的列表视图,其中包含用于行车方向的链接。我被困了......并且愿意接受建议。

2 个答案:

答案 0 :(得分:11)

通常的解决方案是使用google.maps.geometry.spherical library computeDistanceBetween(from:LatLng,to:LatLng,radius?:number)方法将数字减少到大约10,然后使用distance matrix返回驾驶到那些位置的距离,因此结果可以通过行驶距离(实际行驶距离)进行分类,并通过请求限制内的实际行驶距离减少到最近的3到5个位置。

example (finds the 3 closest places from a list) (从FusionTables“披萨商店”示例中借来的数据)

  function codeAddress() {
    var address = document.getElementById('address').value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
    if (customerMarker) customerMarker.setMap(null);
        customerMarker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
    closest = findClosestN(results[0].geometry.location,10);
        // get driving distance
        closest = closest.splice(0,3);
        calculateDistances(results[0].geometry.location, closest,3);
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

function findClosestN(pt,numberOfResults) {
   var closest = [];
   document.getElementById('info').innerHTML += "processing "+gmarkers.length+"<br>";
   for (var i=0; i<gmarkers.length;i++) {
     gmarkers[i].distance = google.maps.geometry.spherical.computeDistanceBetween(pt,gmarkers[i].getPosition());
     document.getElementById('info').innerHTML += "process "+i+":"+gmarkers[i].getPosition().toUrlValue(6)+":"+gmarkers[i].distance.toFixed(2)+"<br>";
     gmarkers[i].setMap(null);
     closest.push(gmarkers[i]);
   }
   closest.sort(sortByDist);
   return closest;
}

function sortByDist(a,b) {
   return (a.distance- b.distance)
}

function calculateDistances(pt,closest,numberOfResults) {
  var service = new google.maps.DistanceMatrixService();
  var request =    {
      origins: [pt],
      destinations: [],
      travelMode: google.maps.TravelMode.DRIVING,
      unitSystem: google.maps.UnitSystem.METRIC,
      avoidHighways: false,
      avoidTolls: false
    };
  for (var i=0; i<closest.length; i++) request.destinations.push(closest[i].getPosition());
  service.getDistanceMatrix(request, function (response, status) {
    if (status != google.maps.DistanceMatrixStatus.OK) {
      alert('Error was: ' + status);
    } else {
      var origins = response.originAddresses;
      var destinations = response.destinationAddresses;
      var outputDiv = document.getElementById('side_bar');
      outputDiv.innerHTML = '';

      var results = response.rows[0].elements;
      for (var i = 0; i < numberOfResults; i++) {
        closest[i].setMap(map);
        outputDiv.innerHTML += "<a href='javascript:google.maps.event.trigger(closest["+i+"],\"click\");'>"+closest[i].title + '</a><br>' + closest[i].address+"<br>"
            + results[i].distance.text + ' appoximately '
            + results[i].duration.text + '<br><hr>';
      }
    }
  });
}

example above with "Get Directions" link in the infoWindow

答案 1 :(得分:1)

如果你有一对拉链代码,你可以使用半正公式来找到你所在位置的距离。