【问题标题】:Google Map API adding more then 10 markersGoogle Map API 添加超过 10 个标记
【发布时间】:2011-12-15 19:00:49
【问题描述】:

我正在尝试使用以下代码在谷歌地图上添加批量标记。

var map = null;
var geocoder = null;
var bounds = null;
var gdir;

var properties_address = new Array();
var blueIcon = new GIcon(G_DEFAULT_ICON);
blueIcon.iconSize = new GSize(26, 33);
markerOptions = { icon:blueIcon };
blueIcon.image = "https://mysite/images/marker.gif";

properties_address = ["Abohar,Punjab,india", "Achhalda,Uttar Pradesh,india", "Achhnera,Uttar Pradesh,india", "Adari,Uttar Pradesh,india", "Adilabad,Andhra Pradesh,india", "Adipur,Gujarat,india", "Adoni,Andhra Pradesh,india", "Adoor,Kerala,india", "Agartala,Tripura,india", "Agra,Uttar Pradesh,india", "Ahmedabad,Gujarat,india", "Ahmedgarh,Punjab,india", "Ahmednagar,Maharashtra,india", "Aizawl,Mizoram,india", "Ajmer,Rajasthan,india", "Akaltara,Chhattisgarh,india", "Akola,Maharashtra,india", "Alappuzha,Kerala,india"];

$(document).ready(function() {
    bounds = new GLatLngBounds;
    initialize();
 });

 function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(21.88, 78.442626), 5);
        geocoder = new GClientGeocoder();
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());

        $.each(properties_address, function(index, property) {
          showAddressByAddress(index, property, false);
        });                        
    }
}

function showAddressByAddress(index,address, is_custom)
{
    if(address){
        geocoder.getLatLng(address,function(point) {
            var marker = new GMarker(point,markerOptions);
            map.addOverlay(marker);
            bounds.extend(marker.getPoint());
            map.setZoom(map.getBoundsZoomLevel(bounds));
            map.setCenter(bounds.getCenter());

            GEvent.addListener(marker, "mouseover", function() {
                if(!is_custom){
                    address_html = address + '<br /><a id="route_'+index+'" href="#" class="route_url" data-address="'+ address +'">Route from here</a>';
                }else{
                    address_html = address;
                }

                marker.openInfoWindowHtml(address_html);
            });               

        });
    }
}

代码工作正常,直到我在 properties_address 数组中提供 10 个地址,但超过 10 个地址给我以下错误。

a 为空 http://maps.gstatic.com/intl/en_ALL/mapfiles/377a/maps2.api/main.js 1131号线

【问题讨论】:

  • 上面的示例代码是main.js吗? a在哪里?
  • 'a is null' 是当您自己的代码中出现 JS 错误时,您将从 Google Maps javascript 中得到的典型错误。
  • 你能告诉我们第11个数组项的内容吗?
  • 您应该考虑迁移到 Maps API 的第 3 版,这是第 2 版的代码。 code.google.com/apis/maps/documentation/javascript/basics.html
  • ["Abohar,Punjab,india", "Achhalda,Uttar Pradesh,india", "Achhnera,Uttar Pradesh,india", "Adari,Uttar Pradesh,india", "Adilabad,Andhra Pradesh,印度”,“阿迪普尔,古吉拉特邦,印度”,“阿多尼,安得拉邦,印度”,“阿多尔,喀拉拉邦,印度”,“阿加塔拉,特里普拉,印度”,“阿格拉,北方邦,印度”,“艾哈迈达巴德,古吉拉特邦,印度”,“艾哈迈德加尔,旁遮普邦,印度”,“艾哈迈德纳加尔,马哈拉施特拉邦,印度”,“艾藻尔,米佐拉姆邦,印度”,“阿杰梅尔,拉贾斯坦邦,印度”,“阿卡尔塔拉,恰蒂斯加尔邦,印度”,“阿科拉,马哈拉施特拉邦,印度” , "阿拉普扎,喀拉拉邦,印度"]

标签: javascript google-maps


【解决方案1】:

您是否尝试过使用 firebug 来查看数组的属性,听起来好像其中的一项为 null。

<html>
<script type="text/javascript">
var myCars=["Saab","Volvo","BMW"];;
console.log(myCars);    
</script>
</html>`

然后在 firebug 控制台窗口中查看数组的属性

【讨论】:

    【解决方案2】:

    数组名错误:

    var properties_add21ress = new Array();
    

    【讨论】:

    • 发布问题时只是拼写错误,但这不是问题。感谢您的回复。
    【解决方案3】:

    它是固定的,我只是跳过了地理编码找不到的点。

    function showAddressByAddress(index,address, is_custom)
    {
        if(address){
            geocoder.getLatLng(address,function(point) {
                if(point){
                    var marker = new GMarker(point,markerOptions);
    
                    map.addOverlay(marker);
                    bounds.extend(marker.getPoint());
                    map.setZoom(map.getBoundsZoomLevel(bounds));
                    map.setCenter(bounds.getCenter());
    
    
                    GEvent.addListener(marker, "mouseover", function() {
                        if(!is_custom){
                            address_html = address + '<br /><a id="route_'+index+'" href="#" class="route_url" data-address="'+ address +'">Route from here</a>';
                        }else{
                            address_html = address;
                        }
    
                        marker.openInfoWindowHtml(address_html);
                    });
    
                }
            });
        }
    

    【讨论】:

      猜你喜欢
      • 2012-04-14
      • 1970-01-01
      • 2017-05-18
      • 2013-06-14
      • 2017-12-16
      • 2010-12-20
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多