【问题标题】:Parsing Json to display polyline/markers with google maps v3解析 Json 以使用谷歌地图 v3 显示折线/标记
【发布时间】:2011-03-06 03:21:25
【问题描述】:

我正在使用 Jquery getJson() 生成要在 google maps 3 api 上显示的 json。 我想按照以下方式显示折线: polyline-simple example

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

【问题讨论】:

    标签: jquery json google-maps-api-3 polyline


    【解决方案1】:

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

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

    【讨论】:

    • 我发现这个教程很有帮助:arunxjacob.blogspot.com/2010/04/…
    • @paullb 一年多来,我一直在玩弄这样的路线图;搜索从 json 加载折线坐标将我带到这里,我喜欢我所看到的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2014-03-05
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多