2016-04-21 139 views
5

路线只创建一个使用PlaceId一个途径,如果我用经纬度或字符串PARAMS但我需要通过PlaceId创建它,但它不工作谷歌地图API(JS)创建航点

例如:

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

你试过选中此相关[SO问题](http://stackoverflow.com/questions/33990153 /传递到位-ID定位到目的地功能于谷歌 - 地图-API)?你的错误日志是什么? – abielita

+0

我试过但没有结果。 placeId仅适用于原产地和目的地。我没有找到结果 –

回答

4

只需要通过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 
} 

位置指定路标的位置,作为一个经纬度,作为 google.maps.Place对象或作为可进行地理编码的字符串。

Google Maps - Direction Services Documentation

Here's the JsFiddle

3

我得到张贴代码JavaScript错误:Uncaught TypeError: google.maps.Place is not a constructor在这条线:

waypoints: [{stopover: true, location: new google.maps.Place('ChIJRVj1dgPP20YRBWB4A_sUx_Q')}], 

你需要指定location你做同样的方式origindestination placeIds:

waypoints: [{ 
    stopover: true, 
    location: {'placeId':"ChIJRVj1dgPP20YRBWB4A_sUx_Q"} 
}], 

说明在documentation

Google.maps.Place对象规范

placeId |类型:字符串 地点的地点ID(例如业务或兴趣点)。地点ID是Google地图数据库中某个地点的唯一标识符。请注意,placeId是识别地点的最准确的方式。如果可能,你应该指定placeId而不是placeQuery。可以从对Places API的任何请求中检索地点ID,例如TextSearch。地点ID也可以从请求中检索到Geocoding API。欲了解更多信息,请参阅overview of place IDs

proof of concept fiddle

代码片段:

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"></script> 
 
<div id="map_canvas"></div>