【问题标题】:Google Maps Api - Passing google.maps.Place to Direction ServiceGoogle Maps Api - 将 google.maps.Place 传递给方向服务
【发布时间】:2017-02-04 02:43:22
【问题描述】:

根据 Google 文档,可以将位置的 Google Place ID 传递给 Direciton 服务。但是,无论我尝试什么组合,我都绝对无法让它发挥作用;我收到一个 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.maps.Place 对象,但是如何?我查阅了文档(https://developers.google.com/android/reference/com/google/android/gms/location/places/Place#getId()),但没有看到构造函数。我该如何解决这个问题?非常感谢。

【问题讨论】:

标签: javascript google-maps


【解决方案1】:

试试这个

directionsService.route({
    origin: {placeId: start},
    destination: {placeId: locationID}
    ...

【讨论】:

    【解决方案2】:

    有两种不同的选择

    如果你想使用地点 id

    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"
    

    并确保您在谷歌控制台中配置了方向服务

    这里是链接

    https://developers.google.com/maps/documentation/javascript/directions

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 2012-11-25
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多