0

我正在尝试使用谷歌地图 API 在谷歌地图上创建两个多边形。

我收到以下错误:

误差:构造函数的无效值0:-94.963194,39.316858,-94.95967,39.32199,-95846,39.3219,95846,39.3214,-95846,39.3214,-95846,39.3214,-95846,39.39.32104,-94.95742,39.32064,-94.95698,39.32021,-94.95625,39.31927, - 94.95599,39.31876,-94.95578​​,39.31824,-94.95564,39.3177,-94.95557,39.31716,-94.95557,39.31661,-94.963194,39.316858

我希望有人可以帮助解释我做错了什么并提供解决方案。

<script
    src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=visualization">
</script>
<script>
var map;

function initialize() {
    var kansas_city = new google.maps.LatLng(39.00495613,-94.64780668);
    var mapOptions = {
        zoom: 10,
        center: kansas_city,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

        // Create a <script> tag and set the USGS URL as the source.
        var script = document.createElement('script');
        script.src = 'sector.json';
        document.getElementsByTagName('head')[0].appendChild(script);
}

  // Loop through the results array and place a marker for each
  // set of coordinates.
  window.sector_callback = function(results) {
  for (var i = 0; i < results.features.length; i++) {
      var coords = results.features[i].geometry.coordinates;
      alert(coords);
      var polygons = new google.maps.Polygon({
      path: coords,
      map: map
      });
  }
}
</script>

JSON 代码:

sector_callback({
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
            "Name": "1_1",
            "Description": ""
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [-94.963194, 39.316858],
                    [-94.959670, 39.321990],
                    [-94.959050, 39.321720],
                    [-94.958460, 39.321400],
                    [-94.957920, 39.321040],
                    [-94.957420, 39.320640],
                    [-94.956980, 39.320210],
                    [-94.956250, 39.319270],
                    [-94.955990, 39.318760],
                    [-94.955780, 39.318240],
                    [-94.955640, 39.317700],
                    [-94.955570, 39.317160],
                    [-94.955570, 39.316610],
                    [-94.963194, 39.316858]
                ]
            ]
        }
    }, {
        "type": "Feature",
        "properties": {
            "Name": "214_1",
            "Description": ""
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [-94.783917, 39.083417],
                    [-94.776470, 39.084670],
                    [-94.776340, 39.084140],
                    [-94.776290, 39.083590],
                    [-94.776300, 39.083040],
                    [-94.776380, 39.082500],
                    [-94.776530, 39.081960],
                    [-94.777020, 39.080940],
                    [-94.777360, 39.080460],
                    [-94.777760, 39.080000],
                    [-94.778210, 39.079570],
                    [-94.778710, 39.079180],
                    [-94.779260, 39.078830],
                    [-94.783917, 39.083417]
                ]
            ]
        }
    }]
});
4

3 回答 3

1

JS 中的数组循环永远不应该以默认方式进行。浏览器将在迭代的每一步查找 results.features.length。这是出于速度考虑。

window.sector_callback = function(results) {
  for (var i = 0, j = results.features.length; i < j; i++) {
      var coords = results.features[i].geometry.coordinates;
      alert(coords);
      var polygons = new google.maps.Polygon({
      path: coords,
      map: map
      });
  }
}

如需答案,请查看此问题

于 2012-10-26T15:05:57.157 回答
1

Google Maps API 中的所有内容都需要使用基于 google 的google.maps.LatLng数据结构。由于 JSON 数据中的值为 ,因此[longitude, latitude]您需要google.maps.LatLng在创建多边形之前使用它们构建对象:

window.sector_callback = function(results) {
  for (var i = 0, len = results.features.length; i < len; i++) {
    var coords = results.features[i].geometry.coordinates[0];
    var path = [];

    for ( var j = 0, len2 = coords.length; j < len2; j++ ){
        path.push(new google.maps.LatLng(coords[j][1], coords[j][0]));
    }

    var polygons = new google.maps.Polygon({
      path: path,
      map: map
    });
  }
}

编辑:我必须0th为坐标指定元素,哎呀!

于 2012-10-26T15:11:10.867 回答
1

您的多边形数组是一个数组数组(以支持多边形中的多个路径)。要访问您需要做的数字(假设您有带有单个路径的简单多边形):

var coords = results.features[i].geometry.coordinates[0];

然后将它们转换为 google.maps.LatLng 对象:

path.push(new google.maps.LatLng(coords[j][1], coords[j][0]));

工作示例

于 2012-10-26T16:19:12.717 回答