jquery - 迭代 JSON 文件以显示多条折线

标签 jquery google-maps getjson google-polyline

我正在尝试使用从外部 JSON 资源提取的多条折线来渲染 map 。每个 JSON 记录都有许多描述性字段以及一个包含 LatLng 数组的字段。该代码似乎可以很好地获取 JSON 数据并对其进行适当的解析。然后,我迭代映射一条折线的每个记录,但由于某种原因我无法让它显示在 map 上。这是我第一次使用 Google Maps API。我可能在做一些愚蠢的事情,但在浏览了尽可能多的例子后,我找不到任何明显错误的地方。非常感谢所有建议。

显示折线的代码的基础取自以下示例:http://code.google.com/intl/no/apis/maps/documentation/javascript/examples/polyline-simple.html

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
     #map_canvas {
        width: 1024px;
        height: 700px;
      }
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">

function initialize(){
    var latlng = new google.maps.LatLng(0, 23);
    var myOptions = {
      zoom: 4,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var url = "http://webstore.thedatahub.org/stevesong/afterfibre/testdata2.json?_limit=3";
    var path = [];
        $.getJSON(url, function (data) {

            //  Parse the Linestring field into an array of LatLngs
            $.each(data.data, function(index, record) {
                line = JSON.parse(record.Linestring);

                //  Parse the array of LatLngs into Gmap points
                for(var i=0; i < line.length; i++){
                    path.push(new google.maps.LatLng(line[1],line[0]));
                }
                var polyline = new google.maps.Polyline({
                  path: path,
                  strokeColor: '#ff0000',
                  strokeOpacity: 1.0,
                  strokeWeight: 3
                });

                polyline.setMap(map);

            });

        });
    }

    // google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body onload="initialize()">
  <div id="map_canvas"></div>
</body>
</html>

最佳答案

感谢 Greg Mahlknecht 提供了最后一 block 拼图。为了使每个 JSON 记录唯一显示而不是一条巨大的折线,需要在每次“.each”迭代后重新初始化折线数组。这是最终完成我所追求的最终版本。谢谢米奇和格雷格。

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
     #map_canvas {
        width: 1024px;
        height: 700px;
      }
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">

function initialize(){
    var latlng = new google.maps.LatLng(0, 23);
    var myOptions = {
      zoom: 4,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var url = "http://webstore.thedatahub.org/stevesong/afterfibre/testdata2.json?_limit=5";

        $.getJSON(url, function (data) {

            //  Parse the Linestring field into an array of LatLngs
            $.each(data.data, function(index, record) {
                var mypath = new Array();
                line = JSON.parse(record.Linestring);
                //  Parse the array of LatLngs into Gmap points
                for(var i=0; i < line.length; i++){
                    //Tokenise the coordinates
                    var coords = (new String(line[i])).split(",");
                    mypath.push(new google.maps.LatLng(coords[1], coords[0]));
                }
                var polyline = new google.maps.Polyline({
                    path: mypath,
                    strokeColor: '#ff0000',
                    strokeOpacity: 1.0,
                    strokeWeight: 3
                });
                polyline.setMap(map);
            });

        });
    }

    // google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body onload="initialize()">
  <div id="map_canvas"></div>
</body>
</html>

关于jquery - 迭代 JSON 文件以显示多条折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8695462/

相关文章:

javascript - 用户登录后如何从布局页面中删除登录按钮

javascript - 是否有可能在javascript中获取撤消文本

jquery - 删除特定的 div

javascript - 在 google maps api v3 中完成多边形绘制时的处理

javascript - JavaScript 中的匿名回调无法正常工作

php - 从存储在 MySql 中的数据创建 json API 请求响应

javascript - 我如何使用 jQuery 从 JSON 文件中读取详细信息?

javascript - 仅 HTML/JS 和谷歌地图上的实时 GPS 跟踪器可以在手机上运行?可能吗?

google-maps - 在 map 上显示 100k+ 点的最佳方式

javascript - jQuery 将 JSON 值插入到具有特定类的元素