【发布时间】:2016-06-10 08:54:55
【问题描述】:
我有一个显示数据库中的邮政编码的应用程序,当单击按钮时,邮政编码被地理编码并在弹出窗口中显示为谷歌地图上的标记。我试图在地图上显示两个标记之间的行车路线。我已将地理编码值保存到 HTML 跨度标签中,并且我正在尝试将这些值用作路线的起点和终点。除了标记之间显示的路线显示错误消息“由于 NOT_FOUND 导致路线请求失败”之外,一切正常。
知道如何显示路线吗?
$(document).ready(function () {
$(".openMap").click(function () {
var directionsService = new google.maps.DirectionsService;
var mapLocation = $(this).prev().prev().prev().text();
var secondMarker = $(this).prev().prev().text();
window.markers = [];
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: mapLocation }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var mapOptions = { center: results[0].geometry.location, zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP };
map = new google.maps.Map(document.getElementById('map'), mapOptions);
var directionsDisplay = new google.maps.DirectionsRenderer({map: map});
directionsDisplay.setMap(map);
var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location, title: "Departure Location" });
markers.push(marker);
$('#route1').text(results[0].geometry.location);
}
});
var geocoder2 = new google.maps.Geocoder();
geocoder.geocode({ address: secondMarker }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker2 = new google.maps.Marker({ map: map, position: results[0].geometry.location, title: "Destination Location" });
markers.push(marker2);
$('#route2').text(results[0].geometry.location);
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
bounds.extend(markers[i].getPosition());
}
map.fitBounds(bounds);
}
});
directionsService.route({
origin: $('#route1').text(),
destination: $('#route2').text(),
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
$("#map").dialog({ title: "Lift Location ", height: 500, width: 500, modal: true });
});
});
【问题讨论】:
-
我收到您的代码错误:
Uncaught ReferenceError: directionsDisplay is not defined -
你的 CSS/HTML 是什么样的?请提供一个 Minimal, Complete, Tested and Readable example 来证明您的问题。
标签: javascript jquery html google-maps