So I am creating a map using Leaflet and Mapbox with multiple overlays and base maps. Currently I have a layers control where these are separated and you can only show one base map at a time, but all the overlays can be shown at once.
However, I have two overlays which interfere with each other and I would like to make it so that if one is checked in the control, the other is unchecked and vice versa. Is there any way to do this?
I am anticipating something like this, but am open to any suggestions.
var layer1 = L.mapbox.tileLayer('mapid1');
var layer2 = L.mapbox.tileLayer('mapid2');
var layerControl = L.control.layers(baseMaps, {"L1": layer1, "L2": layer2}).addTo(map);
map.on("overlayadd", function(e) {
if (e.name === "L1"){
L.layers.control.uncheck(layer2);
} else if (e.name === "L2") {
L.layers.control.uncheck(layer1);
};
});
Thank you!!
Working fiddle.
I don't know if there's an easier way. It seems the library doesn't offer many options to manipulate the L.control.layers overlays.
What I'm doing is firing a click on the conflicting overlay. To achieve this, a little DOM manipulating is needed. You can store an id reference when creating overlay's label.
Related
I have GeoJSON features that consists of many little features. When I hover over one of them, I want the whole layer to be selected and not only a part of my layer.
I don't know where to start to achieve that... anyone can help me ?
This is my code for the moment:
var hoverClick = new ol.interaction.Select({
condition: ol.events.condition.pointerMove,
});
select = hoverClick;
olMap.addInteraction(select);
Thanks
Instead of using an ol.interaction.Select, you could listen to the map pointermove event and use the ol.Map#forEachFeatureAtPixel method. If there's a feature (from your layer) at the location of the pointer, then apply the wanted style to the layer.
I am adding multiple layers with various opacities to my leaflet map object like this:
var TopoLayer = L.esri.dynamicMapLayer("https://gis.in.gov/arcgis/rest/services/Imagery_Basemap/MapServer", {
opacity: 0.8,
// Tell the map to use a loading control
useCors: false
}).addTo(map);
var EPSLayer = L.esri.dynamicMapLayer("https://gis.in.gov/arcgis/rest/services/DOT/EPS_Map_2015/MapServer", {
opacity: 1,
// Tell the map to use a loading control
useCors: false
}).addTo(map);
Now when a user clicks on a checkbox, I would like remove the layer or add it back. I have tried
map.removeLayer("EPSLayer");
map.removeLayer("tiles");
However, that didnt fix the issue. Any ideas or pointers that could help would be greatly appreciated.
*** Update I have created a fiddle to show the issue:
https://jsfiddle.net/31gmr4ss/3/
The idea is to click on the tree icon to show the areial view and then switch to the map view when its clicked again.
It appears to work when the tree icon is clicked however the arial view is present when the map is zoomed.
As #Fabrizio has suggested, the remove should not be passed string values, however passing just variable names causes the map to not work at all.
Thanks
Don't use strings in the function:
map.removeLayer(EPSLayer);
map.removeLayer(TopoLayer);
Depending on the layer you want to remove.
I know how to add a layer with markers, which I can toggle on/off and how to add GeoJSON layer to my map.
But I can't mix these functions.
I need to create a toggling layer from GeoJSON (polyline layer).
Is it possible to get what I need without any external plug-ins or scripts?
GeoJSON Layers and Markers can be used together without problem.
To be able to toggle your layers, you need to catch some sort of click event from something you can click on, for example a button.
From my research what I found is if you need a custom button, it is not so quick to implement yourself, so you might need to use one of the available plugins.
If you still do not want to build a button or use a plugin, you could for example set a click event on the map itself, which toggles the GeoJSON layer on and off.
I took the GeoJSON example from the leaflet website and changed it so it toggles the GeoJSON layer on and off:
var geoLayer = L.geoJson([
// ...
]);
map.on('click', function() {
if(map.hasLayer(geoLayer)) {
map.removeLayer(geoLayer);
} else {
map.addLayer(geoLayer);
}
});
Hope that helps..
Edit:
I changed the example to use the layer control of leaflet.js
which is much better...
var baseLayers = {
"Markers": markerLayer,
"GeoJSON": geoLayer
};
L.control.layers(baseLayers).addTo(map);
Didn't know about this ;)
If you want checkboxes instead of radiobuttons, use this instead
L.control.layers(null, baseLayers).addTo(map);
http://codepen.io/anon/pen/qEaEBg
I've been researching the easiest way to create an interactive map for my company with a custom of USA map I've created in Illustrator. I found this tutorial: http://parall.ax/blog/view/2985/tutorial-creating-an-interactive-svg-map which is very complete and will help me partially to achieve the results im looking.
But what about when i want to add a link on click and Text on hover of the region to each area of the map?
I'm not well versed with JavaScript so I will appreciate a lot the help and understanding from the community.
Let's assume you have an element called regionShape defined in raphael which corresponds to one of the regions in your map. Adding a link on click should be quite easy:
regionShape.node.onclick = function(e) {
window.location.href = "somepage.html";
}
In order to display a text on hover, I would recommend using jQuery. What I normally do is define a div element #hoverInfo and set its css display property to none, and then modify this property with rafael node.onmouseover, like this:
regionShape.node.onmouseover = function(e) {
$("#hoverInfo").css("display", "block");
};
And then you remove it by using onmouseout:
regionShape.node.onmouseout = function(e) {
$("#hoverInfo").css("display", "none");
};
I'm using the Google Maps v3 library, with the Multi-Marker library which extends the functionality of Google Maps for super fast adding of map marker icons to a map.
I can't figure out how to remove a single, individual map marker using the multi-marker library linked above.
Does anyone have any ideas how I'd do this using the multi-marker library (and/or Google Maps)? I've tried contacting the lead developer of the project but am not getting a response.
Thanks for any help.
Also, linked is more information on this library
http://blog.redfin.com/devblog/2010/07/introducing_multimarker_the_fastest_way_to_add_many_hundreds_or_thousands_of_markers_on_google_maps.html
UPDATE:
I have linked example code of what I'm doing. I want to dynamically remove specific map marker icons (overlays) but am struggling on how to do that. Any advise would be very appreciated. Thanks
Live example:
http://multimarker.googlecode.com/svn/trunk/fast-marker-overlay/maps-v3/example/clickable.html
Normaly, using only the google maps api, to remove an overlay from the map you would need to call the setMap(null) method on the overlay.
As I can see, the Multi-Marker library uses an array to hold all the markers and creates an overlay to show on the map, an overlay that contains the markers. To remove one, it should work to remove the marker from the array (you need to know its position in the array) and redraw the overlay.
Edit:
You need something similar to function clearOverlays() { var i = overlays.length; while (i--) { var overlay = overlays[i]; if (overlay) overlay.setMap(null); delete overlays[i]; } }
But you'll need to know the position in the array of the marker you want to delete. The function will look like this:
function clearOneOverlay(var position) { var overlay = overlays[position]; if (overlay) overlay.setMap(null); delete overlays[position]; }
try this: remember which marker you click and then remove it later
consider following code(Google Maps v2)
var current_marker;
GEvent.addListener(marker, "click", function() {
current_marker = marker;
});
//remove later
current_marker.setMap(null);