2010-07-12 123 views
4

我需要使用谷歌地圖方向來顯示兩個地址之間的方向。使用谷歌地圖GDirections地址

如何顯示使用地址的谷歌地圖?另外,我有源地址和目標地址(沒有經度和緯度),如何顯示使用jQuery的地址之間的方向?

回答

9

這是非常簡單的:

var map = new GMap2(document.getElementById('map_canvas')); 
var directions = new GDirections(map); 

directions.load('from: London, UK to: Glasgow, UK'); 

截圖:

Using google map GDirections with address


UPDATE:

使用v3 API比較冗長了一點,但還是爽快:

var map = new google.maps.Map(document.getElementById('map_canvas'), { 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}); 

var directionsService = new google.maps.DirectionsService(); 
var directionsDisplay = new google.maps.DirectionsRenderer(); 

directionsDisplay.setMap(map); 

var request = { 
    origin: 'London, UK', 
    destination: 'Glasgow, UK', 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
}; 

directionsService.route(request, function(response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
    directionsDisplay.setDirections(response); 
    } 
}); 
+0

我需要包含哪些google腳本文件? 它是否可用於最新版本的GMAP(版本3)? – Prasad 2010-07-12 11:50:23

+0

@Prasad:我提供的例子是針對v2 API的。因此,您需要包含腳本'http://maps.google.com/maps?file = api&v = 2&sensor = false&key = your_api_key' ...讓我用v3 API示例更新我的答案:) – 2010-07-12 12:02:45

+0

@ Prasad:我已經用v3 API示例更新了我的答案。在這種情況下,您需要包含「http://maps.google.com/maps/api/js?sensor= false」腳本。 – 2010-07-12 12:09:34