2

外部 JSON リソースから取得した複数のポリラインを含むマップをレンダリングしようとしています。各 JSON レコードには、多数の説明フィールドと、LatLng の配列を含むフィールドがあります。コードは 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 に感謝します。1 つの巨大なポリラインとは対照的に、各 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 に答える