如何在Google Map中获取点的坐标?

时间:2016-09-14 08:59:28

标签: google-maps google-maps-api-3 maps

我目前在我的地图上工作,我可以动态添加路线并将其保存到我的mysql数据库中。我已经知道如何获得Marker 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);
        }
    });
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

像这样初始化地图..我使用了事件监听器方向,这样每次更改都可以自动保存其路点..

 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();

});

保存航路点..

  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)


 }