![](/img/trans.png)
[英]Google Maps / TypeScript - Not Consistently Recognizing google.maps.Place
[英]Google Maps Api - Passing google.maps.Place to Direction Service
根據Google的文檔記錄,可以將位置的Google Place ID傳遞給Direciton Service。 但是,無論我嘗試哪種組合,我都絕對無法使它起作用。 我收到一個NOT_FOUND錯誤。 我已經嘗試將ID硬編碼為無濟於事的測試。
基本初始化代碼:
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
我嘗試僅將場所ID作為字符串傳遞,但沒有成功。 我嘗試給它們加上前綴,但仍然沒有成功。 從Google文檔看來,需要創建一個google.maps.Place對象,但是如何? 我查閱了文檔( https://developers.google.com/android/reference/com/google/android/gms/location/places/Place#getId() ),但沒有看到構造函數。 我該如何解決這個問題? 非常感謝。
嘗試這個
directionsService.route({
origin: {placeId: start},
destination: {placeId: locationID}
...
有兩種不同的選擇
如果您想使用地點編號
directionsService.route({
origin: {placeId: start},
destination: {placeId: locationID})
如果您想使用經緯度
directionsService.route({
origin: {location: {lat:33.699234,lng:-102.870486}},
destination: {location: {lat:33.123366,lng:-102.862864}},
travelMode: "DRIVING"
並確保您在Google控制台中配置導航服務
這是該鏈接
https://developers.google.com/maps/documentation/javascript/directions
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.