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
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.
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.
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 have a situation with google map drawing manager. I want to change the drawing manager toolbar default tooltip. When we move mouse over the drawing manager tool bar(moving mouse over marker,circle)we see that tooltip "Add a Marker", "Draw a circle" . I want to change the toolbar tooltip as " Add New Location " , " Draw a Area" . I use google map API version3. Is it possible to change it?
Thanks in advance
The nodes for the buttons are not accessible via the API, the best way would be to ommit the built-in controls and create your own instead.
Another approach(using jQuery, but it would be possible without a framework too):
$(map.getDiv()).one('mouseover','img[src="https://maps.gstatic.com/mapfiles/drawing.png"]',function(e){
$(e.delegateTarget).find('img[src="https://maps.gstatic.com/mapfiles/drawing.png"]').each(function(){
$(this).closest('div[title]').attr('title',function(){
switch(this.title){
case 'Add a marker':
return 'Add New Location';
break;
case 'Draw a circle':
return 'Draw an area';
break;
default:return this.title;
}
});
});
});
It observes the mouseover-event of the buttons(because you'll never know when the buttons available inside the document) and then modifies the title.
But this approach will only work when the API uses english as language. To achieve it regardless of the language you'll have to check the top-property of the button-image(this seems to be the only detail that may be used to determine the type of shape the button is used for)
For what ever were the reasons, I couldn't get the #Dr.Molle version to work. However adapting the this answer I was able to get it to work in my application.
For example I simply wanted to change the text of the `DrawingManager' drawRectangle method and the code below worked for me. The crucial thing with this modification to the DrawingManager tooltip is to make sure the document and map objects are full loaded before this code runs.
$(".gmnoprint").each(function(){
var newObj = $(this).find("[title='Draw a rectangle']");
newObj.attr('title', 'Draw a rectangle around the area to search');
});
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);