由于bounds.extend方法导致的递归错误太多

时间:2015-03-20 11:23:19

标签: javascript google-maps google-maps-api-3 google-maps-markers

我正在尝试为我的标记创建绑定和缩放。但我有我在线提到的错误:

bounds.extend(markers[i].position);

当我在for循环中设置markers.length的长度时,它会给出Too much recursion error
我显示了所有标记,但它们没有缩放和限制,我也有我刚才提到的错误。

我的代码是:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style>
        #map-canvas {
            width: 1350px;
            height: 600px;
        }
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.3.js"></script>
    <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script>
        function initialize() {
            //var markers = new Array();
            var markers = [];
            $.ajax({
                type: 'GET',
                url: './Content/TextFile/LongLat.txt',
                //filereader\filereader\Content\TextFile\LongLat.txt
                //  url: './distinctGimeiNo.txt',
                dataType: 'text',
            }).success(function (data) {
                var infowindow = new google.maps.InfoWindow();

                var marker;
                var s2 = data.replace(/^.*$/, " ").replace(/\r\n/g, " ");
                var array = s2.split(/[ ]+/g);
                var test = [].concat.apply([], array.map(function (array) { return array.split(/\s+/); }))

                var col1 = [];
                var col2 = [];
                var col3 = [];
                var j = 0;
                // var currentLocation =
                var mapOptions =
                    {
                        zoom: 2,
                        center: new google.maps.LatLng(73, 23),
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    }
                var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
                for (var i = 0; i <= test.length - 3; i = i + 3) {
                    col1[j] = test[i];
                    col2[j] = test[i + 1];
                    col3[j] = test[i + 2];
                    var myLatlng = new google.maps.LatLng(col3[j], col2[j]);
                    marker = new google.maps.Marker(
                       {
                           position: myLatlng,
                           map: map,
                           title: 'Hello World! ' + col1[j]
                       });
                    markers.push(marker);
                    console.log("test2");
                    j++;
                }
                function AutoCenter() {
                    var bounds = new google.maps.LatLngBounds();
                    for (var i = 0; i < markers.length; i++) {
                        bounds.extend(markers[i].position);
                    }
                    map.fitBounds(bounds);
                }
                AutoCenter();
            })
        }
        google.maps.event.addDomListener(window, 'load', initialize);
        console.log("Check finished2");
    </script>    
</head>
<body>
    <div id="map-canvas"></div>
</body>
</html>

有人可以让我知道问题的解决方案吗?

2 个答案:

答案 0 :(得分:3)

您的第一组坐标无效。将无效坐标放入google.map.LatLngBounds时,结果无法用于配置地图(它会生成您报告的错误too much recursion)。

您的数据的第一行(取自this question)不包含数字。

如果我跳过第一个条目,它对我有用:

    for (var i = 3; i <= test.length - 3; i = i + 3) {
        col1[j] = test[i];
        col2[j] = test[i + 1];
        col3[j] = test[i + 2];
        var myLatlng = new google.maps.LatLng(col3[j], col2[j]);
        marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: 'Hello World! ' + col1[j]
        });
        markers.push(marker);
        console.log("test2"+marker.getPosition().toUrlValue(6));
        j++;
    }

proof of concept fiddle

工作代码段:

function initialize() {
  //var markers = new Array();
  var markers = [];
  /*    $.ajax({
          type: 'GET',
          url: './Content/TextFile/LongLat.txt',
          //filereader\filereader\Content\TextFile\LongLat.txt
          //  url: './distinctGimeiNo.txt',
          dataType: 'text',
      }).success(function (data) {
  */
  var infowindow = new google.maps.InfoWindow();

  var marker;
  /*        var s2 = data.replace(/^.*$/, " ").replace(/\r\n/g, " ");
          var array = s2.split(/[ ]+/g);
          var test = [].concat.apply([], array.map(function (array) {
              return array.split(/\s+/);
          }))
  */
  // from https://stackoverflow.com/questions/29160608/how-to-replace-space-with
  var rows = ["ID LONGITUDE LATITUDE", "0 77.139305 28.795975", "2 77.308929 28.486877", "4 73.820680 18.464110"];
  var test = [].concat.apply([], rows.map(function(row) {
    return row.split(' ');
  }))

  var col1 = [];
  var col2 = [];
  var col3 = [];
  var j = 0;
  // var currentLocation =
  var mapOptions = {
    zoom: 2,
    center: new google.maps.LatLng(73, 23),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  for (var i = 3; i <= test.length - 3; i = i + 3) {
    col1[j] = test[i];
    col2[j] = test[i + 1];
    col3[j] = test[i + 2];
    var myLatlng = new google.maps.LatLng(col3[j], col2[j]);
    marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Hello World! ' + col1[j]
    });
    markers.push(marker);
    console.log("test2:" + marker.getPosition().toUrlValue(6));
    j++;
  }

  function AutoCenter() {
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0; i < markers.length; i++) {
      bounds.extend(markers[i].getPosition());
    }
    map.fitBounds(bounds);
  }
  AutoCenter();
  //    })
}
google.maps.event.addDomListener(window, 'load', initialize);
console.log("Check finished2");
html,
body,
#map-canvas {
  width: 100%;
  height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>

答案 1 :(得分:1)

你出界了......在for循环中用<=替换< ......

for (var i = 0; i < markers.length; i++) {

选中此fiddle maps example以指导您......;)

搜索了一下,我找到了一些信息,也许是你的情况:

  

LatLngBounds()不会将两个任意点作为参数,但SW和NE点在空边界对象上使用.extend()方法

var bounds = new google.maps.LatLngBounds();
bounds.extend(myPlace);
bounds.extend(Item_1);
map.fitBounds(bounds);