I have a web map with vector points. Once you click one, FramedCloud popup appears with data based on this feature.vector attributes.
There is an option to search through all points by input text field with added jquery autocomplete. However, if user uses this autocomplete, only one vector.point remains on the whole map. I would like to make that this point is automatically clicked.
selector: new OpenLayers.Control.SelectFeature(warstwa_ikon, { onSelect: PopupLokal, onUnselect: destroyPopup }
warstwa_ikon is my vector layer, PopupLokal(feature) is function generating FramedCloud.
I tried this:
map.events.triggerEvent('click', warstwa_ikon.features[0]);
But it gives an error: TypeError: (intermediate value)._featureId is undefined.
I was trying warstwa_ikon.features[0].id and few other methods of giving this function id of my lonely vector point, but with no success.
Related
I am trying to build an application that uses leaflet, PostgreSQL and PHP. I have created the APIs in PHP and added some layers from PostgreSQL and it works fine. The main theme of the app is to allow the user to be able to filter the points using some fields(its working fine) and I am also trying to show a heatmap that also gets filtered based on the form. I used this heatmap.js (https://github.com/Leaflet/Leaflet.heat) addon for leaflet. Initially all the data loads on my webpage and I am able to add the heat layer too using this code below:
geoJson2heat = function(geojson, weight) {
return geojson.features.map(function(feature) {
return [parseFloat(feature.geometry.coordinates[1]),
parseFloat(feature.geometry.coordinates[0]),
parseFloat((feature.properties[weight]))];
});
}
var geoData = geoJson2heat(lyr, 'area_cos');
var myconf = {
minOpacity: 0.7,
radius: 25,
blur: 25,
max: 1300
};
var heat = L.heatLayer(geoData, myconf).addTo(map);
layerControl.addOverlay(heat, 'Heat Map');
This displays the heatmap perfectly fine. Now I also have a form that allows user to take inputs and filter the data based on that. So when a user submits the form, it gets the filtered data from postgresql and displays it. When I try to apply the same concept to this heat layer, it does not work. I am not able to remove the layer, rather it shows me an error. This is the code I am trying to use:
$('#filterForm').submit(function(e){
e.preventDefault()
map.removeLayer(heat);
layerControl.removeLayer(heat);
.
. rest of the code here working fine
.
});
I get this error:
Uncaught TypeError: Cannot read properties of undefined (reading '_leaflet_id')
I try to print some properties of the heat layer to see if its displaying or not to make sure my layer is correct in the code but get the same error (even thought the layer is perfectly visible on the map and works fine). But if I try to print the properties of this layer outside of this jquery form submit listener, it works and shows me the correct information. I suspect that it has something to do with me calling the remove layer in the jquery listener specifically for the heat layer (because I removed other layers, point layer within this jquery listener and its working fine). I was hoping if you could tell me what the issue is and help me get a work around of this. I am just a beginner, so stuck in here.
Thanks!
I have an openlayers map with a GeoJSON layer of point-features. I would like to get a field in the attribute table (called 'Placeimage') to be passed to a link when the map feature is clicked so the web page scrolls to the correct place.
GeoJSON field name is Placeimage (a spare field I haven't used)
HTML
<span id='attribute_value_in_placeimage' href="#place_in_HTML"></span>
JavaScript-Code:
let clickedFeatureIMGName = feature.get('Placeimage');
document.getElementById(clickedFeatureIMGName).click();
I've also tried
document.getElementById(feature.get('Placeimage')).click();
Thanks for the help so far but I'm still getting something wrong.
I'm trying to use leaflet to make an interactive map (like a choropleth) using data from geojson files and I've a problem to update data and legend on the map, dynamically.
My code use a leaflet map with a mapbox-light baselayer, a dropdownlist and a legend. Two jsons files are loaded and I use two map panes to use them and set their z-index. Dropdownlist has two options and is used to change the selected geojsons to expand this example; the real case has 25 values to choose.
I use $(id_dropdownlist).change to make everything. If ddl change:
remove panes if these exists.
select geojsons files using ddl value (default url myjson files + ajax for this example).
add panes, load and color polygons.
When I make the first change in the ddl works fine: select geojsons files, load and color polygons. My problem appears from the second change onwards, after remove panes: geojsons are loaded successfully but these are not showed/colored on my map.
My amateur debugging detect than in the first change the div leaflet-pane element has the geojsons values, but from the second change onwards the same div leaflet-pane has not data.
Also, I want to update the legend at same time after load the geojsons, but I do not have any idea how to make that.
Here is a functional example in JSFiddle.
If someone could help me step by step, for learn it, I would be very grateful. If panes are not the way to do this, or another way to do this better, please tell me how.
When user changes the selection of your drop down list, you first try to clear your map by removing your previously created custom panes from the DOM.
There is a very high chance Leaflet does not expect such fiddling with its panes, even if they are custom. That is probably why when you create new custom panes with the same names, Leaflet does not reference them correctly, hence it does not display your new layers in those new panes.
For adding and removing your layers dynamically to / from the map, you should simply use Layer Groups instead. In particular, it provides you with a clearLayers method that does what you are trying to achieve.
Therefore, you just add your polygons and markers to that Layer Group (instead of directly to the map), so that you can easily clear them when the user selects a new value. The custom panes remain unaffected.
// Initialize the Layer Group.
// Add it to map so that its children will appear on map.
var layerGroup = L.layerGroup().addTo(map);
// On <select> "change" event:
layerGroup.clearLayers();
// When you have retrieved your data and are ready to convert it to layers:
L.geoJSON(data, {
style: { // Can be a function as usual.
pane: 'polygon'
},
pointToLayer: function(feature, latlng) {
return L.circleMarker(latlng, {
pane: 'POI'
});
}
}).addTo(layerGroup); // Add to layerGroup instead of directly to map.
Updated JSFiddle: https://jsfiddle.net/Ljetvd2x/4/
As for updating your legend, I am sure you should be able to find some resources on that topic, including here on SO. If you still need help, that should very probably be separated into a new post.
How can I retrieve and object on google map without user interaction. Meaning the user do not click on the object.
I have 10 polygons and I want user to navigate to each of them, manipulate maybe the color.
without clicking on the polygon itself.
I do have a "next" button that when user click, it suppose to navigate to the next polygon setting it active and to be filled with desire color.
The polygons are store in an array with json format.
Im using Data Layer. I am not sure how to implement this. I can click on polygon itself and change the color, but I need to traverse them without clicking.
Thanks
making map:
function loadJson(url, style)
{
var jsonfile = $.getJSON(url).done(function (data){
map.data.addGeoJson(data);
map.data.setStyle(style);
});
}
change color
map.data.addListener('click', function(event) {
mapName.data.overrideStyle(event.feature, currentStyle);
});
WORKING PROTOTYPE
In a data-layer there is no way to access the shapes(e.g. polygons) that have been drawn, you only may access the features.
The problem: the features are not available as array(then it would be easy to iterate over the features or select a feature by index)
Possible solution:
store the polygon-features in an array when they will be added to the layer(could be done via a addfeature-callback)
calculate the bounds of the polygons:(See: How to get LatLngBounds of feature polygon geometry in google maps v3?)
to implement the "next"-feature:
use map.data.revertStyle() to reset the style of the previous selected feature
select the first feature in the array and apply the desired style and call map.fitBounds() based on the calculated bounds(step2)
put the first feature to the end of the array
arrayWithFeatures.push(arrayWithFeatures.shift());
Demo: http://jsfiddle.net/doktormolle/anmfe/
I have a vector Layer in Open layers. http://www.geos.ed.ac.uk/~s0825955/cgi-bin/maps6.pl
When selected it uses the selected formatting intent and also brings up a popup. I want the selected formatting to be displayed when the mouse moves over the layer, and the popup to display only when the feature is clicked.
I have tried adding {hover: true} to the SelectFeature control but this makes the popups appear as you move the mouse over the map.
I have also tried adding a second feature control that the popup doesn't listen to but the style map doesn't recognize this either.
Is there any way I can have a different style for hovering that does not have the feature selected?
Only one SelectFeature control may be active at a time, unless the second one has the property highlightOnly = true. From the OpenLayers docs:
highlightOnly{Boolean} -
If true do not actually select features (that is place them in the layer’s selected features array), just highlight them.
Try adding a second SelectFeature control that just changes the style. It should look like this:
var hover_feature_control = new OpenLayers.Control.SelectFeature(
[your_layer],
{
hover: true,
highlightOnly: true,
renderIntent: "temporary"
}
);
The renderIntent property should match the name of the style you want on hover in the style map.
styleMap: new OpenLayers.StyleMap({
'default': default_marker_style,
'temporary': hover_marker_style
})
Make sure you remove {hover: true} from the control that manages popups!