【问题标题】:Avoid geocode limit on custom google map with multiple markers避免对具有多个标记的自定义谷歌地图的地理编码限制
【发布时间】:2011-10-04 14:06:09
【问题描述】:

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

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

更新: 如何为每个请求添加一个时间延迟,比如 0.25 秒(或任何我能逃脱的最低限度)?

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

【问题讨论】:

  • 您也可以考虑这个答案:stackoverflow.com/questions/2419219/…,它建议您将它们全部获取并将它们的 lat/lng 存储在数据库(或数组)中,这样您就可以在不进行反向查找的情况下创建标记。

标签: google-maps google-maps-api-3 gis google-maps-markers


【解决方案1】:

因此,与其在 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>

【讨论】:

  • 感谢您的回答,是否可以将其合并到我的代码中?我不太确定该怎么做。
  • 我使用你的代码作为我的代码的基础,但是我看不到你的代码示例了。
  • 我偶然发现了这个,它出色地解决了我的问题!但是,我有一个问题要问您,Jack:您是否有理由将 address_position 增量和对 addMarker 的调用放在状态检查之外?在我看来,您可能希望将其放在“OK”块内,因为您只想再次安排它并在您确认地理编码返回时增加一个成功的地址位置。是否有理由将其完全排除在状态检查之外?我在想象一个请求超过限制的情况,而且似乎位置无论如何都会增加。我错了吗?
  • 至少在 Chrome 中,“OK”块内的增量会挂在第二个标记之后。我不知道为什么会这样,所以我把它移了出来,它解决了问题。
  • @JackBNimble 这是您的终极解决方案吗?你找到更好的超时了吗?
【解决方案2】:

我当然不是代码管理员,但我没有使用超时,而是继续从函数内部调用函数(我敢肯定是资源消耗)。如果成功,我会遍历我的计数器,否则如果不成功,我会使用当前计数器值调用我的函数。以这个速度,我可能会为我的 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)
...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多