Google Maps Heatmap works only when the heatmap data is hardcoded and not when it is generated using a for loopProgrammatically provide data property for a marker on a Leaflet mapjquery ui map clusterer markers aren't displayedHow do I use variable to define new google.maps.LatLng()Google maps : pan to a center of a geojson route clicking on itAdd GeoJson variable to GoogleMap per addGeoJsonTwo geojson files not superposinggeojson file formatting issue, mini-arrays(coordinates) inside larger array of coordinatesVisualizing Linestrings with properties in LeafletGoogle Maps forEach with setMap to set polyline visiblity (error: setMap is not a function)Adding markers to map (Mapbox)
Find the closest three-digit hex colour
Making arrow with a gradual colour
Existence of infinite set of positive integers s.t sum of reciprocals is rational and set of primes dividing an element is infinite
What type of education should I select in this form?
Why is my 401k manager recommending me to save more?
Why are examinees often not allowed to leave during the start and end of an exam?
Could citing a database like libgen get one into trouble?
Can you run PoE Cat6 alongside standard Cat6 cables?
Tricky riddle from sister
Why am I getting an electric shock from the water in my hot tub?
Cannot overlay, because ListPlot does not draw same X range despite the same PlotRange
Simplify the code
Confusion in understanding the behavior of inductor in RL circuit with DC source
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
What does this Pokemon Trainer mean by saying the player is "SHELLOS"?
Sentences with no verb, but an ablative
Is this house-rule removing the increased effect of cantrips at higher character levels balanced?
Aligning arrays within arrays within another array
Is it theoretically possible to hack printer using scanner tray?
Merging two data frames into a new one with unique items marked with 1 or 0
Odd PCB Layout for Voltage Regulator
Wings for orbital transfer bioships?
Old story where computer expert digitally animates The Lord of the Rings
Installed software from source, how to say yum not to install it from package?
Google Maps Heatmap works only when the heatmap data is hardcoded and not when it is generated using a for loop
Programmatically provide data property for a marker on a Leaflet mapjquery ui map clusterer markers aren't displayedHow do I use variable to define new google.maps.LatLng()Google maps : pan to a center of a geojson route clicking on itAdd GeoJson variable to GoogleMap per addGeoJsonTwo geojson files not superposinggeojson file formatting issue, mini-arrays(coordinates) inside larger array of coordinatesVisualizing Linestrings with properties in LeafletGoogle Maps forEach with setMap to set polyline visiblity (error: setMap is not a function)Adding markers to map (Mapbox)
I am trying to plot a heatmap on Google Maps using the API. On using a hardcoded array of LatLng objects, the rendering works. However, if I then try to remove the hardcoded array and generate the array using a for loop, the rendering fails.
console.log for the 2 array variables give me the same objects.
The js-fiddle is provided here: https://jsfiddle.net/arpanio/7weomu5g/61/
The 2 variables are:
- Hardcoded array: heatmapData
- Generated array: heatmap_data
Please see line 87 and line 88 where I comment out and switch between the 2 variables. The hardcoded option works. The generated array option does not work. I am printing both to console and I don't see any difference in the object (other than actual values of the latitude and longitude).
The js code is reproduced below:
var map;
var service;
var infowindow;
function initMap()
var sydney = new google.maps.LatLng(45.76404, 4.83565);
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(
document.getElementById('map'), center: sydney, zoom: 12);
var heatmapData = [
new google.maps.LatLng(45.7523537999999,4.8405),
new google.maps.LatLng(45.7663606,4.8328),
new google.maps.LatLng(45.7603967,4.8557)
];
console.log(heatmapData);
//Data to be obtained from mymap -> KML -> GeoJSON
geo_json =
"type":"FeatureCollection",
"name":"merged",
"crs":
"type":"name",
"properties":
"name":"urn:ogc:def:crs:OGC:1.3:CRS84"
,
"features":[
"type":"Feature",
"properties":
"Name":"Auchan Drive Lyon Saint Priest"
,
"geometry":
"type":"Point",
"coordinates":[
4.9252405,
45.7235401
]
,
"type":"Feature",
"properties":
"Name":"Auchan Drive Saint Genis (Chapônost)"
,
"geometry":
"type":"Point",
"coordinates":[
4.76585360000001,
45.6992269
]
,
"type":"Feature",
"properties":
"Name":"Auchan"
,
"geometry":
"type":"Point",
"coordinates":[
4.8008698,
45.7498202
]
]
;
//Convert GeoJSON to Google-specific heatmap data
var heatmap_data = [];
for(var i = 0; i < geo_json.features.length; i++)
var temp = geo_json.features[i].geometry.coordinates;
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
//console.log(temp);
/* var lat_lng = new google.maps.LatLng(temp[0], temp[1]);
heatmap_data.push(lat_lng); */
console.log(heatmap_data);
var heatmap = new google.maps.visualization.HeatmapLayer(
/* Problem here */
data: heatmapData, //This works
//data: heatmap_data, //This does not
radius: 50,
opacity: 0.4
);
heatmap.setMap(map);
var request =
location: sydney,
radius: '500',
query: 'Carrefour'
;
service = new google.maps.places.PlacesService(map);
service.textSearch(request, function(results, status)
console.log(results);
if (status === google.maps.places.PlacesServiceStatus.OK)
for (var i = 0; i < results.length; i++)
createMarker(results[i]);
//console.log(JSON.stringify(results[i].geometry.location));
map.setCenter(results[0].geometry.location);
);
function createMarker(place)
var marker = new google.maps.Marker(
map: map,
icon:
path: google.maps.SymbolPath.CIRCLE,
scale: 2
,
position: place.geometry.location
);
google.maps.event.addListener(marker, 'click', function()
infowindow.setContent(place.name);
infowindow.open(map, this);
);
javascript google-maps-api-3
add a comment |
I am trying to plot a heatmap on Google Maps using the API. On using a hardcoded array of LatLng objects, the rendering works. However, if I then try to remove the hardcoded array and generate the array using a for loop, the rendering fails.
console.log for the 2 array variables give me the same objects.
The js-fiddle is provided here: https://jsfiddle.net/arpanio/7weomu5g/61/
The 2 variables are:
- Hardcoded array: heatmapData
- Generated array: heatmap_data
Please see line 87 and line 88 where I comment out and switch between the 2 variables. The hardcoded option works. The generated array option does not work. I am printing both to console and I don't see any difference in the object (other than actual values of the latitude and longitude).
The js code is reproduced below:
var map;
var service;
var infowindow;
function initMap()
var sydney = new google.maps.LatLng(45.76404, 4.83565);
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(
document.getElementById('map'), center: sydney, zoom: 12);
var heatmapData = [
new google.maps.LatLng(45.7523537999999,4.8405),
new google.maps.LatLng(45.7663606,4.8328),
new google.maps.LatLng(45.7603967,4.8557)
];
console.log(heatmapData);
//Data to be obtained from mymap -> KML -> GeoJSON
geo_json =
"type":"FeatureCollection",
"name":"merged",
"crs":
"type":"name",
"properties":
"name":"urn:ogc:def:crs:OGC:1.3:CRS84"
,
"features":[
"type":"Feature",
"properties":
"Name":"Auchan Drive Lyon Saint Priest"
,
"geometry":
"type":"Point",
"coordinates":[
4.9252405,
45.7235401
]
,
"type":"Feature",
"properties":
"Name":"Auchan Drive Saint Genis (Chapônost)"
,
"geometry":
"type":"Point",
"coordinates":[
4.76585360000001,
45.6992269
]
,
"type":"Feature",
"properties":
"Name":"Auchan"
,
"geometry":
"type":"Point",
"coordinates":[
4.8008698,
45.7498202
]
]
;
//Convert GeoJSON to Google-specific heatmap data
var heatmap_data = [];
for(var i = 0; i < geo_json.features.length; i++)
var temp = geo_json.features[i].geometry.coordinates;
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
//console.log(temp);
/* var lat_lng = new google.maps.LatLng(temp[0], temp[1]);
heatmap_data.push(lat_lng); */
console.log(heatmap_data);
var heatmap = new google.maps.visualization.HeatmapLayer(
/* Problem here */
data: heatmapData, //This works
//data: heatmap_data, //This does not
radius: 50,
opacity: 0.4
);
heatmap.setMap(map);
var request =
location: sydney,
radius: '500',
query: 'Carrefour'
;
service = new google.maps.places.PlacesService(map);
service.textSearch(request, function(results, status)
console.log(results);
if (status === google.maps.places.PlacesServiceStatus.OK)
for (var i = 0; i < results.length; i++)
createMarker(results[i]);
//console.log(JSON.stringify(results[i].geometry.location));
map.setCenter(results[0].geometry.location);
);
function createMarker(place)
var marker = new google.maps.Marker(
map: map,
icon:
path: google.maps.SymbolPath.CIRCLE,
scale: 2
,
position: place.geometry.location
);
google.maps.event.addListener(marker, 'click', function()
infowindow.setContent(place.name);
infowindow.open(map, this);
);
javascript google-maps-api-3
add a comment |
I am trying to plot a heatmap on Google Maps using the API. On using a hardcoded array of LatLng objects, the rendering works. However, if I then try to remove the hardcoded array and generate the array using a for loop, the rendering fails.
console.log for the 2 array variables give me the same objects.
The js-fiddle is provided here: https://jsfiddle.net/arpanio/7weomu5g/61/
The 2 variables are:
- Hardcoded array: heatmapData
- Generated array: heatmap_data
Please see line 87 and line 88 where I comment out and switch between the 2 variables. The hardcoded option works. The generated array option does not work. I am printing both to console and I don't see any difference in the object (other than actual values of the latitude and longitude).
The js code is reproduced below:
var map;
var service;
var infowindow;
function initMap()
var sydney = new google.maps.LatLng(45.76404, 4.83565);
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(
document.getElementById('map'), center: sydney, zoom: 12);
var heatmapData = [
new google.maps.LatLng(45.7523537999999,4.8405),
new google.maps.LatLng(45.7663606,4.8328),
new google.maps.LatLng(45.7603967,4.8557)
];
console.log(heatmapData);
//Data to be obtained from mymap -> KML -> GeoJSON
geo_json =
"type":"FeatureCollection",
"name":"merged",
"crs":
"type":"name",
"properties":
"name":"urn:ogc:def:crs:OGC:1.3:CRS84"
,
"features":[
"type":"Feature",
"properties":
"Name":"Auchan Drive Lyon Saint Priest"
,
"geometry":
"type":"Point",
"coordinates":[
4.9252405,
45.7235401
]
,
"type":"Feature",
"properties":
"Name":"Auchan Drive Saint Genis (Chapônost)"
,
"geometry":
"type":"Point",
"coordinates":[
4.76585360000001,
45.6992269
]
,
"type":"Feature",
"properties":
"Name":"Auchan"
,
"geometry":
"type":"Point",
"coordinates":[
4.8008698,
45.7498202
]
]
;
//Convert GeoJSON to Google-specific heatmap data
var heatmap_data = [];
for(var i = 0; i < geo_json.features.length; i++)
var temp = geo_json.features[i].geometry.coordinates;
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
//console.log(temp);
/* var lat_lng = new google.maps.LatLng(temp[0], temp[1]);
heatmap_data.push(lat_lng); */
console.log(heatmap_data);
var heatmap = new google.maps.visualization.HeatmapLayer(
/* Problem here */
data: heatmapData, //This works
//data: heatmap_data, //This does not
radius: 50,
opacity: 0.4
);
heatmap.setMap(map);
var request =
location: sydney,
radius: '500',
query: 'Carrefour'
;
service = new google.maps.places.PlacesService(map);
service.textSearch(request, function(results, status)
console.log(results);
if (status === google.maps.places.PlacesServiceStatus.OK)
for (var i = 0; i < results.length; i++)
createMarker(results[i]);
//console.log(JSON.stringify(results[i].geometry.location));
map.setCenter(results[0].geometry.location);
);
function createMarker(place)
var marker = new google.maps.Marker(
map: map,
icon:
path: google.maps.SymbolPath.CIRCLE,
scale: 2
,
position: place.geometry.location
);
google.maps.event.addListener(marker, 'click', function()
infowindow.setContent(place.name);
infowindow.open(map, this);
);
javascript google-maps-api-3
I am trying to plot a heatmap on Google Maps using the API. On using a hardcoded array of LatLng objects, the rendering works. However, if I then try to remove the hardcoded array and generate the array using a for loop, the rendering fails.
console.log for the 2 array variables give me the same objects.
The js-fiddle is provided here: https://jsfiddle.net/arpanio/7weomu5g/61/
The 2 variables are:
- Hardcoded array: heatmapData
- Generated array: heatmap_data
Please see line 87 and line 88 where I comment out and switch between the 2 variables. The hardcoded option works. The generated array option does not work. I am printing both to console and I don't see any difference in the object (other than actual values of the latitude and longitude).
The js code is reproduced below:
var map;
var service;
var infowindow;
function initMap()
var sydney = new google.maps.LatLng(45.76404, 4.83565);
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(
document.getElementById('map'), center: sydney, zoom: 12);
var heatmapData = [
new google.maps.LatLng(45.7523537999999,4.8405),
new google.maps.LatLng(45.7663606,4.8328),
new google.maps.LatLng(45.7603967,4.8557)
];
console.log(heatmapData);
//Data to be obtained from mymap -> KML -> GeoJSON
geo_json =
"type":"FeatureCollection",
"name":"merged",
"crs":
"type":"name",
"properties":
"name":"urn:ogc:def:crs:OGC:1.3:CRS84"
,
"features":[
"type":"Feature",
"properties":
"Name":"Auchan Drive Lyon Saint Priest"
,
"geometry":
"type":"Point",
"coordinates":[
4.9252405,
45.7235401
]
,
"type":"Feature",
"properties":
"Name":"Auchan Drive Saint Genis (Chapônost)"
,
"geometry":
"type":"Point",
"coordinates":[
4.76585360000001,
45.6992269
]
,
"type":"Feature",
"properties":
"Name":"Auchan"
,
"geometry":
"type":"Point",
"coordinates":[
4.8008698,
45.7498202
]
]
;
//Convert GeoJSON to Google-specific heatmap data
var heatmap_data = [];
for(var i = 0; i < geo_json.features.length; i++)
var temp = geo_json.features[i].geometry.coordinates;
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
//console.log(temp);
/* var lat_lng = new google.maps.LatLng(temp[0], temp[1]);
heatmap_data.push(lat_lng); */
console.log(heatmap_data);
var heatmap = new google.maps.visualization.HeatmapLayer(
/* Problem here */
data: heatmapData, //This works
//data: heatmap_data, //This does not
radius: 50,
opacity: 0.4
);
heatmap.setMap(map);
var request =
location: sydney,
radius: '500',
query: 'Carrefour'
;
service = new google.maps.places.PlacesService(map);
service.textSearch(request, function(results, status)
console.log(results);
if (status === google.maps.places.PlacesServiceStatus.OK)
for (var i = 0; i < results.length; i++)
createMarker(results[i]);
//console.log(JSON.stringify(results[i].geometry.location));
map.setCenter(results[0].geometry.location);
);
function createMarker(place)
var marker = new google.maps.Marker(
map: map,
icon:
path: google.maps.SymbolPath.CIRCLE,
scale: 2
,
position: place.geometry.location
);
google.maps.event.addListener(marker, 'click', function()
infowindow.setContent(place.name);
infowindow.open(map, this);
);
javascript google-maps-api-3
javascript google-maps-api-3
asked Mar 25 at 17:27
ChaosChaos
1156 bronze badges
1156 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Change co-ordinates sequence of temp
variable -
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
to
heatmap_data.push(new google.maps.LatLng(temp[1], temp[0]));
Working JSFiddle - https://jsfiddle.net/hL7n2fek/
Your heatmap
is getting generated, but with hardcoded heatmapData
the co-ordinates are 45.7523537999999,4.8405
which points to somewhere in France
and your google map is initialized nearby to same co-ordinates.
But in your geometry
property in geo_json
object, the co-ordinates are specified as [4.9252405, 45.7235401] which points to location
somewhere in Somalia
. Your google map visible section does not covers this. If you zoom in more, you can see that it is getting generated.
Thank you! My mistake was quite stupid.
– Chaos
Mar 25 at 18:02
add a comment |
GeoJson specifies the coordinates in the order [Longitude, Latitude]
.
So, this code:
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1])); // temp[0] (is Longitude), temp[1] (is Latitude)
should be:
heatmap_data.push(new google.maps.LatLng(temp[1], temp[0])); // temp[1] (is Latitude), temp[1] (is Longitude)
updated fiddle
code snippet:
var map;
var service;
var infowindow;
function initMap()
var sydney = new google.maps.LatLng(45.76404, 4.83565);
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(
document.getElementById('map'),
center: sydney,
zoom: 12
);
var heatmapData = [
new google.maps.LatLng(45.7523537999999, 4.8405),
new google.maps.LatLng(45.7663606, 4.8328),
new google.maps.LatLng(45.7603967, 4.8557)
];
console.log(heatmapData);
//Data to be obtained from mymap -> KML -> GeoJSON
geo_json =
"type": "FeatureCollection",
"name": "merged",
"crs":
"type": "name",
"properties":
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
,
"features": [
"type": "Feature",
"properties":
"Name": "Auchan Drive Lyon Saint Priest"
,
"geometry":
"type": "Point",
"coordinates": [
4.8405,
45.7523537999999
]
,
"type": "Feature",
"properties":
"Name": "Auchan Drive Saint Genis (Chapônost)"
,
"geometry":
"type": "Point",
"coordinates": [
4.8328,
45.7663606
]
,
"type": "Feature",
"properties":
"Name": "Auchan"
,
"geometry":
"type": "Point",
"coordinates": [
4.8557,
45.7603967
]
]
;
//Convert GeoJSON to Google-specific heatmap data
var heatmap_data = [];
for (var i = 0; i < geo_json.features.length; i++)
var temp = geo_json.features[i].geometry.coordinates;
// heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
console.log(temp);
var lat_lng = new google.maps.LatLng(temp[1], temp[0]);
heatmap_data.push(lat_lng);
console.log(heatmap_data);
var heatmap = new google.maps.visualization.HeatmapLayer(
/* Problem here */
// data: heatmapData, //This works
data: heatmap_data, //This does not
radius: 50,
opacity: 0.4
);
heatmap.setMap(map);
var request =
location: sydney,
radius: '500',
query: 'Carrefour'
;
service = new google.maps.places.PlacesService(map);
service.textSearch(request, function(results, status)
console.log(results);
if (status === google.maps.places.PlacesServiceStatus.OK)
for (var i = 0; i < results.length; i++)
createMarker(results[i]);
//console.log(JSON.stringify(results[i].geometry.location));
map.setCenter(results[0].geometry.location);
);
function createMarker(place)
var marker = new google.maps.Marker(
map: map,
icon:
path: google.maps.SymbolPath.CIRCLE,
scale: 2
,
position: place.geometry.location
);
google.maps.event.addListener(marker, 'click', function()
infowindow.setContent(place.name);
infowindow.open(map, this);
);
html,
body,
#map
height: 100%;
margin: 0;
padding: 0;
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places,visualization&callback=initMap" async defer></script>
Thank you! Quite a silly mistake on my end.
– Chaos
Mar 25 at 18:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55343411%2fgoogle-maps-heatmap-works-only-when-the-heatmap-data-is-hardcoded-and-not-when-i%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Change co-ordinates sequence of temp
variable -
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
to
heatmap_data.push(new google.maps.LatLng(temp[1], temp[0]));
Working JSFiddle - https://jsfiddle.net/hL7n2fek/
Your heatmap
is getting generated, but with hardcoded heatmapData
the co-ordinates are 45.7523537999999,4.8405
which points to somewhere in France
and your google map is initialized nearby to same co-ordinates.
But in your geometry
property in geo_json
object, the co-ordinates are specified as [4.9252405, 45.7235401] which points to location
somewhere in Somalia
. Your google map visible section does not covers this. If you zoom in more, you can see that it is getting generated.
Thank you! My mistake was quite stupid.
– Chaos
Mar 25 at 18:02
add a comment |
Change co-ordinates sequence of temp
variable -
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
to
heatmap_data.push(new google.maps.LatLng(temp[1], temp[0]));
Working JSFiddle - https://jsfiddle.net/hL7n2fek/
Your heatmap
is getting generated, but with hardcoded heatmapData
the co-ordinates are 45.7523537999999,4.8405
which points to somewhere in France
and your google map is initialized nearby to same co-ordinates.
But in your geometry
property in geo_json
object, the co-ordinates are specified as [4.9252405, 45.7235401] which points to location
somewhere in Somalia
. Your google map visible section does not covers this. If you zoom in more, you can see that it is getting generated.
Thank you! My mistake was quite stupid.
– Chaos
Mar 25 at 18:02
add a comment |
Change co-ordinates sequence of temp
variable -
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
to
heatmap_data.push(new google.maps.LatLng(temp[1], temp[0]));
Working JSFiddle - https://jsfiddle.net/hL7n2fek/
Your heatmap
is getting generated, but with hardcoded heatmapData
the co-ordinates are 45.7523537999999,4.8405
which points to somewhere in France
and your google map is initialized nearby to same co-ordinates.
But in your geometry
property in geo_json
object, the co-ordinates are specified as [4.9252405, 45.7235401] which points to location
somewhere in Somalia
. Your google map visible section does not covers this. If you zoom in more, you can see that it is getting generated.
Change co-ordinates sequence of temp
variable -
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
to
heatmap_data.push(new google.maps.LatLng(temp[1], temp[0]));
Working JSFiddle - https://jsfiddle.net/hL7n2fek/
Your heatmap
is getting generated, but with hardcoded heatmapData
the co-ordinates are 45.7523537999999,4.8405
which points to somewhere in France
and your google map is initialized nearby to same co-ordinates.
But in your geometry
property in geo_json
object, the co-ordinates are specified as [4.9252405, 45.7235401] which points to location
somewhere in Somalia
. Your google map visible section does not covers this. If you zoom in more, you can see that it is getting generated.
edited Mar 25 at 18:00
answered Mar 25 at 17:39
randomSoulrandomSoul
2,8131 gold badge8 silver badges15 bronze badges
2,8131 gold badge8 silver badges15 bronze badges
Thank you! My mistake was quite stupid.
– Chaos
Mar 25 at 18:02
add a comment |
Thank you! My mistake was quite stupid.
– Chaos
Mar 25 at 18:02
Thank you! My mistake was quite stupid.
– Chaos
Mar 25 at 18:02
Thank you! My mistake was quite stupid.
– Chaos
Mar 25 at 18:02
add a comment |
GeoJson specifies the coordinates in the order [Longitude, Latitude]
.
So, this code:
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1])); // temp[0] (is Longitude), temp[1] (is Latitude)
should be:
heatmap_data.push(new google.maps.LatLng(temp[1], temp[0])); // temp[1] (is Latitude), temp[1] (is Longitude)
updated fiddle
code snippet:
var map;
var service;
var infowindow;
function initMap()
var sydney = new google.maps.LatLng(45.76404, 4.83565);
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(
document.getElementById('map'),
center: sydney,
zoom: 12
);
var heatmapData = [
new google.maps.LatLng(45.7523537999999, 4.8405),
new google.maps.LatLng(45.7663606, 4.8328),
new google.maps.LatLng(45.7603967, 4.8557)
];
console.log(heatmapData);
//Data to be obtained from mymap -> KML -> GeoJSON
geo_json =
"type": "FeatureCollection",
"name": "merged",
"crs":
"type": "name",
"properties":
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
,
"features": [
"type": "Feature",
"properties":
"Name": "Auchan Drive Lyon Saint Priest"
,
"geometry":
"type": "Point",
"coordinates": [
4.8405,
45.7523537999999
]
,
"type": "Feature",
"properties":
"Name": "Auchan Drive Saint Genis (Chapônost)"
,
"geometry":
"type": "Point",
"coordinates": [
4.8328,
45.7663606
]
,
"type": "Feature",
"properties":
"Name": "Auchan"
,
"geometry":
"type": "Point",
"coordinates": [
4.8557,
45.7603967
]
]
;
//Convert GeoJSON to Google-specific heatmap data
var heatmap_data = [];
for (var i = 0; i < geo_json.features.length; i++)
var temp = geo_json.features[i].geometry.coordinates;
// heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
console.log(temp);
var lat_lng = new google.maps.LatLng(temp[1], temp[0]);
heatmap_data.push(lat_lng);
console.log(heatmap_data);
var heatmap = new google.maps.visualization.HeatmapLayer(
/* Problem here */
// data: heatmapData, //This works
data: heatmap_data, //This does not
radius: 50,
opacity: 0.4
);
heatmap.setMap(map);
var request =
location: sydney,
radius: '500',
query: 'Carrefour'
;
service = new google.maps.places.PlacesService(map);
service.textSearch(request, function(results, status)
console.log(results);
if (status === google.maps.places.PlacesServiceStatus.OK)
for (var i = 0; i < results.length; i++)
createMarker(results[i]);
//console.log(JSON.stringify(results[i].geometry.location));
map.setCenter(results[0].geometry.location);
);
function createMarker(place)
var marker = new google.maps.Marker(
map: map,
icon:
path: google.maps.SymbolPath.CIRCLE,
scale: 2
,
position: place.geometry.location
);
google.maps.event.addListener(marker, 'click', function()
infowindow.setContent(place.name);
infowindow.open(map, this);
);
html,
body,
#map
height: 100%;
margin: 0;
padding: 0;
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places,visualization&callback=initMap" async defer></script>
Thank you! Quite a silly mistake on my end.
– Chaos
Mar 25 at 18:03
add a comment |
GeoJson specifies the coordinates in the order [Longitude, Latitude]
.
So, this code:
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1])); // temp[0] (is Longitude), temp[1] (is Latitude)
should be:
heatmap_data.push(new google.maps.LatLng(temp[1], temp[0])); // temp[1] (is Latitude), temp[1] (is Longitude)
updated fiddle
code snippet:
var map;
var service;
var infowindow;
function initMap()
var sydney = new google.maps.LatLng(45.76404, 4.83565);
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(
document.getElementById('map'),
center: sydney,
zoom: 12
);
var heatmapData = [
new google.maps.LatLng(45.7523537999999, 4.8405),
new google.maps.LatLng(45.7663606, 4.8328),
new google.maps.LatLng(45.7603967, 4.8557)
];
console.log(heatmapData);
//Data to be obtained from mymap -> KML -> GeoJSON
geo_json =
"type": "FeatureCollection",
"name": "merged",
"crs":
"type": "name",
"properties":
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
,
"features": [
"type": "Feature",
"properties":
"Name": "Auchan Drive Lyon Saint Priest"
,
"geometry":
"type": "Point",
"coordinates": [
4.8405,
45.7523537999999
]
,
"type": "Feature",
"properties":
"Name": "Auchan Drive Saint Genis (Chapônost)"
,
"geometry":
"type": "Point",
"coordinates": [
4.8328,
45.7663606
]
,
"type": "Feature",
"properties":
"Name": "Auchan"
,
"geometry":
"type": "Point",
"coordinates": [
4.8557,
45.7603967
]
]
;
//Convert GeoJSON to Google-specific heatmap data
var heatmap_data = [];
for (var i = 0; i < geo_json.features.length; i++)
var temp = geo_json.features[i].geometry.coordinates;
// heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
console.log(temp);
var lat_lng = new google.maps.LatLng(temp[1], temp[0]);
heatmap_data.push(lat_lng);
console.log(heatmap_data);
var heatmap = new google.maps.visualization.HeatmapLayer(
/* Problem here */
// data: heatmapData, //This works
data: heatmap_data, //This does not
radius: 50,
opacity: 0.4
);
heatmap.setMap(map);
var request =
location: sydney,
radius: '500',
query: 'Carrefour'
;
service = new google.maps.places.PlacesService(map);
service.textSearch(request, function(results, status)
console.log(results);
if (status === google.maps.places.PlacesServiceStatus.OK)
for (var i = 0; i < results.length; i++)
createMarker(results[i]);
//console.log(JSON.stringify(results[i].geometry.location));
map.setCenter(results[0].geometry.location);
);
function createMarker(place)
var marker = new google.maps.Marker(
map: map,
icon:
path: google.maps.SymbolPath.CIRCLE,
scale: 2
,
position: place.geometry.location
);
google.maps.event.addListener(marker, 'click', function()
infowindow.setContent(place.name);
infowindow.open(map, this);
);
html,
body,
#map
height: 100%;
margin: 0;
padding: 0;
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places,visualization&callback=initMap" async defer></script>
Thank you! Quite a silly mistake on my end.
– Chaos
Mar 25 at 18:03
add a comment |
GeoJson specifies the coordinates in the order [Longitude, Latitude]
.
So, this code:
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1])); // temp[0] (is Longitude), temp[1] (is Latitude)
should be:
heatmap_data.push(new google.maps.LatLng(temp[1], temp[0])); // temp[1] (is Latitude), temp[1] (is Longitude)
updated fiddle
code snippet:
var map;
var service;
var infowindow;
function initMap()
var sydney = new google.maps.LatLng(45.76404, 4.83565);
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(
document.getElementById('map'),
center: sydney,
zoom: 12
);
var heatmapData = [
new google.maps.LatLng(45.7523537999999, 4.8405),
new google.maps.LatLng(45.7663606, 4.8328),
new google.maps.LatLng(45.7603967, 4.8557)
];
console.log(heatmapData);
//Data to be obtained from mymap -> KML -> GeoJSON
geo_json =
"type": "FeatureCollection",
"name": "merged",
"crs":
"type": "name",
"properties":
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
,
"features": [
"type": "Feature",
"properties":
"Name": "Auchan Drive Lyon Saint Priest"
,
"geometry":
"type": "Point",
"coordinates": [
4.8405,
45.7523537999999
]
,
"type": "Feature",
"properties":
"Name": "Auchan Drive Saint Genis (Chapônost)"
,
"geometry":
"type": "Point",
"coordinates": [
4.8328,
45.7663606
]
,
"type": "Feature",
"properties":
"Name": "Auchan"
,
"geometry":
"type": "Point",
"coordinates": [
4.8557,
45.7603967
]
]
;
//Convert GeoJSON to Google-specific heatmap data
var heatmap_data = [];
for (var i = 0; i < geo_json.features.length; i++)
var temp = geo_json.features[i].geometry.coordinates;
// heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
console.log(temp);
var lat_lng = new google.maps.LatLng(temp[1], temp[0]);
heatmap_data.push(lat_lng);
console.log(heatmap_data);
var heatmap = new google.maps.visualization.HeatmapLayer(
/* Problem here */
// data: heatmapData, //This works
data: heatmap_data, //This does not
radius: 50,
opacity: 0.4
);
heatmap.setMap(map);
var request =
location: sydney,
radius: '500',
query: 'Carrefour'
;
service = new google.maps.places.PlacesService(map);
service.textSearch(request, function(results, status)
console.log(results);
if (status === google.maps.places.PlacesServiceStatus.OK)
for (var i = 0; i < results.length; i++)
createMarker(results[i]);
//console.log(JSON.stringify(results[i].geometry.location));
map.setCenter(results[0].geometry.location);
);
function createMarker(place)
var marker = new google.maps.Marker(
map: map,
icon:
path: google.maps.SymbolPath.CIRCLE,
scale: 2
,
position: place.geometry.location
);
google.maps.event.addListener(marker, 'click', function()
infowindow.setContent(place.name);
infowindow.open(map, this);
);
html,
body,
#map
height: 100%;
margin: 0;
padding: 0;
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places,visualization&callback=initMap" async defer></script>
GeoJson specifies the coordinates in the order [Longitude, Latitude]
.
So, this code:
heatmap_data.push(new google.maps.LatLng(temp[0], temp[1])); // temp[0] (is Longitude), temp[1] (is Latitude)
should be:
heatmap_data.push(new google.maps.LatLng(temp[1], temp[0])); // temp[1] (is Latitude), temp[1] (is Longitude)
updated fiddle
code snippet:
var map;
var service;
var infowindow;
function initMap()
var sydney = new google.maps.LatLng(45.76404, 4.83565);
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(
document.getElementById('map'),
center: sydney,
zoom: 12
);
var heatmapData = [
new google.maps.LatLng(45.7523537999999, 4.8405),
new google.maps.LatLng(45.7663606, 4.8328),
new google.maps.LatLng(45.7603967, 4.8557)
];
console.log(heatmapData);
//Data to be obtained from mymap -> KML -> GeoJSON
geo_json =
"type": "FeatureCollection",
"name": "merged",
"crs":
"type": "name",
"properties":
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
,
"features": [
"type": "Feature",
"properties":
"Name": "Auchan Drive Lyon Saint Priest"
,
"geometry":
"type": "Point",
"coordinates": [
4.8405,
45.7523537999999
]
,
"type": "Feature",
"properties":
"Name": "Auchan Drive Saint Genis (Chapônost)"
,
"geometry":
"type": "Point",
"coordinates": [
4.8328,
45.7663606
]
,
"type": "Feature",
"properties":
"Name": "Auchan"
,
"geometry":
"type": "Point",
"coordinates": [
4.8557,
45.7603967
]
]
;
//Convert GeoJSON to Google-specific heatmap data
var heatmap_data = [];
for (var i = 0; i < geo_json.features.length; i++)
var temp = geo_json.features[i].geometry.coordinates;
// heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
console.log(temp);
var lat_lng = new google.maps.LatLng(temp[1], temp[0]);
heatmap_data.push(lat_lng);
console.log(heatmap_data);
var heatmap = new google.maps.visualization.HeatmapLayer(
/* Problem here */
// data: heatmapData, //This works
data: heatmap_data, //This does not
radius: 50,
opacity: 0.4
);
heatmap.setMap(map);
var request =
location: sydney,
radius: '500',
query: 'Carrefour'
;
service = new google.maps.places.PlacesService(map);
service.textSearch(request, function(results, status)
console.log(results);
if (status === google.maps.places.PlacesServiceStatus.OK)
for (var i = 0; i < results.length; i++)
createMarker(results[i]);
//console.log(JSON.stringify(results[i].geometry.location));
map.setCenter(results[0].geometry.location);
);
function createMarker(place)
var marker = new google.maps.Marker(
map: map,
icon:
path: google.maps.SymbolPath.CIRCLE,
scale: 2
,
position: place.geometry.location
);
google.maps.event.addListener(marker, 'click', function()
infowindow.setContent(place.name);
infowindow.open(map, this);
);
html,
body,
#map
height: 100%;
margin: 0;
padding: 0;
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places,visualization&callback=initMap" async defer></script>
var map;
var service;
var infowindow;
function initMap()
var sydney = new google.maps.LatLng(45.76404, 4.83565);
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(
document.getElementById('map'),
center: sydney,
zoom: 12
);
var heatmapData = [
new google.maps.LatLng(45.7523537999999, 4.8405),
new google.maps.LatLng(45.7663606, 4.8328),
new google.maps.LatLng(45.7603967, 4.8557)
];
console.log(heatmapData);
//Data to be obtained from mymap -> KML -> GeoJSON
geo_json =
"type": "FeatureCollection",
"name": "merged",
"crs":
"type": "name",
"properties":
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
,
"features": [
"type": "Feature",
"properties":
"Name": "Auchan Drive Lyon Saint Priest"
,
"geometry":
"type": "Point",
"coordinates": [
4.8405,
45.7523537999999
]
,
"type": "Feature",
"properties":
"Name": "Auchan Drive Saint Genis (Chapônost)"
,
"geometry":
"type": "Point",
"coordinates": [
4.8328,
45.7663606
]
,
"type": "Feature",
"properties":
"Name": "Auchan"
,
"geometry":
"type": "Point",
"coordinates": [
4.8557,
45.7603967
]
]
;
//Convert GeoJSON to Google-specific heatmap data
var heatmap_data = [];
for (var i = 0; i < geo_json.features.length; i++)
var temp = geo_json.features[i].geometry.coordinates;
// heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
console.log(temp);
var lat_lng = new google.maps.LatLng(temp[1], temp[0]);
heatmap_data.push(lat_lng);
console.log(heatmap_data);
var heatmap = new google.maps.visualization.HeatmapLayer(
/* Problem here */
// data: heatmapData, //This works
data: heatmap_data, //This does not
radius: 50,
opacity: 0.4
);
heatmap.setMap(map);
var request =
location: sydney,
radius: '500',
query: 'Carrefour'
;
service = new google.maps.places.PlacesService(map);
service.textSearch(request, function(results, status)
console.log(results);
if (status === google.maps.places.PlacesServiceStatus.OK)
for (var i = 0; i < results.length; i++)
createMarker(results[i]);
//console.log(JSON.stringify(results[i].geometry.location));
map.setCenter(results[0].geometry.location);
);
function createMarker(place)
var marker = new google.maps.Marker(
map: map,
icon:
path: google.maps.SymbolPath.CIRCLE,
scale: 2
,
position: place.geometry.location
);
google.maps.event.addListener(marker, 'click', function()
infowindow.setContent(place.name);
infowindow.open(map, this);
);
html,
body,
#map
height: 100%;
margin: 0;
padding: 0;
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places,visualization&callback=initMap" async defer></script>
var map;
var service;
var infowindow;
function initMap()
var sydney = new google.maps.LatLng(45.76404, 4.83565);
infowindow = new google.maps.InfoWindow();
map = new google.maps.Map(
document.getElementById('map'),
center: sydney,
zoom: 12
);
var heatmapData = [
new google.maps.LatLng(45.7523537999999, 4.8405),
new google.maps.LatLng(45.7663606, 4.8328),
new google.maps.LatLng(45.7603967, 4.8557)
];
console.log(heatmapData);
//Data to be obtained from mymap -> KML -> GeoJSON
geo_json =
"type": "FeatureCollection",
"name": "merged",
"crs":
"type": "name",
"properties":
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
,
"features": [
"type": "Feature",
"properties":
"Name": "Auchan Drive Lyon Saint Priest"
,
"geometry":
"type": "Point",
"coordinates": [
4.8405,
45.7523537999999
]
,
"type": "Feature",
"properties":
"Name": "Auchan Drive Saint Genis (Chapônost)"
,
"geometry":
"type": "Point",
"coordinates": [
4.8328,
45.7663606
]
,
"type": "Feature",
"properties":
"Name": "Auchan"
,
"geometry":
"type": "Point",
"coordinates": [
4.8557,
45.7603967
]
]
;
//Convert GeoJSON to Google-specific heatmap data
var heatmap_data = [];
for (var i = 0; i < geo_json.features.length; i++)
var temp = geo_json.features[i].geometry.coordinates;
// heatmap_data.push(new google.maps.LatLng(temp[0], temp[1]));
console.log(temp);
var lat_lng = new google.maps.LatLng(temp[1], temp[0]);
heatmap_data.push(lat_lng);
console.log(heatmap_data);
var heatmap = new google.maps.visualization.HeatmapLayer(
/* Problem here */
// data: heatmapData, //This works
data: heatmap_data, //This does not
radius: 50,
opacity: 0.4
);
heatmap.setMap(map);
var request =
location: sydney,
radius: '500',
query: 'Carrefour'
;
service = new google.maps.places.PlacesService(map);
service.textSearch(request, function(results, status)
console.log(results);
if (status === google.maps.places.PlacesServiceStatus.OK)
for (var i = 0; i < results.length; i++)
createMarker(results[i]);
//console.log(JSON.stringify(results[i].geometry.location));
map.setCenter(results[0].geometry.location);
);
function createMarker(place)
var marker = new google.maps.Marker(
map: map,
icon:
path: google.maps.SymbolPath.CIRCLE,
scale: 2
,
position: place.geometry.location
);
google.maps.event.addListener(marker, 'click', function()
infowindow.setContent(place.name);
infowindow.open(map, this);
);
html,
body,
#map
height: 100%;
margin: 0;
padding: 0;
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places,visualization&callback=initMap" async defer></script>
edited Mar 25 at 21:51
answered Mar 25 at 17:53
geocodezipgeocodezip
130k10 gold badges154 silver badges185 bronze badges
130k10 gold badges154 silver badges185 bronze badges
Thank you! Quite a silly mistake on my end.
– Chaos
Mar 25 at 18:03
add a comment |
Thank you! Quite a silly mistake on my end.
– Chaos
Mar 25 at 18:03
Thank you! Quite a silly mistake on my end.
– Chaos
Mar 25 at 18:03
Thank you! Quite a silly mistake on my end.
– Chaos
Mar 25 at 18:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55343411%2fgoogle-maps-heatmap-works-only-when-the-heatmap-data-is-hardcoded-and-not-when-i%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown