I've created a google map using the Google Maps API v3 and have added a custom marker, I have added an ID to the Marker constructor which I am hoping to target with additional JS using jQuery, at the moment however when I simply try something like $( '#'+marker.id ).hide(); nothing happens?
Could anyone advise me on how to properly access this marker id?
My maker code is as follows:
marker = new google.maps.Marker({
externalURL: 'http://www.google.com',
position: defaults.center,
map: map,
icon: markerImg,
id: 'marker'
});
and then I use the following code to create a jQuery object to target:
var mapMarker = $( '#'+marker.id );
mapMarker.hide();
After creating a marker with
var myMarker = new google.maps.Marker({
externalURL: 'http://www.google.com',
position: defaults.center,
map: map,
icon: markerImg,
id: 'marker'
});
To remove it from the map, use:
myMarker.setMap(null);
To hide the marker marker from view, use:
myMarker.setVisible(false);
If you need to have a lot of markers to access later, consider:
var allMyMarkers = [];
allMyMarkers.push( myMarker );
To access a specific id, consider:
for(var i=0;i<allMyMarkers.length;i++){
if(allMyMarkers[i].id === "marker"){
allMyMarker[i].setMap(null);
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With