用于获取两个位置之间的距离和时间的功能

时间:2013-09-21 15:49:34

标签: javascript jquery function google-maps return

下面我有一个代码,当用户点击标记时,会显示google放置API数据。

我还有一个带坐标的两个点位置(lat,lng),所以Point1[], Point2[]

如何计算这些地方之间的公路距离和时间,并以HTML格式显示。 谷歌地图有一个能够计算这个距离和时间的函数吗?

CODE:

google.maps.event.addListener(marker,'click',function(){

        service.getDetails(request, function(place, status) {
          if (status == google.maps.places.PlacesServiceStatus.OK) {
            //class m_tooltip
            var contentStr = '<div><h5>'+place.name+'</h5><p>'+place.formatted_address+'</p>';
            if (!!place.photos) contentStr += '<img src='+place.photos[0].getUrl({ 'maxWidth': 500, 'maxHeight': 500 })+'></img>';
            if (!!place.formatted_phone_number) contentStr += '<br>'+place.formatted_phone_number;
            if (!!place.website) contentStr += '<br><a target="_blank" href="'+place.website+'">'+place.website+'</a>';

            var Point1 = [34,666, 56,888];
            var Point2 = [77.876, 45,555];
            contentStr += '<p>Distance in km:' + functionThatReturnRoadDistance(Point1, Point2) + '</p>';


            $(contentStr).dialog({
              modal:true
          });

          } else { 
            var contentStr = "<h5>No Result, status="+status+"</h5>";

           $(contentStr).dialog();
          }  
        });

那么是否有一些谷歌地图或类似的功能会让我返回米或公里的距离而不是地点之间的简单距离,而是ROAD / ROUTE距离???

更新:我认为:

function calculateDistances(start,stop) {
  var service = new google.maps.DistanceMatrixService();
  service.getDistanceMatrix(
    {
      origins: [start],
      destinations: [stop],
      travelMode: google.maps.TravelMode.DRIVING,
      unitSystem: google.maps.UnitSystem.METRIC,
      avoidHighways: false,
      avoidTolls: false
    }, callback);
}
  function callback(response, status) {
  if (status != google.maps.DistanceMatrixStatus.OK) {
    alert('Error was: ' + status);
  } else {
    var origins = response.originAddresses;
    var destinations = response.destinationAddresses;


    for (var i = 0; i < origins.length; i++) {
      var results = response.rows[i].elements;
      for (var j = 0; j < results.length; j++) {
       var xxx= "";
        xxx += origins[i] + ' to ' + destinations[j]
            + ': ' + results[j].distance.text + ' in '
            + results[j].duration.text + '<br>';
     return xxx;
      }
    }
  }
}

并用以下方式调用:

  var start = document.getElementById("from").value;
        var stop = place.formatted_address;
        contentStr += '<p>'+ calculateDistances(start,stop); + '</p>';

        $(contentStr).dialog({
          modal:true
      });

但DONT适用于DistanceMatrix

2 个答案:

答案 0 :(得分:0)

根据http://www.movable-type.co.uk/scripts/latlong.html

if (typeof(Number.prototype.toRad) === 'undefined')
{
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

function getDistance(lat1, lon1, lat2, lon2) {
    var R = 6371; // km
    var dLat = (lat2-lat1).toRad();
    var dLon = (lon2-lon1).toRad();
    var lat1 = lat1.toRad();
    var lat2 = lat2.toRad();

    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c;
    return d;
}

此处的集成示例:http://jsfiddle.net/dFqmG/(以km为单位)

答案 1 :(得分:0)

这可以帮到你 Distance Matrix