如何使用 JSON 获取 google 热图 LatLng?
how to get google heat map LatLng with JSON?
我想要 google 热图 LatLng 和 Json 文件
这是我的 google 热图函数
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: {lat: 37.782551, lng: -122.445368}
});
heatmap = new google.maps.visualization.HeatmapLayer({
data: getPoints(),
map: map
});
}
这是我的 LatLng 函数,我想改用 getPoints() 函数 JSON file
function getPoints() {
return [
new google.maps.LatLng(37.782551, -122.445368),
new google.maps.LatLng(37.782745, -122.444586),
new google.maps.LatLng(37.782842, -122.443688),
new google.maps.LatLng(37.782919, -122.442815),
new google.maps.LatLng(37.782992, -122.442112),
}
假设您创建了一个名为 points.json 的文件,它看起来像这样,并且与您网页的 HTML 在同一目录中。
{
"points": [
[37.782551, -122.445368],
[37.782745, -122.444586],
[37.782842, -122.443688],
[37.782919, -122.442815],
[37.782992, -122.442112]
]
}
然后您需要从网络服务器请求 JSON 文件。如果你可以使用最新的网络浏览器,你可以使用fetch:
function requestPoints() {
fetch('points.json')
.then(function(response) {
return response.json();
})
.then(initMap);
}
如果您不能使用最近的网络浏览器,那么您可以使用 jQuery.getJSON 做类似的事情。
当 'points.json' 的网络请求返回时,这将使用 JSON 调用 initMap。然后你可以像这样使用JSON:
function initMap(json) {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: {lat: 37.782551, lng: -122.445368}
});
var points = json.points;
var data = [];
var i;
for (i = 0; i < points.length; i++) {
data[i] = new google.maps.LatLng(points[i][0], points[i][1]);
}
var heatmap = new google.maps.visualization.HeatmapLayer({
data: data,
map: map
});
}
如果您正在关注 Google 示例,您需要将脚本请求中的回调参数从 callback=initMap
更改为 callback=requestPoints
。
我想要 google 热图 LatLng 和 Json 文件
这是我的 google 热图函数
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: {lat: 37.782551, lng: -122.445368}
});
heatmap = new google.maps.visualization.HeatmapLayer({
data: getPoints(),
map: map
});
}
这是我的 LatLng 函数,我想改用 getPoints() 函数 JSON file
function getPoints() {
return [
new google.maps.LatLng(37.782551, -122.445368),
new google.maps.LatLng(37.782745, -122.444586),
new google.maps.LatLng(37.782842, -122.443688),
new google.maps.LatLng(37.782919, -122.442815),
new google.maps.LatLng(37.782992, -122.442112),
}
假设您创建了一个名为 points.json 的文件,它看起来像这样,并且与您网页的 HTML 在同一目录中。
{
"points": [
[37.782551, -122.445368],
[37.782745, -122.444586],
[37.782842, -122.443688],
[37.782919, -122.442815],
[37.782992, -122.442112]
]
}
然后您需要从网络服务器请求 JSON 文件。如果你可以使用最新的网络浏览器,你可以使用fetch:
function requestPoints() {
fetch('points.json')
.then(function(response) {
return response.json();
})
.then(initMap);
}
如果您不能使用最近的网络浏览器,那么您可以使用 jQuery.getJSON 做类似的事情。
当 'points.json' 的网络请求返回时,这将使用 JSON 调用 initMap。然后你可以像这样使用JSON:
function initMap(json) {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: {lat: 37.782551, lng: -122.445368}
});
var points = json.points;
var data = [];
var i;
for (i = 0; i < points.length; i++) {
data[i] = new google.maps.LatLng(points[i][0], points[i][1]);
}
var heatmap = new google.maps.visualization.HeatmapLayer({
data: data,
map: map
});
}
如果您正在关注 Google 示例,您需要将脚本请求中的回调参数从 callback=initMap
更改为 callback=requestPoints
。