首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google限界错误(无法读取属性e3)

Google限界错误(无法读取属性e3)
EN

Stack Overflow用户
提问于 2015-10-05 18:21:21
回答 2查看 1.6K关注 0票数 0

我是非常新的,与json积分的边界,我正在努力使地图的中心根据当前的点。

我的最新小提琴:http://jsfiddle.net/inkedraskal/3fchn090/9/

如有任何参考或建议,将不胜感激。

我目前的js如下:

代码语言:javascript
复制
(function() {

    window.onload = function() {
        var start_point = new google.maps.LatLng(0, 0);

        // Creating a new map
        var map = new google.maps.Map(document.getElementById("map"), {
          center: start_point,
          zoom: 6,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });


        // Creating the JSON data
        var json = [
            {
                "title": "Stockholm",
                "lat": 59.3,
                "lng": 18.1,
                "description": "<strong>Stockholm</strong> is the capital and the largest city of Sweden and constitutes the most populated urban area in Scandinavia with a population of 2.1 million in the metropolitan area (2010)"
            },
            {
                "title": "Oslo",
                "lat": 59.9,
                "lng": 10.8,
                "description": "<strong>Oslo</strong> is a municipality, and the capital and most populous city of Norway with a metropolitan population of 1,442,318 (as of 2010)."
            },
            {
                "title": "Copenhagen",
                "lat": 55.7,
                "lng": 12.6,
                "description": "<strong>Copenhagen</strong> is the capital of Denmark and its most populous city, with a metropolitan population of 1,931,467 (as of 1 January 2012)."
            }
        ]

        // Creating a global infoWindow object that will be reused by all markers
        var infoWindow = new google.maps.InfoWindow();

        function setMarkerPoints(map){
            var bounds = new google.maps.LatLngBounds();
            // Looping through the JSON data
            for (var i = 0, length = json.length; i < length; i++) {
                var data = json[i],
                    latLng = new google.maps.LatLng(data.lat, data.lng);

                // Creating a marker and putting it on the map
                var marker = new google.maps.Marker({
                    position: latLng,
                    map: map,
                    title: data.title
                });

                // Creating a closure to retain the correct data, notice how I pass the current data in the loop into the closure (marker, data)
                (function(marker, data) {

                    var windowContent = '<h3>'+ data.title +'</h3>'+
                                        '<p>'+ data.description + '</p>'
                    console.log(windowContent);

                    // Attaching a click event to the current marker
                    //google.maps.event.addListener(marker, "click", function(e) {
                        //infoWindow.setContent(data.title + data.description);
                    //  infoWindow.setContent(windowContent);
                    //    infoWindow.open(map, marker);
                    //});
                    infobox = new InfoBox({
                        content: infoWindow.setContent(windowContent),
                        alignBottom: true,
                        pixelOffset: new google.maps.Size(-160, -45)
                    });

                    google.maps.event.addListener(marker, 'click', function () {

                        // Open this map's infobox
                        infobox.open(map, marker);
                        infobox.setContent(windowContent);
                        map.panTo(marker.getPosition());
                        infobox.show();
                    });
                    google.maps.event.addListener(map, 'click', function () {
                        infobox.setMap(null);
                    });


                })(marker, data);
                //END MARKER DATA

            }
            // end loop through json
            bounds.extend(start_point);
            map.setCenter(start_point);
            map.fitBounds(bounds);
        }

        setMarkerPoints();



    }

})();
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-05 19:03:32

  1. bounds.extend调用放入标记循环中,并将标记的所有位置添加到其中:
代码语言:javascript
复制
bounds.extend(marker.getPosition());
  1. 在调用setMarkers时输入一个错误,需要将映射传递到该函数中:
代码语言:javascript
复制
setMarkerPoints(map);

更新小提琴

代码片段:

代码语言:javascript
复制
(function() {

  window.onload = function() {
    var start_point = new google.maps.LatLng(0, 0);

    // Creating a new map
    var map = new google.maps.Map(document.getElementById("map"), {
      center: start_point,
      zoom: 6,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });


    // Creating the JSON data
    var json = [{
      "title": "Stockholm",
      "lat": 59.3,
      "lng": 18.1,
      "description": "<strong>Stockholm</strong> is the capital and the largest city of Sweden and constitutes the most populated urban area in Scandinavia with a population of 2.1 million in the metropolitan area (2010)"
    }, {
      "title": "Oslo",
      "lat": 59.9,
      "lng": 10.8,
      "description": "<strong>Oslo</strong> is a municipality, and the capital and most populous city of Norway with a metropolitan population of 1,442,318 (as of 2010)."
    }, {
      "title": "Copenhagen",
      "lat": 55.7,
      "lng": 12.6,
      "description": "<strong>Copenhagen</strong> is the capital of Denmark and its most populous city, with a metropolitan population of 1,931,467 (as of 1 January 2012)."
    }];

    // Creating a global infoWindow object that will be reused by all markers
    var infoWindow = new google.maps.InfoWindow();

    function setMarkerPoints(map) {
      var bounds = new google.maps.LatLngBounds();
      // Looping through the JSON data
      for (var i = 0, length = json.length; i < length; i++) {
        var data = json[i],
          latLng = new google.maps.LatLng(data.lat, data.lng);

        // Creating a marker and putting it on the map
        var marker = new google.maps.Marker({
          position: latLng,
          map: map,
          title: data.title
        });

        // Creating a closure to retain the correct data, notice how I pass the current data in the loop into the closure (marker, data)
        (function(marker, data) {

          var windowContent = '<h3>' + data.title + '</h3>' +
            '<p>' + data.description + '</p>';
          console.log(windowContent);

          // Attaching a click event to the current marker
          infobox = new InfoBox({
            content: infoWindow.setContent(windowContent),
            alignBottom: true,
            pixelOffset: new google.maps.Size(-160, -45)
          });

          google.maps.event.addListener(marker, 'click', function() {

            // Open this map's infobox
            infobox.open(map, marker);
            infobox.setContent(windowContent);
            map.panTo(marker.getPosition());
            infobox.show();
          });
          google.maps.event.addListener(map, 'click', function() {
            infobox.setMap(null);
          });
        })(marker, data);
        //END MARKER DATA
        bounds.extend(marker.getPosition());
      }
      // end loop through json

      map.setCenter(start_point);
      map.fitBounds(bounds);
    }
    setMarkerPoints(map);
  };
})();
代码语言:javascript
复制
html,
body {
  height: 100%;
  width: 100%;
}
#map {
  display: block;
  height: 100%;
}
.infoBox {
  max-width: 300px;
  background: #fff;
  padding: 10px;
  border: 1px solid red; //so pilot red
  img {
    border: 1px solid yellow;
  }
}
代码语言:javascript
复制
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script>
<script src="https://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js"></script>
<div id="map"></div>

票数 2
EN

Stack Overflow用户

发布于 2015-10-05 19:02:06

下面是我在上一个项目中使用的函数,对您的代码进行了快速调整

代码语言:javascript
复制
function recenterMap(map, json) {
    map.bounds = new google.maps.LatLngBounds();
    for (var i = 0, length = json.length; i < length; i++) {
        var latLng = new google.maps.LatLng(json[i].lat, json[i].lng);
        map.bounds.extend(latLng);
        map.fitBounds(map.bounds);
    };
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32955280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档