create two markers with drag drop in google maps api

Asked

Viewed 103 times

0

Fala galera,

I’m creating a map where the idea is that the person can insert a pin in it and then in the sequence insert another pin, when he inserts the second pin, a line between the two pins would appear stating the distance between them. I would save the lat/long of the two pins in a mysql to display several 'two-point paths' on the map. Does anyone have any idea how I could do that ?

inserir a descrição da imagem aqui

1 answer

0

Creating a map and adding 2 "draggable 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);

        }
    }
  • but how would I add the first pin, and then the second ? So he adds the two at once....

  • Separate into a function and call her when you need her!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.