create two markers with drag drop in google maps api

0

Talk to people,

I am creating a map where the idea is to be able to insert a pin in it and then in the sequence insert another pin, when it inserts the second pin, a line between the two pins would appear informing the distance between them. It would save the lat / long of the two pins in a mysql to be able to display several 'colon paths' on the map. Anyone have any idea how I could do this?

    
asked by anonymous 10.03.2017 / 14:22

1 answer

0

Creating a map and adding 2 "drag" markers:

var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
  zoom: 4,
  center: myLatlng
}
var map = new google.maps.Map(document.getElementById("map"), mapOptions);

// Place a draggable marker on the map
var marker1 = new google.maps.Marker({
    position: myLatlng1,
    map: map,
    draggable:true,
    title:"Marcador 1"
    });

  var marker2 = new google.maps.Marker({
        position: myLatlng2,
        map: map,
        draggable:true,
        title:"Marcador 2"
    });

Calculate distance:

function CalcularDistancia(latlng1, latlng2) {
        var service = new google.maps.DistanceMatrixService();
        service.getDistanceMatrix(
        {
            origins: [latlng1],
            destinations: [latlng2],
            travelMode: google.maps.TravelMode.DRIVING // mude de acordo com seu proposito
        }, callback);
    }
function callback(response, status) {
        if (status == google.maps.DistanceMatrixStatus.OK) {
            alert("Distância:" + response.rows[0].elements[0].distance.text);

        }
    }
    
10.03.2017 / 16:58