Javascript 谷歌地图Api-将Google.Maps.Place传递到方向服务
根据GoogleDocumnetation,可以将位置的GooglePlaceID传递给Direciton服务。然而,无论我尝试什么组合,我绝对不能让它工作;我收到一个未找到的错误。我试着将id编码为测试,但没有成功 基本初始化代码:Javascript 谷歌地图Api-将Google.Maps.Place传递到方向服务,javascript,google-maps,Javascript,Google Maps,根据GoogleDocumnetation,可以将位置的GooglePlaceID传递给Direciton服务。然而,无论我尝试什么组合,我绝对不能让它工作;我收到一个未找到的错误。我试着将id编码为测试,但没有成功 基本初始化代码: var directionsDisplay; var directionsService = new google.maps.DirectionsService(); var hotelMap; var placesService; functi
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var hotelMap;
var placesService;
function initialize() {
var mapOptions = {
center: { lat: 37.30138, lng: -89.57778},
zoom: 15,
};
hotelMap = new google.maps.Map(document.getElementById("googlemaps"), mapOptions);
var marker = new google.maps.Marker({
position:{ lat: 37.30138, lng: -89.57778},
map: hotelMap,
});
var info = new google.maps.InfoWindow({
content: "3265 William Street, Cape Girardeau, MO 63701"
});
marker.setMap(hotelMap);
info.open(hotelMap, marker);
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(hotelMap);
directionsDisplay.setPanel(document.getElementById("directionModalBody"));
document.getElementById("searchButton").addEventListener("click", function() {
var keyword = document.getElementById("searchBox").value;
var requestOptions = {
location: { lat: 37.3011339, lng: -89.5770238},
radius: '5000',
keyword: keyword
};
placesService = new google.maps.places.PlacesService(hotelMap);
placesService.nearbySearch(requestOptions, findCallback);
});
}; // end initiallize
window.onload函数:
window.onload = function() {
initialize();
document.getElementById("calcDirections").onclick = function() {
if ($("#city").val() != null && $("#city").val() != "") {
findRoute();
} else {
alert("Please Enter a City");
}
}; // end onclick
$(".areaList").on("click", "a", function(e) {
e.preventDefault();
var placeID = $(this).attr("href");
locationRoute(placeID);
}) // end onclick
};
function locationRoute(locationID) {
var start = "ChIJfbJ8AyaId4gR4XCrciru2Qc";
var end = new google.maps.Place(locationID);
alert(locationID);
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
}; // end request object
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
document.getElementById("getDirectionButton").click();
} else {
alert(status);
}// end if
}); // end route
} // end findRoute
问题函数:
window.onload = function() {
initialize();
document.getElementById("calcDirections").onclick = function() {
if ($("#city").val() != null && $("#city").val() != "") {
findRoute();
} else {
alert("Please Enter a City");
}
}; // end onclick
$(".areaList").on("click", "a", function(e) {
e.preventDefault();
var placeID = $(this).attr("href");
locationRoute(placeID);
}) // end onclick
};
function locationRoute(locationID) {
var start = "ChIJfbJ8AyaId4gR4XCrciru2Qc";
var end = new google.maps.Place(locationID);
alert(locationID);
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
}; // end request object
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
document.getElementById("getDirectionButton").click();
} else {
alert(status);
}// end if
}); // end route
} // end findRoute
我尝试将位置ID作为字符串传递,但没有成功。我尝试过在它们前面加前缀,但同样没有成功。从Google文档中可以看出,需要创建一个Google.maps.Place对象,但是如何创建呢?我查阅了文档(),但没有看到构造函数。我如何解决这个问题?非常感谢。试试这个
directionsService.route({
origin: {placeId: start},
destination: {placeId: locationID}
...
试试这个
directionsService.route({
origin: {placeId: start},
destination: {placeId: locationID}
...
有两种不同的选择 如果您想使用位置id
directionsService.route({
origin: {placeId: start},
destination: {placeId: locationID})
如果您想使用lat和long
directionsService.route({
origin: {location: {lat:33.699234,lng:-102.870486}},
destination: {location: {lat:33.123366,lng:-102.862864}},
travelMode: "DRIVING"
还要确保在谷歌控制台中配置方向服务
这里是链接
有两种不同的选项可供选择 如果您想使用位置id
directionsService.route({
origin: {placeId: start},
destination: {placeId: locationID})
如果您想使用lat和long
directionsService.route({
origin: {location: {lat:33.699234,lng:-102.870486}},
destination: {location: {lat:33.123366,lng:-102.862864}},
travelMode: "DRIVING"
还要确保在谷歌控制台中配置方向服务
这里是链接
可能的重复可能的重复可能的重复可能的重复可能的重复