缺少动态标记GMaps V3

时间:2011-09-09 00:38:20

标签: ajax google-maps

我已经编写了一些代码来通过ajax调用从mysql数据库中获取zipcodes,对它们进行地理编码,然后在GMap上创建标记。可点击标记以显示某些人口统计数据。如果我将每个循环中的警报取消注释,它就可以工作。如果不是,它只显示约8个标记。任何和所有的帮助表示赞赏。相关代码:

function initialize() {
var geocoder = new google.maps.Geocoder();
var markers = [];
var latlng = new google.maps.LatLng("33.7463915", "-117.86044720000001");
var myOptions = {
    zoom: 11,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

$.ajax({
    url: 'getzips.php',
    dataType: 'json',                         
    success: function (data) {                              
        $.each(data.rows, function(i, item) {
                //alert(item.zip);
                if (geocoder) {
                    geocoder.geocode({ 'address': item.zip }, function (results, status) {

                    if (status == google.maps.GeocoderStatus.OK) {
                        var latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());                        
                        var marker = new google.maps.Marker({
                            position: latlng,
                            map: map,
                            title: item.zip 
                            });
                        var infowindow = new google.maps.InfoWindow({  
                            content: item.zip  
                        });  
                        google.maps.event.addListener(marker, "click", function() {                                 
                            $.ajax({
                                url: 'getinfo.php?zipcode=' + marker.title,
                                success: function(data){
                                  infowindow.setContent(data);
                                  infowindow.open(map, marker);
                                }
                             });       
                        });
                        markers.push(marker);                   
                    }
                    });
                }
                else
                    alert("geocode error");
            });
        }
    }); 
    //alert(markers.length);
}

1 个答案:

答案 0 :(得分:0)

Google地理编码API具有查询和速率限制。查询时需要放慢速度。

  

使用Google地理编码API每天的查询限制为2,500个地理定位请求。

     

此外,我们会强制执行请求率限制以防止滥用服务。

相关问题