首页 > 解决方案 > 谷歌地图地理编码标记加载

问题描述

我有一个带有自定义帖子类型的 WP 和 +150 个使用自定义字段的完整地址的帖子。

我正在使用带有地理编码的 Google API 在地图上显示这些帖子,这显示在模板 file.php 上。它完美地工作:

$args = array( ... );
$fiches = get_posts($args);
foreach ($fiches as $post) {
    $array_adresse = array('ADRESSE1','ADRESSE2','ADRESSE3','CODE_POSTAL','VILLE');
    [...]
    codeAddress("<?php echo addslashes($adresse_membre); ?>","<?php echo $titre; ?>","<?php echo $cat_name; ?>","<?php echo $post->guid; ?>","<?php echo $adresse_desc; ?>","<?php echo $icon_at; ?>","<?php echo $carre_color; ?>");

    function codeAddress(address,title,cat_name,guid,adress_desc,icon_at,color) {
        geocoder.geocode( { "address": address},function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location,
                    title: title,
                    icon: icon_at
                });
                var markerCluster = new MarkerClusterer(map, marker,
        {imagePath: 'https://raw.githubusercontent.com/googlemaps/js-marker-clusterer/gh-pages/images/m1.png'});
                var myWindowOptions = {
                    content: "<h5 style=\"margin: 0 0 5px 0;font-size: 16px;\"><a href=\""+guid+"\" title=\"Voir cette fiche\" target=\"_blank\">"+title+"</a></h5><p style=\"font-size: 12px;\">Catégorie : "+cat_name+" <span style=\"display:inline-block;vertical-align: middle;background-color:"+color+";width: 10px;height: 10px;\">&nbsp;</span></p><p>"+adress_desc+"</p><a style=\"float: right;color: blue;font-size:12px;\" href=\""+guid+"\" target=\"_blank\">>> Voir la fiche</a>"
                };
                var myInfoWindow = new google.maps.InfoWindow(myWindowOptions);
                google.maps.event.addListener(marker, "click", function() {
                    myInfoWindow.open(map,marker);
                });             
            } 
            else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                setTimeout(function() {
                   codeAddress(address,title,cat_name,guid,adress_desc,icon_at,color);
                }, 0);
            } 
            else {
                //alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
<?php } ?>
}

但是我有一个问题,我的标记是一个一个加载的。有人知道是否存在问题:

- 同时加载所有标记?

- 使用缓存加载更快?

- 或者只是加载更快?

标签: google-mapsgoogle-apigoogle-geocoding-api

解决方案


听起来您的应用程序经常遇到 OVER_QUERY_LIMIT 错误,以至于它总是必须在每个标记之间等待(setTimeout)。

首先要检查的是您是否使用了 API 密钥,因为如果使用了,您不应该经常遇到 OVER_QUERY_LIMIT 错误。有关如何开始使用 API 密钥的一些提示,请参阅 https://issuetracker.google.com/116675310#comment2

如果即使使用 API 密钥,OVER_QUERY_LIMIT 错误仍然比您想要的更频繁,您可以使用批处理作业中的 Geocoding API Web 服务每天对地址进行一次地理编码(以保持结果新鲜),然后显示所有标记一旦使用那些缓存的结果。


推荐阅读