谷歌地图超过查询限制 [英] google maps over query limit

查看:79
本文介绍了谷歌地图超过查询限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道类似的问题已经发布,但我没有找到答案,因为它涉及到我的特定问题。

我有一个使用谷歌地图将客户邮编放在地图上的JavaScript。我遇到的问题与其他人已发布的问题类似 - 我收到超出查询限制错误。

我尝试了不同的设置,使用setTimeOut尝试在允许的时间间隔内向Google发送数据,但是我无法使其工作。



这是我的动作:

 函数initialize()
{
var rowNum = 0;
var rowColor =;

var latlng = new google.maps.LatLng(27.91425,-82.842617);

var myOptions =
{
zoom:7,
center:latlng,
mapTypeId:google.maps.MapTypeId.ROADMAP
} ;

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

geocoder = new google.maps.Geocoder();
$ b data.forEach(function(mapData,idx)
{
window.setTimeout(function()
{
geocoder.geocode({'address ':mapData.address},函数(结果,状态)
{
if(status == google.maps.GeocoderStatus.OK)
{
var marker = new google。 maps.Marker({
map:map,
position:results [0] .geometry.location,
title:mapData.title,
icon:getIcon(mapData.type)
});

var contentHtml =< div style ='width:250px; height:90px'>< strong>+ mapData.title +< / strong> < br /> ;\"+mapData.address+\"</div>;

var infowindow = ne w google.maps.InfoWindow({
content:contentHtml
});

google.maps.event.addListener(marker,'click',function()
{
infowindow.open(map,marker);
});

marker.locid = idx + 1;
marker.infowindow = infowindow;
标记[markers.length] = marker;

if(idx%2 == 0)
{
rowColor ='style =background-color:#00FFFF;';
}
else
{
rowColor ='style =background-color:#FFFFFF;';
}

var sideHtml ='< div'+ rowColor +'class =locdata-locid ='+ marker.locid +'>< b>'+ mapData.title + '< / b><峰; br />';
sideHtml + = mapData.address +'< / div>';
$(#locs)。append(sideHtml);

//我们都完成了吗?不是100%肯定这个
if(markers.length == data.length)doFilter();
}
else
{
// alert(由于以下原因,Geocode不成功:+ status);
}
},3000);
});
});

当我使用这个动作运行我的页面时,即使我拥有多个在我的JSON字符串中。 window.setTimeout绝对没有影响 - 我显然在这里做错了。



我希望在这个问题上有任何帮助。



谢谢, 我找到了我的问题的答案。我在Web上找到了下面的代码,并将其修改为我的需要。

使用它,您可以加载多个标记,而不会超过Google的查询限制。

我用超过100个标记测试过它,它工作得很好。该页面根本不会冻结。

我相信你们中的一些人可以做得更加优雅高效,但这是一个很好的起点。

 < script type =text / javascript> 
//<![CDATA [

//显示ani gif
loadingGMap();

//地理编码请求之间的延迟 - 在撰写本文时,100毫秒似乎很好用
var delay = 100;

// ======创建地图对象======
var infowindow = new google.maps.InfoWindow();
var latlng = new google.maps.LatLng(27.989551,-82.462235);

var mapOptions =
{
zoom:7,
center:latlng,
mapTypeId:google.maps.MapTypeId.ROADMAP
}

var geo = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById(map),mapOptions);
var bounds = new google.maps.LatLngBounds();

// ====== Geocoding ======
function getAddress(search,next)
{
geo.geocode({address:搜索},函数(结果,状态)
{
//如果成功
if(status == google.maps.GeocoderStatus.OK)
{
//让我们假设第一个标记是我们想要的标记
var p = results [0] .geometry.location;
var lat = p.lat();
var lng = p .lng();

//输出数据
var msg ='address =''+ search +''lat ='+ lat +'lng ='+ lng +'(delay = '+ delay +'ms)< br>';
//document.getElementById(\"messages\").innerHTML + = msg;
//创建一个记号

createMarker (search,lat,lng);
}
// ======解码错误状态======
else
{
// ===如果我们发送请求的速度很快,再试一次,并增加延迟
if(status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
{
nextAddress--;
delay ++;
}
else
{
var reason =Code+ status;
var msg ='address =''+ search +'error ='+ reason +'(delay ='+ delay +'ms)< br>';
// document.getElementById(messages)。innerHTML + = msg;
}
}
next();
}
);
}

// =======创建标记的函数
函数createMarker(add,lat,lng)
{
var contentString = add;

if(add =='EOF')
{
stopLoadingGMap();
}

var addArray = add.split('');
var zipcode = addArray.pop();
var zipcode = add.match(/ \d {5} /)[0];

var image ='图标/ sm_02.png';
var marker = new MarkerWithLabel(
{
position:new google.maps.LatLng(lat,lng),
map:map,
icon:image,
labelContent:zipcode,
labelAnchor:new google.maps.Point(50,0),
labelClass:labels,//标签的CSS类
labelStyle:{不透明度:0.75},
zIndex:Math.round(latlng.lat()* - 100000)<5
});

google.maps.event.addListener(marker,'click',function()
{
infowindow.setContent(contentString);
infowindow.open(map ,marker);
});

bounds.extend(marker.position);
}

// =======我们想要的地理位置数组地址代码========
//使用static或动态构建
//根据需要使用尽可能多的标记 - 我已经测试了超过100
var addresses = var data = [
{'StreetAddress1 City State Zipcode'},
{ 'StreetAddress2 City State Zipcode'},
{'StreetAddress3 City State Zipcode'},
{'StreetAddress14 City State Zipcode'},
...
{'EOF'},
];

// =======全局变量提醒我们接下来要做什么
var nextAddress = 0;

// =======函数在回复回来时调用下一个Geocode操作
函数theNext()
{
if(nextAddress< ; addresses.length)
{
setTimeout('getAddress(''+ addresses [nextAddress] +',theNext)',delay);
nextAddress ++;
}
else
{
//我们完成了。显示地图边界
map.fitBounds(bounds);
}
}

// =======第一次调用该函数=======
theNext();

//此Javascript基于
提供的代码//社区教会Javascript团队
// http://www.bisphamchurch.org.uk/
// http://econym.org.uk/gmap/

//]]>
< / script>


I know that similar questions have been posted but I have not found and answer in any of them as it relates to my particular issue.

I have a javascript that uses google maps to place customer zipcodes on a map. The problem I am have is similar to what others have already posted – I get a "over query limit" error.

I have tried different setups using setTimeOut to try to send google the data within the allowable time intervals but I can’t get it to work.

Here is my action:

        function initialize() 
        {
            var rowNum      = 0 ; 
            var rowColor    = "" ;

            var latlng      = new google.maps.LatLng(27.91425, -82.842617);             

            var myOptions   = 
            {
                zoom: 7,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

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

            geocoder        = new google.maps.Geocoder();

            data.forEach(function(mapData,idx) 
            {       
                window.setTimeout(function() 
                { 
                    geocoder.geocode({ 'address': mapData.address}, function(results, status) 
                    {                   
                        if (status == google.maps.GeocoderStatus.OK) 
                        {
                            var marker = new google.maps.Marker({
                            map: map, 
                            position: results[0].geometry.location,
                            title: mapData.title,
                            icon: getIcon(mapData.type)
                        });

                        var contentHtml = "<div style='width:250px;height:90px'><strong>"+mapData.title+"</strong><br />"+mapData.address+"</div>";

                        var infowindow = new google.maps.InfoWindow({
                            content: contentHtml
                        });

                        google.maps.event.addListener(marker, 'click', function() 
                        {
                          infowindow.open(map,marker);
                        });

                        marker.locid = idx+1;
                        marker.infowindow = infowindow;
                        markers[markers.length] = marker;

                        if (idx%2 == 0)
                        {
                            rowColor = 'style="background-color:#00FFFF;"' ; 
                        }
                        else
                        {
                            rowColor = 'style="background-color:#FFFFFF;"' ; 
                        }

                        var sideHtml = '<div ' + rowColor + ' class="loc" data-locid="'+marker.locid+'"><b>'+mapData.title+'</b><br/>';
                             sideHtml += mapData.address + '</div>';
                             $("#locs").append(sideHtml); 

                        //Are we all done? Not 100% sure of this
                        if(markers.length == data.length) doFilter();
                    } 
                    else 
                    {
                        // alert("Geocode was not successful for the following reason: " + status);
                    }   
                }, 3000);                   
            });                             
        });

When I run my page using this action, I get back 11 markers even though I have many more than that in my JSON string. The window.setTimeout has absolutely no effect – I’m obviously doing something wrong here.

I would appreciate any help on this matter.

Thanks,

解决方案

I found the answer to my question. I found the following code on the Web and modified it to my needs.

With it, you can load many markers without getting Over Query Limit from Google.

I have tested it with over 100 markers and it works beautifully. The page does not freeze up at all.

I am certain some of you guys can do something much more elegant and efficient but this is a good starting point.

<script type="text/javascript">
    //<![CDATA[    

// display ani gif
    loadingGMap() ; 

 // delay between geocode requests - at the time of writing, 100 miliseconds seems to work well
      var delay = 100;

    // ====== Create map objects ======
        var infowindow = new google.maps.InfoWindow();
    var latlng = new google.maps.LatLng(27.989551,-82.462235);

    var mapOptions = 
    {
        zoom: 7,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var geo         = new google.maps.Geocoder(); 
    var map     = new google.maps.Map(document.getElementById("map"), mapOptions);
    var bounds  = new google.maps.LatLngBounds();

    // ====== Geocoding ======
    function getAddress(search, next) 
    {
        geo.geocode({address:search}, function (results,status)
        { 
            // If that was successful
            if (status == google.maps.GeocoderStatus.OK) 
            {
                // Lets assume that the first marker is the one we want
                var p   = results[0].geometry.location;
                var lat = p.lat();
                var lng = p.lng();

                // Output the data
                var msg = 'address="' + search + '" lat=' +lat+ ' lng=' +lng+ '(delay='+delay+'ms)<br>';
                //document.getElementById("messages").innerHTML += msg;
                // Create a marker

                createMarker(search,lat,lng);
            }
            // ====== Decode the error status ======
            else 
            {
                // === if we were sending the requests to fast, try this one again and increase the delay
                if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) 
                {
                    nextAddress--;
                    delay++;
                } 
                else 
                {
                    var reason  =   "Code "+status;
                    var msg     = 'address="' + search + '" error=' +reason+ '(delay='+delay+'ms)<br>';
                    // document.getElementById("messages").innerHTML += msg;
                }   
            }
            next();
        }
    );
}

// ======= Function to create a marker
function createMarker(add,lat,lng) 
{
    var contentString   = add;  

    if (add=='EOF') 
    {
        stopLoadingGMap() ; 
    }

    var addArray        = add.split(' ');       
    var zipcode         = addArray.pop();
    var zipcode         = add.match(/\d{5}/)[0] ;       

    var image           = 'icons/sm_02.png';        
    var marker          = new MarkerWithLabel(
    {
            position: new google.maps.LatLng(lat,lng),
        map: map,
        icon: image,
        labelContent: zipcode,
        labelAnchor: new google.maps.Point(50, 0),
         labelClass: "labels", // the CSS class for the label
         labelStyle: {opacity: 0.75},           
        zIndex: Math.round(latlng.lat()*-100000)<<5
    });

    google.maps.event.addListener(marker, 'click', function() 
    {
        infowindow.setContent(contentString); 
        infowindow.open(map,marker);
    });

    bounds.extend(marker.position);
}

// ======= An array of locations that we want to Geocode ========
// use static or build dynamically
// use as many markers as you need – I’ve test with over 100
var addresses = var data = [
{‘StreetAddress1 City State Zipcode’},
    {‘StreetAddress2 City State Zipcode’},
    {‘StreetAddress3 City State Zipcode’},
    {‘StreetAddress14 City State Zipcode’},
…
    {‘EOF’},
    ];

// ======= Global variable to remind us what to do next
var nextAddress = 0;

// ======= Function to call the next Geocode operation when the reply comes back
function theNext() 
{
    if (nextAddress < addresses.length) 
    {
        setTimeout('getAddress("'+addresses[nextAddress]+'",theNext)', delay);
        nextAddress++;
         } 
    else 
    {
        // We're done. Show map bounds
        map.fitBounds(bounds);
            }
}

// ======= Call that function for the first time =======
theNext();

// This Javascript is based on code provided by the
 // Community Church Javascript Team
 // http://www.bisphamchurch.org.uk/   
 // http://econym.org.uk/gmap/

    //]]>
    </script>

这篇关于谷歌地图超过查询限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆