简体   繁体   中英

how to get latitude, longitude onclick of a map in google maps api v3 javascript?

how to know latitude, longitude on click of a map in google maps api v3. i have done this in google maps api v2 with this code

 GEvent.addListener(map, "click", function(overlay, latlng) {
          if (latlng) {
            marker = new GMarker(latlng, {draggable:true});
            GEvent.addListener(marker, "click", function() {
                //alert("hello");
              var html = "<table>" +
                         "<tr><td>Name:</td> <td><input type='text' id='name'/> </td> </tr>" +
                         "<tr><td>Time:</td> <td><input type='text' id='time'/> </td> </tr>" +
                         "<tr><td>Bus Id:</td> <td><input type='text' id='busId'/> </td> </tr>" +
                         "<tr><td>Device Id:</td> <td><input type='text' id='deviceId'/> </td> </tr>" +
                         "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";
              marker.openInfoWindow(html);
            });
            map.addOverlay(marker);
          }
        });

how to do this same thing in v3?

i tried this but it didn't work.

google.maps.event.addListener(map, "click", function(overlay,latlng) {
        if (latlng) {
        marker = new google.maps.Marker(latlng);
           google.maps.event.addListener(marker, "click", function() {
              var html = "<table>" +
                         "<tr><td>Name:</td> <td><input type='text' id='name'/> </td> </tr>" +
                         "<tr><td>Time:</td> <td><input type='text' id='time'/> </td> </tr>" +
                         "<tr><td>Bus Id:</td> <td><input type='text' id='busId'/> </td> </tr>" +
                         "<tr><td>Device Id:</td> <td><input type='text' id='deviceId'/> </td> </tr>" +
                         "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";
              marker.openInfoWindow(html);
            });
            map.setMap(marker);

         }
        });

You have to use event argument.

google.maps.event.addListener(map, 'click', function(event) {

    marker = new google.maps.Marker({position: event.latLng, map: map});

});

You have to use this.

 google.maps.event.addListener(map, 'click', function(event) {
    alert("Latitude: " + event.latLng.lat() + " " + ", longitude: " + event.latLng.lng());
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM