谷歌用javascript映射步行路线

时间:2012-11-02 21:23:51

标签: javascript json google-maps

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

    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像谷歌地图那样提供方向?

1 个答案:

答案 0 :(得分:1)

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

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
};

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

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