1

我正在使用 Jquery getJson() 生成要显示在 google maps 3 api 上的 json。我想按照以下示例显示折线: 折线简单示例

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

查询:

$.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 以 [new 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 数组吗?或者显示折线的最佳方法是什么。

4

1 回答 1

2

我的网站 ( http://www.cyclistsroadmap.com ) 上有类似的路线查找器。

如果使用 Google 的路线查找器,您可以使用返回的 DirectionsObject。当我使用存储在数据库中的数据时,我只需将其作为包含所有 lat、lng 坐标的 2d 数组发回。(我只是做一个常规的 AJAX 调用,而不是 $.getJSON 调用。AJAX 返回一个 JSON 字符串。

于 2010-06-21T03:03:42.490 回答