【问题标题】:Google Maps API V3 requires page reload to show marker iconsGoogle Maps API V3 需要重新加载页面才能显示标记图标
【发布时间】:2012-02-22 21:41:02
【问题描述】:

我正在以 JSON 格式从数据库中读取点,以在页面上创建地图标记和非结构化列表。添加一些代码以自定义列表元素后,地图在第一次请求时停止显示标记图标 - 直到发出页面重新加载。这是由于 API 超时造成的吗?加载地图后是否可以从数组构建列表对象,或者是否有其他方法可以加快可能消除问题的代码?地图加载的标记很好,标记数量翻了一番(300+),所以我知道问题是由于将格式添加到列表对象。不需要聚类。该页面的演示版可在此处获得

签署了 JS n00b。谢谢。 …… JSON 支持的 GOOGLEMAP

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

<script type="text/javascript" charset="utf-8"> 
var map;
    var infoWindow = new google.maps.InfoWindow();
    function initialize() {
        var myLatlng = new google.maps.LatLng(49.57154029531499,-125.74951171875);

        var myOptions = {
            zoom: 8,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.TERRAIN,
            streetViewControl: false
            }

        this.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        } /* end initialize function */


<!--  load points from database into Locations JSON -->

$(document).ready(function () {
    initialize();
    $.getJSON("map-service.php?action=listpoints", function(json) {

    if (json.Locations.length > 0) {
        for (i=0; i<json.Locations.length; i++) {
            var location = json.Locations[i];
                addMarker(location); 
                }
            }
        });

    //define grn icon as closed
    var greenicon = 'http://maps.google.com/mapfiles/ms/icons/green-dot.png';

    function addMarker(location) {
            if(location.datesummit == "0000-00-00") {
                var markerOptions = {map: map, title: location.name, position: new google.maps.LatLng(location.lat, location.lng),icon: greenicon};
                //create marker info window content
                var html='<B>'+location.name+'</B><BR> Register a summit <A href="http://goo.gl/Nl0UQ">here</A>  ';
                //create list element text and onclick          
                $("<li class=\"notclimbed\">")
                .html(location.name+"</li>") 
                .click(function(){ 
                    infoWindow.close();
                    infoWindow.setContent(html);
                    infoWindow.open(map, marker)
                }) 
                .appendTo("#list"); 
            }
            else{
                var markerOptions = {map: map, title: location.name, position: new google.maps.LatLng(location.lat, location.lng)};
                //create marker info window content
                var html='<B>'+location.name+'</B><BR> Summitted: '+location.datesummit+'<BR> By:'+location.summitlog;
                //create list element text and onclick  
                $("<li class=\"climbed\">")
                .html(location.name+"</li>") 
                .click(function(){ 
                    infoWindow.close();
                    infoWindow.setContent(html);
                    infoWindow.open(map, marker)
                }) 
                .appendTo("#list"); 
            }
            var marker = new google.maps.Marker(markerOptions);

            // add a listener to open an info window when a user clicks on one of the markers
            google.maps.event.addListener(marker, 'click', function() {
                infoWindow.close();
                infoWindow.setContent(html);
                infoWindow.open(map, marker);
                });

        }; // end of addmarker function
});
</script>
</head>


<body>
<div id="banner" {vertical-align:text-top;} >
    <img src="test.jpg" alt="Logo" style="width:150px;height:150px;vertical-align:middle">
    <img src="test.png" alt="Logo" style="vertical-align:middle">
</div>

<div id="map_canvas" >
        Map Here!
</div>

<div id="mindthegap"></div>

<div id="list" >    </div>
</body>

【问题讨论】:

  • 这是一个长镜头,但请尝试在 map-service.php 页面上放置“Content-Type: application/json”的标题。
  • @corbin 感谢您的提示-我知道这应该在响应中-但是正如您所建议的那样……它并没有改变结果。
  • 你看过我的回答了吗?这绝对是问题所在(或者可能是“一个”问题——不确定是否是唯一的问题)。

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


【解决方案1】:

在将 map 变量传递给 markerOptions 之前,您需要确保它已初始化。

一些过分热心的调试告诉我,在页面失败的时候,地图仍然是未定义的。

$(document).ready() 通常出现在 body.onload 之前,所以要么在 $(document).ready(function() { ... } 的最顶部调用 initialize() );或者把初始化代码放在那里。

此外,尽管并非绝对必要,但您应该考虑封装 map 变量而不是使用全局变量。如果您想在一页上有 2 个地图怎么办?

【讨论】:

  • 这是一个时间问题。将初始化调用移动到 $(document).ready(function() 的顶部就可以了。谢谢!@Corbin
  • 没问题。很高兴我能帮忙:)。
猜你喜欢
  • 1970-01-01
  • 2012-04-12
  • 1970-01-01
  • 1970-01-01
  • 2011-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多