【问题标题】:How to get the coordinates of the dot in Google Map?如何在谷歌地图中获取点的坐标?
【发布时间】:2016-09-14 08:59:27
【问题描述】:

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

Picture 1Picture 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 google-maps-api-3 maps


【解决方案1】:

像这样初始化地图。我使用了事件侦听器方向,这样每次更改都会自动保存其航点。

 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)


 }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      相关资源
      最近更新 更多