避免使用多个标记对自定义Google地图进行地理编码限制

避免使用多个标记对自定义Google地图进行地理编码限制

本文介绍了避免使用多个标记对自定义Google地图进行地理编码限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用大约150个标记创建了一个自定义地图。它只能在达到地理代码限制之前大约20次左右。



我可以在我的代码中加入什么来避免达到限制?



更新:
如何为每个请求添加一个时间延迟,比如说0.25秒(或者我可以放弃的最低值)?



我尝试了一些不同的建议,但似乎无法让他们使用我的代码,所以请让任何示例使用我的代码。 OVER_QUERY_LIMIT ,请重新发送请求,延长时间。我还没有找到产生最少 OVER_QUERY_LIMIT 的最佳点超时,但至少它会创建所有标记(用于有效地址)。在我的机器上完成大约需要45秒。

 <!DOCTYPE html> 
< html>
< head>
< link href =http://code.google.com/apis/maps/documentation/javascript/examples/default.css
rel =stylesheettype =text / css />
< script type =text / javascriptsrc =http://maps.googleapis.com/maps/api/js?sensor=false>< / script>
< script type =text / javascript>
var geocoder;
var map;
var infowindow = new google.maps.InfoWindow();

var places = [];
var popup_content = [/ * all your popup_content * /];
var address = [/ *所有地址* /];
var address_position = 0;

var timeout = 600;

函数initialize(){
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(52.40,-3.61);
var myOptions = {
zoom:8,
center:latlng,
mapTypeId:'roadmap'
}
map = new google.maps.Map (document.getElementById(map_canvas),myOptions);
addMarker(address_position);

$ b $函数addMarker(position)
{
geocoder.geocode({'address':address [position]},function(results,status)
{
if(status == google.maps.GeocoderStatus.OK){
places [position] = results [0] .geometry.location;

var marker = new google.maps.Marker({
position:places [position],
map:map
});

google.maps.event.addListener(marker ,'click',function(){
if(!infowindow){
infowindow = new google.maps.InfoWindow();
}
infowindow.setContent(popup_content [position ]);
infowindow.open(map,marker);
});
}
else
{
if(status == google.maps .GeocoderStatus.OVER_QUERY_LIMIT)
{
setTimeout(function(){addMarker(pos ition);},(timeout * 3));
}
}
address_position ++;
if(address_position< address.length)
{
setTimeout(function(){addMarker(address_position);},(timeout));
}
});
}
< / script>
< / head>
< body onload =initialize()>
< div id =map_canvasstyle =height:80%; top:10px; border:1px solid black;>< / div>
< / body>
< / html>


I've created a custom map with around 150 markers. It only plots around 20 or so before it hits the geocode limit.

What can I incorporate into my code to avoid hitting the limit?

UPDATE:How can I add a time delay to each request say 0.25seconds (or whatever the lowest I can get away with)?

I've tried a few different suggestions but I can't seem to get them to work with my code so please could any examples use my code.

解决方案

So instead of sending all the requests almost instantly in a for-loop, I thought it might be better that when a geocode returns a result, to send the next one. If the result coming back is a OVER_QUERY_LIMIT, resend the request with an increase on the delay. I haven't found the sweet spot timeout that produces the fewest OVER_QUERY_LIMIT's, but at least it will create all the markers (for valid addresses). It takes about 45 seconds or so to finish on my machine.

<!DOCTYPE html>
<html>
<head>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;
var map;
var infowindow = new google.maps.InfoWindow();

var places = [];
var popup_content = [ /* all your popup_content */];
var address = [/* all of your addresses */];
var address_position = 0;

var timeout = 600;

function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(52.40, -3.61);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: 'roadmap'
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    addMarker(address_position);
}

function addMarker(position)
{
    geocoder.geocode({'address': address[position]}, function(results, status)
    {
        if (status == google.maps.GeocoderStatus.OK) {
            places[position] = results[0].geometry.location;

            var marker = new google.maps.Marker({
                position: places[position],
                map: map
            });

            google.maps.event.addListener(marker, 'click', function() {
                if (!infowindow) {
                    infowindow = new google.maps.InfoWindow();
                }
                infowindow.setContent(popup_content[position]);
                infowindow.open(map, marker);
            });
        }
        else
        {
            if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
            {
                setTimeout(function() { addMarker(position); }, (timeout * 3));
            }
        }
        address_position++;
        if (address_position < address.length)
        {
            setTimeout(function() { addMarker(address_position); }, (timeout));
        }
    });
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="height: 80%; top:10px; border: 1px solid black;"></div>
</body>
</html>

这篇关于避免使用多个标记对自定义Google地图进行地理编码限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:46