只有当我使用LatLng或String参数时才会创建路径,但是我需要通过PlaceId创建它,但它不起作用
示例:
directionsService.route({
origin: {'placeId': 'ChIJc1lGdwfP20YR3lGOMZD-GTM'},
destination: {'placeId': 'ChIJdTGhqsbP20YR6DZ2QMPnJk0'},
waypoints: [{stopover: true, location: new google.maps.Place('ChIJRVj1dgPP20YRBWB4A_sUx_Q')}],
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
}
答案 0 :(得分:5)
只需将google.maps.Place
对象作为航点位置传递即可。
例如:
directionsService.route({
origin: { placeId: "ChIJc1lGdwfP20YR3lGOMZD-GTM" },
destination: { placeId: "ChIJdTGhqsbP20YR6DZ2QMPnJk0" },
waypoints: [{ stopover: true, location: { placeId: "ChIJRVj1dgPP20YRBWB4A_sUx_Q" } }],
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
}
位置指定航点的位置,作为LatLng,作为 google.maps.Place对象或将作为地理编码的String。
答案 1 :(得分:4)
我在此行中发布了代码:Uncaught TypeError: google.maps.Place is not a constructor
的javascript错误:
waypoints: [{stopover: true, location: new google.maps.Place('ChIJRVj1dgPP20YRBWB4A_sUx_Q')}],
您需要使用与location
和origin
placeIds相同的方式指定destination
:
waypoints: [{
stopover: true,
location: {'placeId':"ChIJRVj1dgPP20YRBWB4A_sUx_Q"}
}],
documentation中的说明:
Google.maps.Place对象规范
placeId |输入:string 地点的地点ID(例如商家或兴趣点)。地点ID是Google地图数据库中地点的唯一标识符。请注意,placeId是识别地点的最准确方式。如果可能,您应该指定placeId而不是placeQuery。可以从Places API的任何请求中检索地点ID,例如TextSearch。也可以从对Geocoding API的请求中检索地点ID。有关详细信息,请参阅overview of place IDs。
代码段
function initialize() {
var map = new google.maps.Map(document.getElementById("map_canvas"));
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer({
map: map
});
directionsService.route({
origin: {
'placeId': 'ChIJc1lGdwfP20YR3lGOMZD-GTM'
},
destination: {
'placeId': 'ChIJdTGhqsbP20YR6DZ2QMPnJk0'
},
waypoints: [{
stopover: true,
location: {
'placeId': "ChIJRVj1dgPP20YRBWB4A_sUx_Q"
}
}],
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>