Google MAP API: how to draw the shortest path between two points?

I need to draw the shortest path on the map. I created KML in Google Earth and uploaded an int to Google Map. As you can see the attached images: the paths are very different: the Google Vap path is longer. How to draw an arc path on a google map?

Path in google earthSame path in google map

+6
source share
2 answers

In V3, it simply adds the geodesic: true parameter to your Polyline.

Demo http://jsfiddle.net/yV6xv/20/ Click on the map to determine the endpoints of the path

  var myLine = new google.maps.Polyline({ map: map, geodesic: true }); var path = []; google.maps.event.addListener(map, 'click', function(event) { new google.maps.Marker({map:map,position:event.latLng}); path.push(event.latLng); myLine.setPath(path); }); 

result from demo

+9
source

This example is for the V2 API, but the math for V3 is the same: http://maps.forum.nu/gm_flight_path.html

Marcelo.

0
source

Source: https://habr.com/ru/post/917672/


All Articles