我正在使用Google Maps API V3可拖动标记,并在FROM和TO字段中使用自动建议。单击显示路线后,我得到带有2个标记A和B的地图。如果我更改标记A或B,则显示导航中的更新位置(directionsPanel)。我需要FROM NEW MARKER ADDRESS中的这些位置:和新标记地址:拖动标记A和B时的输入字段.A表示FROM,B表示TO位置。我在谷歌搜索并阅读API详细信息,但没有找到解决方案。我需要在数据库中插入这些更新的标记位置(地址)以供将来参考。提前致谢。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Google Map Finalized</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
#directionsPanel{
overflow-y:auto;
width:40%;
height:200px;
}
input[type="text"] {
width: 350px;
height:30px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var rendererOptions = {
draggable: true
};
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);;
var directionsService = new google.maps.DirectionsService();
var map;
var start_pos = new google.maps.LatLng(33.7167,73.0667);
function initialize() {
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(69.386,29.967),
new google.maps.LatLng(69.3451155,30.3753205)
);
var mapOptions = {
bounds:defaultBounds,
zoom:17,
center: start_pos
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var from = (document.getElementById('from'));
var to = (document.getElementById('to'));
var autocomplete = new google.maps.places.Autocomplete(from);
var autocomplete = new google.maps.places.Autocomplete(to);
autocomplete.bindTo('defaultBounds', map);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directionsPanel'));
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
computeTotalDistance(directionsDisplay.getDirections());
});
calcRoute();
codeAddress();
}
function calcRoute() {
var request = {
origin: document.getElementById('from').value,
destination: document.getElementById('to').value,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
for (var i = 0; i < myroute.legs.length; i++) {
total += myroute.legs[i].distance.value;
}
total = total / 1000.0;
document.getElementById('total').innerHTML = total + ' km';
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<script>
$(document).ready(function(){
$('button').on('click',initialize);
});
</script>
</head>
<body>
<div id="map-canvas" style="float:left;width:60%; height:100%"></div>
<div id="from_to">
FROM: <input id="from" class="controls" type="text" placeholder="Enter a location">
<br/><br/><hr/>
TO: <input id="to" class="controls" type="text" placeholder="Enter a location">
<br/><hr/>
<button id="btn_refresh">Show route</button>
<p>Total Distance: <span id="total"></span></p>
</div>
NAVIGATION:
<div id="directionsPanel">
</div>
FROM NEW MARKER ADDRESS: <input type="text" id="addressmarker1" size="30">
<br/><br/><hr/>
TO NEW MARKER ADDRESS: <input type="text" id="addressmarker2" size="30">
</body>
</html>
答案 0 :(得分:3)
试试这个:
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
var directionsList = directionsDisplay.getDirections();
computeTotalDistance(directionsList);
document.getElementById("from").value = directionsList.routes[0].legs[0]["start_address"];
document.getElementById("to").value = directionsList.routes[0].legs[0]["end_address"];
});
您还可以访问其他值:
//DISTANCE
document.getElementById("distance").value = directionsList.routes[0].legs[0]["distance"]["text"];
//DURATION
document.getElementById("duration").value = directionsList.routes[0].legs[0]["duration"]["text"];
//LATLNG POINT A
document.getElementById("fromLatLng").value = directionsList.routes[0].legs[0]["start_location"]["k"] + "," + directionsList.routes[0].legs[0]["start_location"]["B"] ;
//LATLNG POINT B
document.getElementById("toLatLng").value = directionsList.routes[0].legs[0]["end_location"]["k"] + "," + directionsList.routes[0].legs[0]["end_location"]["B"];
如果有帮助,请将其标记为已解决