我正致力于
的功能到目前为止的成就
- 搜索位置和点标记
- 在标记周围绘制圆圈
- 如果有所需位置“(lat,lng)”则通过绘图标记显示它们 在谷歌地图上使用gmaps.js。
醇>
- 我能够在地图上指出所需的标记
- 围绕该标记绘制圆圈
醇>
问题是
- 启动其他搜索后,它会绘制标记和圆圈并删除现有标记,但不会移除圆圈,只会绘制其他圆圈。
醇>
这是片段 非常感谢你
/* Map Object */
var mapObj = new GMaps({
el: '#map',
lat: 9.0217624,
lng: 38.7525155,
zoom:14.5
});
//show searched location and point on map with marker
$('#geocoding_form').submit(function(e){
e.preventDefault();
GMaps.geocode({
address: $('#address').val().trim(),
callback: function(results, status){
if(status=='OK'){
//window.location.reload();
//clean the map from existing markers first
mapObj.removeMarkers();
//searched location
var latlng = results[0].geometry.location;
newLat = latlng.lat();
newLng = latlng.lng();
//draw circle
center1 = mapObj.setCenter(newLat, newLng);
circle = mapObj.drawCircle({
lat:newLat,
lng:newLng,
clickable: false,
radius:1000, // metres
strokeColor: "red",
strokeOpacity:0.8,
strokeWeight: 2,
fillColor: "blue"
});
//get circle area
var circleArea = circle.getBounds();
//check if circle contains markers
var containsMarker = circleArea.contains({
lat: pharmacyLat,
lng: pharmacyLng
});
//if marker is within the circle
if(containsMarker){
//mapObj2.removeMarkers();
//we needed another object to show new data on map
var mapObj2 = new GMaps({
el: '#map',
lat: 9.0217624,
lng: 38.7525155,
zoom:14.5
});
//remove existing markers of mapObj
mapObj.removeMarkers();
//NOTE: nothing will be visible on map if uncommented
//remove existing circles of mapObj
//circle = mapObj.setMap(null);
//draw user location
drawNewUserLocation(mapObj2, newLat, newLng);
//new circle
drawNewCircle(mapObj2, newLat, newLng);
//show pharmacies in that area
var pharmacy1 = mapObj2.addMarker({
lat: pharmacyLat,
lng: pharmacyLng,
title: 'place',
infoWindow: {
content: '<h4>place</h4><div>Addis Ababa, Ethiopia</div>',
maxWidth: 100
}
});
//add few more pharmacies
//...
}
else if(!containsMarker){
alert("oops!");
//remove existing markers of mapObj2
mapObj2.removeMarkers();
//draw user's location
drawUserLocation(newLat, newLng);
}
}
}
});
});