不能让鼠标悬停多边形与未捕获的参考错误:谷歌没有定义
Can't get mouseover polygon to work with Uncaught Reference Error: google is not defined
新的编码和尝试建立和应用程序与谷歌地图功能利用多边形。看到这个问题问了几次,但似乎不能确定我的问题。地图初始化,多边形从定义的位置创建。我得到一个"未捕获的参考错误:谷歌没有定义"时,试图利用事件监听器,我用它来尝试改变多边形的样式时,它是悬停。
var map;
// Map Display options
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 9,
center: {lat: 42.05, lng: -70.25},
mapTypeId: google.maps.MapTypeId.SATELLITE
});
// Define the LatLng coordinates for the polygon.
var cc_peaked_hill = [
{lat: 42.049803, lng: -69.970551},
{lat: 42.048273, lng: -69.978790},
{lat: 42.043684, lng: -70.046082},
{lat: 42.043684, lng: -70.058441},
{lat: 42.056940, lng: -70.085907},
{lat: 42.070194, lng: -70.118179},
{lat: 42.079369, lng: -70.156631},
{lat: 42.049803, lng: -69.970551}
];
// Construct the polygon.
var cc_peaked_hill_billsPollygon = new google.maps.Polygon({
paths: cc_peaked_hill,
strokeColor: '#F7F8FF',
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: '#4562A8',
fillOpacity: 0.45,
editable: false
});
// Build the Polygons
polygons = cc_peaked_hill_billsPollygon.setMap(map);
//Listen for when the mouse hovers over the polygon.
google.maps.event.addListener(polygons, 'mouseover', function (event) {
// Within the event listener, "this" refers to the polygon which
// received the event.
this.setOptions({
strokeColor: '#00ff00',
fillColor: '#00ff00'
});
});
}
由于某些原因,我得到以下错误:
Uncaught Reference Error: google is not defined
如何解决这个问题?
问题是你首先这样做:
polygons = cc_peaked_hill_billsPollygon.setMap(map);
这实际上只是在多边形上设置map
属性(实际上setMap
不返回任何东西)。它没有给你一个多边形。在cc_peaked_hill_billsPollygon
变量中已经有了。
所以当你尝试设置事件监听器时,就使用它。
事实上,你甚至不需要调用setMap
。只需在创建多边形时分配map属性。
// Construct the polygon.
var cc_peaked_hill_billsPollygon = new google.maps.Polygon({
paths: cc_peaked_hill,
strokeColor: '#F7F8FF',
strokeOpacity: 0.8,
strokeWeight: 1,
fillColor: '#4562A8',
fillOpacity: 0.45,
editable: false,
map: map // Does the equivalent of `setMap(map)`
});
//Listen for when the mouse hovers over the polygon.
google.maps.event.addListener(cc_peaked_hill_billsPollygon, 'mouseover', function (event) {
// Within the event listener, "this" refers to the polygon which
// received the event.
this.setOptions({
strokeColor: '#00ff00',
fillColor: '#00ff00'
});
});
PS:还有一种更简洁的方式可以添加事件侦听器。只做:
cc_peaked_hill_billsPollygon.addListener('mouseover', function (event) {
// Within the event listener, "this" refers to the polygon which
// received the event.
this.setOptions({
strokeColor: '#00ff00',
fillColor: '#00ff00'
});
});
相关文章:
- 相位器状态未捕获参考错误
- 尝试从 Jasmine 访问主干模型时出现参考错误
- 参考酶导入错误
- 全新车把安装会在首次运行时产生参考错误
- Dart 中的参考错误:按下按钮时未定义功能
- 参考错误:结帐页面上未定义付款
- 学习聚合物,获取参考错误:聚合物未定义
- 参考错误onnotification没有在推送通知cordova android中定义
- 未捕获的参考错误:谷歌没有定义谷歌图表
- 使用JS变量对PHP数组进行索引(预加载):;未捕获参考错误”;即使函数不是't调用
- 回波日期的参考错误
- 奇怪的错误:懦弱地拒绝包装带有圆形参考的物体
- 参考错误地理位置
- 参考错误:谷歌不是使用 ng-map GeoCoder 服务定义的
- 相位器未捕获参考错误:游戏未定义
- 未捕获的参考错误:stLight未定义(仅在Chrome中)
- 错误url对于动态注入的javascript函数是没有用的.需要用户提供参考
- 参考错误:Can't find variable: bootbox
- 参考错误:Can't find variable: dataKeys
- Chrome扩展-调用函数在标签上定义(参考错误)