我正在开发一个Web应用程序,用户可以在屏幕上拖动目标标记,并自动更新持续时间和目标更新的输入字段。我还放置了原点和目标坐标的字段,我想在拖动屏幕标记时更新它们。我只是不知道从哪里获取这些值。这是我的代码:
<div id="map" style="width: 400px; height: 300px;"></div>
Duration: <input id="duration"></div>
Distance: <input id="distance"></input>
Origin Longitude <input id="origin_longitude"></input>
Origin Latitude <input id="origin_latitude"></input>
Destination Longitude <input id="destination_longitude"></input>
Destination Latitude <input id="destination_latitude"></input>
<div>
<script type="text/javascript">
function initialize() {
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer({
draggable: true
});
var myOptions = {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
directionsDisplay.setMap(map);
var request = {
origin: new google.maps.LatLng(51.403650, -1.323252),
destination: new google.maps.LatLng(51.403650, -1.323252),
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
directions = directionsDisplay.getDirections();
// Display the distance:
document.getElementById('distance').value =
directions.routes[0].legs[0].distance.value + " meters";
// Display the duration:
document.getElementById('duration').value =
directions.routes[0].legs[0].duration.value + " seconds";
})
} else {
alert("directions request failed:" + status)
}
});
}
google.maps.event.addDomListener(window, "load", initialize);
</script>
答案 0 :(得分:2)
查看this示例。
添加
marker.addListener('position_changed', printMarkerLocation);
然后你可以定义函数
function printMarkerLocation(){
console.log('Lat: ' + marker.position.lat() + ' Lng:' + marker.position.lng() );}
答案 1 :(得分:1)
在标记拖动事件期间获取标记的位置怎么样?这应该有用。
查看https://developers.google.com/maps/documentation/javascript/3.exp/reference#Marker
您可以收听许多活动。
答案 2 :(得分:1)
开始和结束位置位于方向响应对象中。根据当前请求,您只有一条腿,因此它们将是:
adjustPan
代码段
document.getElementById('origin_latitude').value = directions.routes[0].legs[0].start_location.lat();
document.getElementById('origin_longitude').value = directions.routes[0].legs[0].start_location.lng();
document.getElementById('destination_latitude').value = directions.routes[0].legs[0].end_location.lat();
document.getElementById('destination_longitude').value = directions.routes[0].legs[0].end_location.lng();
&#13;
function initialize() {
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer({
draggable: true
});
var myOptions = {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
directionsDisplay.setMap(map);
var request = {
origin: new google.maps.LatLng(51.403650, -1.323252),
destination: new google.maps.LatLng(51.403650, -1.323252),
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
directions = directionsDisplay.getDirections();
// Display the distance:
document.getElementById('distance').value =
directions.routes[0].legs[0].distance.value + " meters";
// Display the duration:
document.getElementById('duration').value =
directions.routes[0].legs[0].duration.value + " seconds";
document.getElementById('origin_latitude').value = directions.routes[0].legs[0].start_location.lat();
document.getElementById('origin_longitude').value = directions.routes[0].legs[0].start_location.lng();
document.getElementById('destination_latitude').value = directions.routes[0].legs[0].end_location.lat();
document.getElementById('destination_longitude').value = directions.routes[0].legs[0].end_location.lng();
})
} else {
alert("directions request failed:" + status)
}
});
}
google.maps.event.addDomListener(window, "load", initialize);
&#13;
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
&#13;