Google Maps API v3: Does placemarker exist on given LatLng?
I want to find out using the API if a marker has been placed开发者_高级运维 on google maps at a given LatLng
. Is this possible without me saving all the latlngs at which the placemarker is placed?
No. The API doesn't track your markers, so you will need to track them yourself. I suggest adding each location to an array, and then writing a function which can check whether any marker in that array is located at a specific lat/lng.
Something like:
var marker_locations = [];
function addMarker(latlng) {
marker = new google.maps.Marker(....);
marker.setMap(map);
marker_locations.push(latlng);
}
function markerAtPoint(latlng) {
for (var i = 0; i < marker_locations.length; ++i) {
if (marker_locations[i].equals(latlng)) return true;
}
return false;
}
Try using map.getBounds().contains(marker.getPosition())
and replace getPosition()
with your latlng. I haven't tested this.
精彩评论