0

我正在尝试在建筑物之间的校园内提供步行路线。当我使用以下代码时,开始和结束坐标被放置在最近的命名道路上,而不是指定的特定路径上。

    var request = {
    origin: "34.23974770397957,-118.53099968624116",
        destination: "34.240064785369974,-118.52827992630006",
        travelMode: google.maps.DirectionsTravelMode.WALKING
    };
    directionsService = new google.maps.DirectionsService();
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });

您可以在这里http://www.csun.edu/~kjm39451/maps/mapsError/main.html通过选择两个字段中任何内容的下拉菜单来查看问题。

但我想要的结果看起来像这样https://maps.google.com/maps?saddr=34.23974770397957,-118.53099968624116&daddr=34.240064785369974,-118.52827992630006&hl=en&ll=34.239825,-118.530614&spn=0.003721,0.004823&sll=34.23962, -118.52964&sspn=0.003721,0.004823&geocode=FQR1CgIdSFzv-A%3BFUF2CgId6Gbv-A&dirflg=w&mra=ls&t=m&z=18

如何使用谷歌地图 api 来提供方向,就像谷歌地图一样?

4

1 回答 1

1

的目的地和来源属性DirectionsRequest可以采用字符串或LatLng类。如果您提供 aLatLng它似乎正在工作。

var request = {
  origin: new google.maps.LatLng(34.23974770397957, -118.53099968624116),
  destination: new google.maps.LatLng(34.240064785369974, -118.52827992630006),
  travelMode: google.maps.DirectionsTravelMode.WALKING
};

您可以参考地图 API 文档: https ://developers.google.com/maps/documentation/javascript/3.exp/reference#DirectionsRequest

https://developers.google.com/maps/documentation/javascript/reference#LatLng

于 2012-11-02T21:53:54.757 回答