2010-06-21 102 views
1

我正在使用Jquery getJson()生成一個JSON以顯示在谷歌地圖3 API。 我想顯示多行按照: polyline-simple example解析Json以顯示折線/標記與谷歌地圖v3

function initialize() { 
    var myLatLng = new google.maps.LatLng(0, -180); 
    var myOptions = { 
     zoom: 3, 
     center: myLatLng, 
     mapTypeId: google.maps.MapTypeId.TERRAIN 
    }; 

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

    var flightPlanCoordinates = [ 
     new google.maps.LatLng(37.772323, -122.214897), 
     new google.maps.LatLng(21.291982, -157.821856), 
     new google.maps.LatLng(-18.142599, 178.431), 
     new google.maps.LatLng(-27.46758, 153.027892) 
    ]; 
    var flightPath = new google.maps.Polyline({ 
     path: flightPlanCoordinates, 
     strokeColor: "#FF0000", 
     strokeOpacity: 1.0, 
     strokeWeight: 2 
    }); 

    flightPath.setMap(map); 
    } 

JQuery的:

$.getJSON("routefinder", { "lat1": lat1 , "lng1": lng1 , "lat2": lat2 , "lng2": lng2 }, function(json) { 
    var poly = new google.maps.Polyline({ 
     path: json, 
     strokeColor: "#FF0000", 
     strokeOpacity: 1.0, 
     strokeWeight: 2 
    }); 

    poly.setMap(map); 
}); 

的$ .getJSON返回一個字符串與折線在[新google.maps.LatLng座標(lat,lng)]格式:

[ new google.maps.LatLng(53.369567, -6.323699), new google.maps.LatLng(53.367705, -6.317386),new google.maps.LatLng(53.337705,-6.917386), new google.maps.LatLng(53.397705,-6.47386)] 

但是沒有生成線,我可以返回一個包含google.maps.LatLng對象的json數組嗎?或者什麼是最好的方法來顯示折線。

回答

2

我對我的網站(http://www.cyclistsroadmap.com)有類似的路線查找工具。

如果使用Google Googles路徑查找器,則可以使用返回的DirectionsObject。當我使用存儲在數據庫中的數據時,我將它作爲包含所有lat,lng座標的二維數組發送回來。 (我只是做一個常規的AJAX調用,但不是$ .getJSON調用。AJAX返回一個JSON字符串。

+0

我發現本教程非常有幫助:http://arunxjacob.blogspot.com/2010/04usingusing- google-maps-api-v3-jquery.html – patrickandroid 2010-06-21 08:27:53

+0

@paullb我一直在做這樣的路線圖玩弄了一年左右;尋找從json加載多段線coords帶我到這裏,我喜歡我所看到的! – panhandel 2014-01-13 21:37:34