I have an array "markerGroups" defined like this:
var markerGroups = {"foo": [], "bar": [], "foobar": []};
This array is used for storing all the markers in a Google Map. As you can see, I have three categories where I store the markers. One marker will only exist in one category, and will never exist in any other category.
Each marker is also identyfied by a unique ID, for instance the ID can be 1422.
Now I want to add the markers to this group in such a way that i can reach it by doing for instance
console.log(markerGroups.foo[1422]);
Even if that's the only marker in that category. I must also be able to remove it completly from the category.
I have tried to make this possible by defining markerGroups like this:
var markerGroups = {"foo": {}, "bar": {}, "foobar": {}};
This line is in a function where the marker is defined as marker, and category is passed into the function, telling the function where to store the marker.
markerGroups[category][marker.ID] = marker;
This works excellent, except the fact that markers now won't show up in my map.
Any ideas why or how this could be done in a better way?
Markers are simple JS objects that can be manipulated in the same way that any JS object can.
The only way you can control if a Marker is shown on the map is via it's setMap() function. So check out your code and see if you call setMap(your_map) for the markers you want to be shown on the map. Also bear in mind that there are some properties that must be set (position for example) in order the Marker to be shown.
So, don't worry of simple JS object manipulation, it doesn't interact with the Marker visibility, as long as it's initialized properly.
Of course, this is a theoretical point of view, if you've done all this and it still doesn't work, you must provide some additional details and code.
Related
As a newbie to ArcGIS and Javascript as a whole, I might be going about this problem the wrong way, especially seeing the samples given. However, I will still try to see if my ideal method is eligible.
In my ArcGIS file, I would like to individually isolate the entries of a FeatureLayer and modify them, mid-runtime. In this case, I would want to hide certain entries, based on their content.
function changeVisibility ()
{
var checkBox = document.getElementById("expLayer");
for (var i = 0; i < featureLayer.length; i++)
{
featureLayer[i].visible = checkBox.checked;
}
}
The above code is incorrect as featureLayer, which is my "FeatureLayer" type, is not an array. I'm currently searching for a method to gather the individual values of featureLayer.
The sample programs that the ESRI website gives has all the separate entries isolated into different layer classes (lake entries are separated from city entries), and then choosing to make the whole layer visible or invisible. Its making me think that isolating entries of a layer isn't possible. Even if it is possible, I'm unsure if each layer entry would have their own "visible" flag.
Is it possible to isolate the individual values of a layer class, and if so, does it take the form of an array?
I realize hashes have unique keys already, but my question is aimed at a particular use case (that I feel may be common) regarding hashes.
I have a google map (v3) on my website. I load a bunch of locations from my database, and throw markers on the map at each location. I had an issue in my application where I was seeing duplicate markers for every location. I decided to add a check at the top of my add marker function, to use a hash of previously placed markers (stored by location).
markers = {};
function addMarker(latlng) {
if (!markers.hasOwnProperty(latlng)) {
markers[latlng] = true;
} else {
return;
}
// do other stuff
}
This works, but it feels hacky. Adding the unused boolean value to each hash key/pair just doesn't feel right, because it's technically not used... I just need the key half.
Is there another approach to accomplish the same idea?
layers.group3 = new L.MarkerClusterGroup();
layers.group3.clearLayers();
map.addLayer(layers.group3);
Also i first need to check if the layer already exists when i call the method again to add/remove markers it seems the hasLayer is not working
like for example "map.addLayer(markers, "group1markers")" ????
No: Leaflet doesn't support naming layers in that way, regardless of what plugin you use: the key is using variables correctly. You can keep your own index of layers, though, like:
var layers = {};
layers.group1markers = group1markers;
map.addLayer(markers);
Likewise, hasLayer, as in the Leaflet documentation takes a layer object as an argument, not a variable name as a string.
I'm attempting to create a dynamic map using the latest Google Map API. Everything is going smooth so far. I do have a question of course:
How would I/you go about saving my current map?
Let's say you build a map with dynamic markers: Since everything is done via JavaScript, I need to get/set those values from a file/database.
I was thinking about outputting the entire google.map.Markers as a JSON and send it to a database as a string, but then if I have around 100 places, I'm not sure how well it would go and I'm worried about efficiency.
Is this the only way to do it and am I thinking properly? Basically your website users are allowed to place a marker on the map, which then are subject to confirmation of course. Once it's confirmed, that marker must be in the latest "version" of the map, so I must get that info from/into a database/file.
Thanks in advance!
I'm creating similar application and I implemented it this way:
I have a Marker table which has columns for each attribute that I'm using in markers (e.g. latitude, longitude, name, description, type, etc.) When someone adds a marker I'm just saving the attributes of the marker to the database. Next time I want to show the marker I'm just getting the attributes from database, encode them to JSON and attach to the page. Inside page I have the JS that grabs those attributes and creates the markers inside the map. Pseudocode:
//this is generated dynamically from DB.
var markers = [
{lat:115416,lng:26411},
{lat:115416,lng:26411}
];
//this is static, just grabs the dynamic bit and puts it on the map:
for (var i = 0; i < markers.length; i++){
//creates a marker object
var marker = new Marker({lat:markers[i].lat, lng:markers[i].lng })
//displays it on the map
map.addMarker(marker);
}
the benefit of this is that your data in the database is independent from map implementation, e.g. if in the future you decide to move to apple maps and it has different implementation you can just write different JS to handle the data. Also you can query over it, e.g. you can query for places that are close by looking at lat and lang, etc..
I'm using Google Maps v3 and I build in the server side a json collection of location data. So when I receive that, I iterate through that collection and create a marker object for each and every one of them.
For each of these markers, I bind them with the click event, so when the user clicks on a marker in the map, it displays an infoWindow with data related to the associated location.
I have an array to store all the location got from the server that holds the retrieved json objects and their corresponding marker.
The problem is that, even when I have an array that I can reference or iterate through, I need to not only get the marker object when the user clicks on them, but also the location object, that stores more information.
I tried to mantain this array of objects and reference it from the calling object without success, because the function is called by the marker and not the location object. So I thought if it's possible to store more information in the google maps marker object like using a general purpose field.
Please let me know if more information is needed.
Thanks!
Yes you can, thanks to JavaScript. In this language, objects and hashtables are the same thing1.
This is how you are creating your marker:
var point = new google.maps.LatLng(40.70, -74.00);
var myMarker = new google.maps.Marker({ position: point, map: map });
And this is how you can add an additional property to your myMarker object:
myMarker.myNewField = 100;
Voila! No need to hold separate arrays of related data. No need of a general purpose field. Simply invent a new property name, and you're good to go.
1 A Survey of the JavaScript Programming Language by Douglas Crockford.