问题描述
好的,我有一张带有一些标记的地图。在地图上有一个点击事件应该得到最接近的标记,但是当find_closest_marker(event)函数运行时,它会在map.markers.length上中断它的未定义。这里是一个链接到什么即时尝试 http://jsfiddle.net/VyzFq/7/
任何人都可以请显示我在哪里出错了。
看起来有几个问题:
map.markers数组尚未被声明。
...
var infowindow =在$循环中将其标记到标记数组上。 new google.maps.InfoWindow();
var marker,i;
map.markers = []; //添加此行
(i = 0; i< locations.length; i ++){
marker = new google.maps.Marker({
position:new google.maps。 LatLng(位置[i] [1],位置[i] [2]),
map:map
});
map.markers.push(marker); //添加此行
...
find_closest_point函数看不到地图变量。将find_closest_point函数移动到要添加click事件侦听器的作用域:
//更改此行
google .maps.event.addListener(地图,'click',find_closest_marker);
// TO:
google.maps.event.addListener(map,'click',function(event){
var lat = event.latLng。 lat();
var lng = event.latLng.lng();
var R = 6371;
var distance = [];
var closest = -1;
for(i = 0; i< map.markers.length; i ++){
var mlat = map.markers [i] .position.lat();
var mlng = map.markers [ i] .position.lng();
var dLat = rad(mlat - lat);
var dLong = rad(mlng - lng);
var a = Math.sin(dLat / 2)* Math.sin(dLat / 2)+
Math.cos(rad(lat))* Math.cos(rad(lat))* Math.sin(dLong / 2)* Math.sin(dLong / 2);
var c = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
var d = R * c;
距离[i] = d;
如果(最接近== -1 || d<距离[最接近]){
最接近= i;
}
}
alert(map.markers [closest] .title);
});
Ok so i have a map with some markers on it. There is a click event on the map that should get the closest marker however when the find_closest_marker(event) function runs it breaks on the map.markers.length saying that its undefined.
Here is a link to what im trying to http://jsfiddle.net/VyzFq/7/
Can anyone can please show me where I'm going wrong here.
Looks like there are a couple of problems:
1) the map.markers array hasn't been declared. declare it above the for loop, and push the markers onto the markers array.
...
var infowindow = new google.maps.InfoWindow();
var marker, i;
map.markers = []; // ADD THIS LINE
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
map.markers.push(marker); // ADD THIS LINE
...
2) the find_closest_point function can't see the map variable. move the find_closest_point function to the scope where you are adding the click event listener:
// CHANGE THIS LINE
google.maps.event.addListener(map, 'click', find_closest_marker);
// TO:
google.maps.event.addListener(map, 'click', function( event ) {
var lat = event.latLng.lat();
var lng = event.latLng.lng();
var R = 6371;
var distances = [];
var closest = -1;
for( i=0;i<map.markers.length; i++ ) {
var mlat = map.markers[i].position.lat();
var mlng = map.markers[i].position.lng();
var dLat = rad(mlat - lat);
var dLong = rad(mlng - lng);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(rad(lat)) * Math.cos(rad(lat)) * Math.sin(dLong/2) * Math.sin(dLong/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
distances[i] = d;
if ( closest == -1 || d < distances[closest] ) {
closest = i;
}
}
alert(map.markers[closest].title);
});
这篇关于map.markers未定义Google Map API v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!