2

我正在尝试使用从外部 JSON 资源中提取的多条折线来渲染地图。每个 JSON 记录都有许多描述性字段以及一个包含 LatLngs 数组的字段。该代码似乎可以很好地获取 JSON 数据并对其进行适当的解析。然后我遍历每条记录映射一条折线,但由于某种原因我无法让它显示在地图上。这是我第一次使用 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>
4

2 回答 2

1

感谢 Greg Mahlknecht 提供了最后一块拼图。为了让每条 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>
于 2012-01-07T11:30:15.410 回答
0

问题不是在循环“线”时标记坐标

<!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 mypath = new Array();
        $.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++){
        //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>
于 2012-01-05T13:31:23.183 回答