javascript – “无法读取未定义的”Google地图“的属性”zindex“

前端之家收集整理的这篇文章主要介绍了javascript – “无法读取未定义的”Google地图“的属性”zindex“前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用API​​向Google地图添加自定义控件.我已经添加了两个自定义控件,他们工作正常.我试图复制并粘贴第三个控件的代码(当然更改相关的变量),并且我不断得到上面的错误(在标题中).

Chrome控制台和Firebug似乎没有指出一个特定的问题(它打破了google maps api代码).通过逐渐评论,我已经把它缩小到这一行:

map.controls[google.maps.ControlPosition.TOP_RIGHT].push(churchControlDiv);

添加控件的完整代码如下:

function ChurchControl(churchControlDiv,map) {
churchControlDiv.style.padding = '5px 0px';
var churchControlUI = document.createElement('DIV');
churchControlUI.style.backgroundColor = 'white';
churchControlUI.style.borderStyle = 'solid';
churchControlUI.style.borderWidth = '1px';
churchControlUI.style.borderColor = 'gray';
churchControlUI.style.BoxShadow = 'rgba(0,0.398438) 0px 2px 4px';
churchControlUI.style.cursor = 'pointer';
churchControlUI.style.textAlign = 'center';
churchControlUI.title = 'Click to see Churches';
churchControlDiv.appendChild(churchControlUI);
var churchControlText = document.createElement('DIV');
churchControlText.style.fontFamily = 'Arial,sans-serif';
churchControlText.style.fontSize = '13px';
churchControlText.style.padding = '1px 6px';
churchControlText.style.fontWeight = 'bold';
churchControlText.innerHTML = 'Churches<br>แสดงจำนวนคริสเตียน';
churchControlUI.appendChild(churchControlText);

google.maps.event.addDomListener(churchControlUI,'click',function() {
    toggle(churches);
    if (churchControlText.style.fontWeight == 'bold') {
        churchControlText.style.fontWeight = 'normal';
    } else {
        churchControlText.style.fontWeight = 'bold';
    }
});

google.maps.event.addDomListener(churchControlUI,'mouSEOver',function() {
    churchControlUI.style.backgroundColor = '#e8e8e8';
});

google.maps.event.addDomListener(churchControlUI,'mouSEOut',function() {
    churchControlUI.style.backgroundColor = 'white';
});
}

function initialize(){
    map = new google.maps.Map(document.getElementById("map_canvas"),{
    center: centerLatLng,zoom: 7,streetViewControl: false,mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var churchControlDiv = document.createElement('DIV');
    var churchControlDiv = new ChurchControl(churchControlDiv,map);
    churchControlDiv.index = 3;
    map.controls[google.maps.ControlPosition.TOP_RIGHT].push(churchControlDiv);
}

有任何想法吗?有3个控件的任何原因都是一个问题?

解决方法

我跟着 the tutorial,这是非常接近你的代码.

这条接近终点的线路需要改变

var churchControlDiv = new ChurchControl(churchControlDiv,map);

将churchControlDiv替换为churchControl或其他名称,因为churchControlDiv不应被覆盖.

见这里http://jsfiddle.net/FTjnE/2/

标记了我的更改//更改了点击警报和新的地图中心

原文链接:https://www.f2er.com/js/152627.html

猜你在找的JavaScript相关文章