且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向Google地图标记添加一个ID,然后定位它

更新时间:2022-10-17 18:51:46

使用

$ b创建标记后
$ b
  var myMarker = new google.maps.Marker({
externalURL:'http://www.google.com',
position:defaults.center,
map:map,
icon:markerImg,
id:'marker'
});

要从地图中删除它,请使用:

  myMarker.setMap(空); 

要隐藏标记,请使用:

  myMarker.setVisible(false); 

如果您以后需要大量标记才能访问,请考虑:

  var allMyMarkers = []; 
allMyMarkers.push(myMarker);

要访问特定的ID,请考虑:

休息;
}
}


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;
    }
}