I'm using JavaScript Mapping Library - OpenLayer to create a markers overlay. I want to control the markers dynamically: add new ones and remove existing markers from the layer.
the way to add a new marker to the layer is by the command:
markers.addMarker(new OpenLayers.Marker(new OpenLay开发者_运维技巧ers.LonLat(0,0),icon));
as you can see, the initialization parameters contain only coordinates and icon image, not id, even not as an optional parameter.
in order to control the markers I want to create 2 dimensional array, that contain markers array by reference and ID array.
then, when I want to remove a marker from the layer, the command will be simply:
markers.removeMarker(ArrayMarkers[i]);
How do I push an element to JavaScript array by reference?
How can I run on ArrayMarkers elements by reference?
Try:
var ArrayMarkers = [];
var myMarker = new OpenLayers.Marker(new OpenLayers.LonLat(0,0),icon);
markers.addMarker(myMarker);
ArrayMarkers.push(myMarker);
Basically, JavasScript generally uses references to pass objects around. You're already passing a reference in to the library. You can use the same technique.
In JavaScript, you can't chose to push an variable by value of by reference... this is done automatically depending on the type of the variable.
I guess your markers are Objects. So they will be pushed in the array by reference. The ids are String, they will be pushed by value.
精彩评论