How to get total driving distance with Google Maps API V3?

前端 未结 5 956
北海茫月
北海茫月 2020-12-05 04:47

I used to be able to get it like this:

directionsService.route(directionsRequest, function(directionsResult, directionsStatus) {
    var directionsRenderer =         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 05:11

    Mark's answer is in meters for totalDistance and seconds for totalDuration.

    If you're in the U.S. and want miles with a single decimal point, multiply like so:

    var METERS_TO_MILES = 0.000621371192;
    $('#distance').text((Math.round( totalDistance * METERS_TO_MILES * 10 ) / 10)+' miles');
    

    And if you want minutes:

    $('#distance').text(Math.round( totalDuration / 60 )+' minutes');
    

提交回复
热议问题