避免具有多个标记的自定义谷歌地图的地理编码限制

发布于 2024-12-08 09:36:27 字数 233 浏览 0 评论 0原文

我创建了一个包含大约 150 个标记的自定义地图。在达到地理编码限制之前,它仅绘制大约 20 个左右。

我可以在代码中加入哪些内容以避免达到限制?

更新: 如何为每个请求添加一个时间延迟,比如 0.25 秒(或者我能做到的最低时间)?

我尝试了一些不同的建议,但我似乎无法让它们与我的代码一起使用,因此请任何示例都可以使用我的代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一片旧的回忆 2024-12-15 09:36:27

因此,我认为与其在 for 循环中几乎立即发送所有请求,不如在地理编码返回结果时发送下一个结果。如果返回的结果是 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="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>

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>
德意的啸 2024-12-15 09:36:27

我当然不是代码大师,但我没有使用超时,而是继续从函数内部调用该函数(我确信资源占用)。如果成功,我会迭代计数器,否则如果不成功,我会使用当前计数器值调用我的函数。按照这个速度,我可能会为我的 37 条折线(或标记,如果这就是您的绘图)循环 1000 次,但我相信它会尽快完成。这是一个例子 - 我首先将我的绘图函数 onclick 称为plot(0):

function plot(k)
{
...
if (status == google.maps.GeocoderStatus.OK)
        {
            draw(r.routes[0]);
            if(k<origArray.length) plot(k++);
        }
else plot(k)
...
}

i'm certainly no codemaster, but instead of using a timeout i just keep calling the function from within the function (resource hog i'm sure). i iterate through my counter if i'm successful, otherwise i call my function with the current counter value if unsuccessful. at this rate, i may loop 1000 times for my 37 polylines (or markers if that's what your drawing), but i believe it will finish at the earliest time possible. here's an example - i first call my plot function onclick as plot(0):

function plot(k)
{
...
if (status == google.maps.GeocoderStatus.OK)
        {
            draw(r.routes[0]);
            if(k<origArray.length) plot(k++);
        }
else plot(k)
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文