working out distances between two points using google maps api?

后端 未结 8 1330
北海茫月
北海茫月 2021-01-03 10:39

Is it possible to send two lat long points to google to calculate the distance between the two?

相关标签:
8条回答
  • 2021-01-03 11:22

    If you're looking to use the v3 google maps API, here is a function I use: Note: you must add &libraries=geometry to your script source

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>
    

    Now the function:

    //calculates distance between two points in km's
    function calcDistance(p1, p2){
      return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
    }
    
    0 讨论(0)
  • 2021-01-03 11:26

    there is a handy function

    http://code.google.com/apis/maps/documentation/reference.html#GLatLng.distanceFrom

    basically create 2 GlatLng objects then use the above method

    0 讨论(0)
提交回复
热议问题