0

i have a function that i can give coordinate into input by click on map.

i want to can get two coordinate of two point on map for get direction between them by two click on map.

how can i do this?

 var pointa;
        var pointb;
     google.maps.event.addListener(map,'click',function(event){
          var point=new google.maps.LatLng(event.latLng.lat(),event.latLng.lng());
          document.path.lat1.value=event.latLng.lat();
          document.path.lon1.value=event.latLng.lng();

            pointa=new google.maps.Marker({
                map: map,
                position: point,
                icon:'http://google.com/mapfiles/kml/paddle/A.png ',
                draggable:true
            }); 
     });   
  • possible duplicate of [get coordinate of two point by click on map](http://stackoverflow.com/questions/19327605/get-coordinate-of-two-point-by-click-on-map) – geocodezip Oct 12 '13 at 17:24
  • possible duplicate of [Array to create multiple routes on Google Maps v3](http://stackoverflow.com/questions/11778201/array-to-create-multiple-routes-on-google-maps-v3) – geocodezip Oct 12 '13 at 17:26

1 Answers1

0

You're already creating a marker when they click. You can readily tell when they click a second time by looking at pointa. pointa will have the value undefined to start with. That value is "falsey," but when you store a Google Maps Marker reference in pointa it's no longer falsey. So you can use "if (!pointa)" to know that you're dealing with the first click:

var pointa;
var pointb;
google.maps.event.addListener(map, 'click', function (event) {
    var point = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
    document.path.lat1.value = event.latLng.lat();
    document.path.lon1.value = event.latLng.lng();

    if (!pointa) {
        // This is the first click
        pointa = new google.maps.Marker({
            map: map,
            position: point,
            icon: 'http://google.com/mapfiles/kml/paddle/A.png ',
            draggable: true
         });
    }
    else {
        // This is a subsequent click (the second, third, etc.)
    }
});
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875