谷歌地图,获得目的地

时间:2015-06-02 07:38:09

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

刚刚开始使用谷歌地图api并陷入困境我如何获得起源和目的地的位置?

我想在管理面板中使用它们来更改位置并保存。

我找到了如何用标记制作,但需要与TravelMode相同。

标记脚本:

    ...
    var point = marker.getPosition();
    map.panTo(point);

    // Update text fields with lat and lng
    document.getElementById("latitude").value = point.lat();
    document.getElementById("longitude").value = point.lng();

目前我的剧本:

<script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {

 directionsDisplay = new google.maps.DirectionsRenderer({
          //suppressMarkers: true,
          //suppressInfoWindows: true,
          draggable: true,
          polylineOptions: {
                strokeColor: '#FF0000',
                strokeOpacity: 0.7,
                    strokeWeight: 5
                },
     });


var request = {
  origin:     new google.maps.LatLng(<?php echo $start_ps["lat"].', '.$start_ps["lon"]; ?>),
  <?php foreach ($waypoints as $wp) {
        $loc = ToLL( $wp["A"], $wp["B"], '35');
        echo 'new google.maps.LatLng('.$loc["lat"].', '.$loc["lon"].'),'; echo "\n";
} ?>

  destination:new google.maps.LatLng(<?php echo $end_ps["lat"].', '.$end_ps["lon"]; ?>),      
  travelMode: google.maps.TravelMode.DRIVING
};



directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
  directionsDisplay.setDirections(response);
}
});



var vilnius = new google.maps.LatLng(<?php echo $start_ps["lat"].', '.$start_ps["lon"]; ?>);

var mapOptions = {
  zoom:7,
  center: vilnius
};

map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
 }

google.maps.event.addDomListener(window, 'load', initialize);

</script>

1 个答案:

答案 0 :(得分:0)

找到解决方案,然后改变方向即可使用:

开始:

directionsDisplay.directions.routes [0] .legs [0] .start_location.lat() directionsDisplay.directions.routes [0] .legs [0] .start_location.lng()

<强>结束:

directionsDisplay.directions.routes [0] .legs [0] .end_location.lat() directionsDisplay.directions.routes [0] .legs [0] .end_location.lng()

测试提醒:显示已更改的起始位置

  google.maps.event.addListener(directionsDisplay, 'directions_changed', function (event) {

       alert("lat: " + directionsDisplay.directions.routes[0].legs[0].start_location.lat() + " \n lng: " + directionsDisplay.directions.routes[0].legs[0].start_location.lng());

});