我正在尝试在Gmaps中创建地理围栏,然后将多个标记加载到地图上,但在此过程中,我使用checkGeofence()
检查标记是否在地理围栏的边界内。如果是,那么我将它添加到地图中,如果不是,我想忽略它,只显示标记不在边界内的消息。
问题是当我尝试使用以下代码执行此操作时:
var poi_coordinates = [[32.27, 77.19],[32.25, 77.22] , [32.23, 77.21], [32.24, 77.17],[26.61,77.19]]
var lattitude = 32.25; //28.6339;
var longitude = 77.19; //77.2200;
//creating map
var map = new GMaps({
div: '#map',
lat: 32.25,
lng: 77.19,
zoom:10
});
//creating geofence
circle = map.drawCircle({
lat: lattitude,
lng: longitude,
strokeColor: '#fff',
strokeOpacity: 1,
strokeWeight: 3,
fillColor: 'rgba(75,255,0,0.2)',
fillOpacity: 0.4,
radius: 4000,
editable: true,
});
//Plotting Main Coordinates
map.addMarker({
lat: lattitude,
lng: longitude,
draggable: true,
fences: [circle],
outside: function (marker, fence) {
alert('This marker has been moved outside of its fence');
}
});
//Plotting extra coordinates
for (var a = 0; a < poi_coordinates.length; a++) {
if (map.checkGeofence(poi_coordinates[a][0], poi_coordinates[a][1], circle)) {
//Code work properly as it should if I add this alert line
//alert(map.checkGeofence(poi_coordinates[a][0], poi_coordinates[a][1], circle))
map.addMarker({
lat: poi_coordinates[a][0],
lng: poi_coordinates[a][1],
draggable: true,
fences: [circle],
outside: function (marker, fence) {
alert('This marker has been moved outside of its fence');
}
});
}
else {
alert(poi_coordinates[a][0] + "," + poi_coordinates[a][1] + ' is out of the 4 Km fence')
}
}
只有在我在地图上添加标记时添加了alert
命令才有效。如果我不在那里添加alert
命令,我不知道究竟发生了什么,但所有标记都会添加到地图中。
请帮忙。以下是jsfiddle上的代码:http://jsfiddle.net/e52792j5/16/