2011-09-07 24 views
0

如何根據google api返回的XML或json繪製地圖?Google Map方向查詢

例如,我有一個旅遊信息的網站。管理方的經理可以添加包括多個目的地的旅遊詳情。期望的結果將是顯示方向,地圖和目的地細節的動態地圖。

我讀過google api可以輸出XML或json,那麼我該如何從解析的XML或json代碼中繪製一張圖?

回答

1

這是我在我的網站使用jQuery的一點點(一起使用的功能:

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

function setMap(midpoint) 
{ 
    var latlng = new google.maps.LatLng(midpoint.lat(), midpoint.lng()); 
    var myOptions = { 
     zoom: 12, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), 
              myOptions); 
    directionsDisplay.setMap(map); 

    var request = { 
     origin:"[Put origin here]", 
     destination:"[Put destination here]", 
     travelMode: google.maps.TravelMode.DRIVING 
    }; 

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

} 

function initialize() { 
    var address = "[Put destination here]"; 
    var geocoder = new google.maps.Geocoder(); 
    directionsDisplay = new google.maps.DirectionsRenderer(); 

    var result = ""; 
    geocoder.geocode({'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) 
     { 
      result = results[0].geometry.location; 
      setMap(result); 
     } 
     else 
     { 
      $("#map_canvas").hide(); 
     } 
    }); 
} 
+0

您的解決方案是正確的,我們可以輸入目的地畫在地圖上,但我想畫一個完整的方向。動態地圖,所以我用谷歌方向api。其中使用起源n目的地參數一個url請求,返回一個.json文件,其中全方向地圖是在json文件中寫入。現在我該如何繪製該json文件的地圖。你知道嗎?請幫助。 –

+0

編輯答案。 –

+0

非常感謝多多的傢伙。更多這​​兩個目的地是可能的嗎? –