2010-06-21 98 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