Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an ID to Google Map Marker then targeting it [duplicate]

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();
like image 859
styler Avatar asked Aug 08 '13 14:08

styler


1 Answers

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;
    }
}
like image 78
Xion Dark Avatar answered Sep 24 '22 13:09

Xion Dark