如何获取Google地图中点的坐标?

How to get the coordinates of the dot in Google Map?

我目前正在制作我的地图,我可以在其中动态添加路线并将其保存到我的 mysql 数据库中。我已经知道如何获取标记 A 和 B 的坐标,但我确实知道如何获取白点的坐标。如何得到它的坐标?或者可以得到它的坐标?

Picture 1 Picture 2

我用过这种代码..

function initMap() {
    var lat_lng = {
        lat: 22.08672,
        lng: 79.42444
    };
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 6,
        center: lat_lng
    });

    var directionsService = new google.maps.DirectionsService;
    var directionsDisplay = new google.maps.DirectionsRenderer({
        draggable: true,
        map: map,
        panel: document.getElementById('right-panel')
    });
    directionsDisplay.addListener('directions_changed', function() {
        computeTotalDistance(directionsDisplay.getDirections());
    });

    displayRoute('New Delhi, IN', 'Indore, IN', directionsService,
        directionsDisplay);
}

function displayRoute(origin, destination, service, display) {
    service.route({
        origin: origin,
        destination: destination,
        waypoints: [{
            location: 'New Delhi, IN'
        }, {
            location: 'Indore, IN'
        }],
        travelMode: google.maps.TravelMode.DRIVING,
        avoidTolls: true
    }, function(response, status) {
        if (status === google.maps.DirectionsStatus.OK) {
            display.setDirections(response);
        } else {
            alert('Could not display directions due to: ' + status);
        }
    });
}

您可以尝试以下事件侦听器:

google.maps.event.addListener(marker, 'dragend', function (event) {
    document.getElementById("latbox").value = this.getPosition().lat();
    document.getElementById("lngbox").value = this.getPosition().lng();
});

像这样初始化地图。我使用了事件侦听器方向,这样对于您所做的每一次更改,它都会自动保存其 waypoints..

 var map, ren, ser, waypoints;
  function initmap()
{
   map = new google.maps.Map( document.getElementById('map'),
   {'zoom':10,'mapTypeId': google.maps.MapTypeId.ROADMAP, 'center': new 
    google.maps.LatLng(9.848070, 124.218979) })

   ren = new google.maps.DirectionsRenderer( {'draggable':true} );
   ren.setMap(map);
   ser = new google.maps.DirectionsService();


   ren.addListener('directions_changed', function() {
//if you drag the dots it automatically chnage the way ponts
 computeTotalDistance(ren.getDirections());
save_waypoints();

});

正在保存 waypoints..

  function save_waypoints()
 {
var w=[],wp;
var rleg = ren.directions.routes[0].legs[0];
data.start = {'lat': rleg.start_location.lat(), 'lng':rleg.start_location.lng()}
data.end = {'lat': rleg.end_location.lat(), 'lng':rleg.end_location.lng()}
var wp = rleg.via_waypoints
for(var i=0;i<wp.length;i++)w[i] = [wp[i].lat(),wp[i].lng()]
data.waypoints = w;

waypoints = JSON.stringify(data)


 }