Comments (12)
First a function to convert the geojson to a list of coords:
geoJson2heat = function(geojson) {
return geojson.features.map(function(feature) {
return [parseFloat(feature.geometry.coordinates[1]), parseFloat(feature.geometry.coordinates[0])];
});
}
Now create your map as usual (not shown). Then add the heat map as a layer, where geojson_data
is the variable declared by the .js file that contains the geojson data:
var geoData = geoJson2heat(geojson_data, 1);
var heatMap = new L.heatLayer(geoData,{radius: 40, blur: 25, maxZoom: 17});
map.addLayer(heatMap);
from leaflet.heat.
Sorry, but in fact, it was really simple.
Consider that we have a php file obs.php
that generate our geojson data :
geoJson2heat = function(geojson) {
return geojson.features.map(function(feature) {
return [parseFloat(feature.geometry.coordinates[1]), parseFloat(feature.geometry.coordinates[0])];
});
}
function addObsToMap(obs, map) {
var geoData = geoJson2heat(obs, 1);
var heatMap = new L.heatLayer(geoData,{radius: 50, blur: 25, maxZoom: 17});
map.addLayer(heatMap);
}
$.getJSON("obs.php", function(obs) { addObsToMap(obs, map); });
In case this is useful to others! ;)
from leaflet.heat.
I'd love to see this fully implemented, for qgis2web.
from leaflet.heat.
World you share your solution here?
from leaflet.heat.
Sorry, but I try to do it with a GeoJson created by a JQuery request, with no success (but i am a newbie in Javascript).
My GeoJson come from this request :
$.getJSON("geojson_7j.php", function(obs) { addObsToMap(obs, map); });
And its contents are as follows:
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-0.341547,48.528597]},"properties":{ ... ...
As I get lost in the variable names, would it be possible to help me adapt the code proposed previously?
In particular, I do not understand where the geojson_data
variable is defined.
from leaflet.heat.
I simply have a file that is a script that creates a variable containing the geojson. I load that script and so create the variable. So it starts out:
var geojson_data = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
…
from leaflet.heat.
Problem is that my geojson is created with PHP from a query on PostGreSQL database
(with echo json_encode($geojson, JSON_NUMERIC_CHECK);
)
Do you think it is a good idea to add var geojson_data =
dynamically with another PHP file/code ?
Like this :
<?php
ob_start();
include('geojson_7j.php');
$geojson = ob_get_contents();
ob_end_clean();
echo 'var geojson_data = '.$geojson;
?>
from leaflet.heat.
I haven't gotten my heat maps to work with geojson from a mysql database, though I haven't tried too hard either.
from leaflet.heat.
Easy for you! ;-)
But thanks, that got it working for me. Not quite sure what I was doing wrong, but all better now.
PS I did have one problem with my sql query, in that it was returning null values in some cases (because I forgot to include that in my WHERE clause when selecting the data from the database). It would be great if leaflet.heat would ignore null values instead of failing. (I can put this as a separate request, if needed.)
from leaflet.heat.
When I try to run the code you wrote @Jmuccigr , I get an Uncaught TypeError: Cannot read property 'map' of undefined at geoJson2heat. I am still relatively new to Javascript and coding. Here's the relevant code:
`var crime2010_points = L.geoJSON(crime2010_points)
geoJson2heat = function(crime2010_points) {
return crime2010_points.features.map(function(feature) {
return [parseFloat(feature.geometry.coordinates[1]),
parseFloat(feature.geometry.coordinates[0])];
});
}
var map = L.map('map').setView([41.75437, -72.71202], 9.5);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a
href="http://osm.org/copyright">OpenStreetMap</a> contributors',
maxZoom: 20
}).addTo(map);
var geoData = geoJson2heat(crime2010_points, 1);
var heatMap = new L.heatLayer(geoData, {radius: 40, blur: 25, maxZoom: 17});
map.addLayer(heatMap);`
from leaflet.heat.
@eliasm123 Not sure I can be much help. You're loading leaflet first, right?
from leaflet.heat.
@Jmuccigr Nevermind, I figured it out. Your code works, you just have to change the file extension of the geojson to js.
from leaflet.heat.
Related Issues (20)
- IndexSizeError: Index or size is negative or greater than the allowed amount HOT 2
- maxZoom in leaflet HOT 2
- Need demo for adding heat using click
- Is it possible to change the intensity color based on the number of heatpoints combined in view? HOT 6
- Heatlayer radius over 80 creates strange object outside default heat HOT 6
- Layering different heat maps on top of eachother
- Crashes when container has 0 width/height HOT 2
- Zooming to bounds of points added. HOT 1
- How to add popup to heat points. HOT 1
- Crash on Firefox 72 on Mac (script is "blocked from extracting canvas data") HOT 7
- Laggy Heatmaps
- Demos don't work: Blocked loading mixed active content HOT 1
- ERROR: ENOENT: no such file or directory, open 'node_modules/simpleheat/simpleheat.js' HOT 1
- Not working offline HOT 1
- Uncaught TypeError: L.heatLayer is not a function HOT 2
- MaxOpacity HOT 2
- Intensity doesn't affect gradient HOT 2
- New npm release HOT 1
- possible to add a legend ?
- Heatmap Gradient disapears when zooming in
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leaflet.heat.