【问题标题】:Google Maps Ajax multiple markers JsonGoogle Maps Ajax 多个标记 Json
【发布时间】:2016-01-05 03:00:42
【问题描述】:

我有以下内容:https://jsfiddle.net/inkedraskal/h35dz9qd/5/

我收到一个错误:无法分配给 0 的只读属性“__e3_”

我之前有一个 for 循环,它在 for 循环之后调用该函数,但 jshint 不接受它。所以现在我正在使用 Ajax,但我被卡住了。我可以返回控制台中的对象以及第一个信息框内容(请参阅控制台),然后出现错误。

js如下:(任何提示,技巧等将不胜感激)

(function () {
    renderGoogleMap();


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

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



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

        function setMarkerPoints(map,marker) {
            var bounds = new google.maps.LatLngBounds();
            // Looping through the JSON data

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



                $.ajax({
                   type: "GET",
                    url: 'https://raw.githubusercontent.com/roryheaney/jsonexample/master/locatoins.json',
                    dataType: "json",
                    success: function (data) {
                        console.log(data);

                        if (data.length !== 0) {

                         var   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
                        });


                            $.each(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);
                                });
                            });

                        } 


                    },
                    error: function (data) {
                        // alert('Please refresh the page and try again');

                        console.log('Please refresh the page and try again');
                    }

                });
                //END MARKER DATA


                // bounds.extend(marker.getPosition());

            // end loop through json

            map.setCenter(start_point);
            map.fitBounds(bounds);
        }
        setMarkerPoints(map);
    }

})();

【问题讨论】:

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


    【解决方案1】:

    改动列表:

    1.Google Maps API 包含它自己的事件,一旦页面加载就会触发,所以我替换了这些行:

    (function () {
        renderGoogleMap();
    
        //...
    
    
    })();
    

    function renderGoogleMap() {
        //...
      }
    
    google.maps.event.addDomListener(window, 'load', renderGoogleMap);  
    

    2.增加了标记lat/lng边界的初始化。

    3.其他一些小修复。

    Working example

    代码 sn-p:

    function renderGoogleMap() {
      var start_point = new google.maps.LatLng(0, 0);
    
      // Creating a new map
      var map = new google.maps.Map(document.getElementById("locations-map"), {
        center: start_point,
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
    
    
    
      // Creating a global infoWindow object that will be reused by all markers
      var infoWindow = new google.maps.InfoWindow();
    
      function setMarkerPoints(map, marker) {
        var bounds = new google.maps.LatLngBounds();
    
    
        $.ajax({
          type: "GET",
          url: 'https://raw.githubusercontent.com/roryheaney/jsonexample/master/locatoins.json',
          dataType: "json",
          success: function(data) {
    
            if (data.length !== 0) {
    
    
              $.each(data, function(marker, data) {
    
    
                var latLng = new google.maps.LatLng(data.lat, data.lng);
                bounds.extend(latLng);
    
                // Creating a marker and putting it on the map
                var marker = new google.maps.Marker({
                  position: latLng,
                  map: map,
                  title: data.title
                });
    
    
                var windowContent = '<h3>' + data.title + '</h3>' +
                  '<p>' + data.description + '</p>';
    
                // 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);
                });
              });
              map.fitBounds(bounds);
    
            }
    
          },
          error: function(data) {
            console.log('Please refresh the page and try again');
          }
        });
        //END MARKER DATA
    
        // end loop through json
    
      }
      setMarkerPoints(map);
    }
    
    
    google.maps.event.addDomListener(window, 'load', renderGoogleMap);
    // renderGoogleMap();
    #locations-map {
      display: block;
      height: 500px;
    }
    .infoBox {
      max-width: 300px;
      background: #fff;
      padding: 10px;
      border: 1px solid red;
    }
    .infoBox img {
      border: 1px solid yellow;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="https://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js"></script>
    <div id="locations-map"></div>

    【讨论】:

    • 在您的小提琴中,您注释掉了“google.maps.event.addDomListener(window, 'load', renderGoogleMap); ”。是意外还是还没更新?
    • 这是故意评论的,因为在 JSFiddle 中 google.maps.event.addDomListener(window, 'load', renderGoogleMap); 没有被触发。
    • 啊,明白了。好吧,谢谢你,我真的很感激。不确定您是否知道,但 jshint 说标记已在 .each 语句中定义。有什么想法吗?
    • this one 怎么样,确实和不同作用域的一些变量有冲突。
    • 根据上面的代码,为什么zoom不起作用?
    猜你喜欢
    • 2013-04-12
    • 2012-01-29
    • 2016-09-15
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2015-02-17
    • 2017-04-28
    • 1970-01-01
    相关资源
    最近更新 更多