2012-11-02 77 views
0

我正試圖在校園之間提供建築物之間的步行路線。當我使用下面的代碼時,開始和結束座標被放置在最近的命名道路上,而不是指定的特定路徑。Google地圖步行路線與javascript

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

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

你可以參考地圖API文檔: https://developers.google.com/maps/documentation/javascript/3.exp/reference#DirectionsRequest

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

+0

非常感謝你。 –