问题
I'm stuck trying to figure out why I'm getting the above issue for my heat-layer. The answers I've seen on while seatching address improper LatLng setup, however my marker's LatLng array work just fine.
I've tried adding weights as well even though the api say it's optional.
var map;
function initMap(resultSet) {
map = new google.maps.Map(document.getElementById('maps'), {
center: {
lat: 40.8173,
lng: -96.7005
},
zoom: 16,
mapTypeId: 'satellite'
});`
var labels = [];
var locations = [];
for (var i = 0; i < resultSet.length; i++) {
labels.push(resultSet[i][0]);
locations.push({
lat: parseFloat(resultSet[i][3]),
lng: parseFloat(resultSet[i][4])
});
if (data[0] == '1') {
var marker = new google.maps.Marker({
position: locations[i],
map: map,
title: labels[i]
});
marker.setMap(map);
}
}
if (data[0] == '2') {
var heatmap = new google.maps.visualization.HeatmapLayer({
data: locations,
});
heatmap.setMap(map);
}
}
回答1:
The HeatmapLayer is one of the remaining places you can't use a google.maps.LatLngLiteral for a position, it must be a google.maps.LatLng object.
(the error you get is because the anonymous object LatLngLiteral
doesn't have a .lat()
method, it has a .lat
property).
Change:
for (var i = 0; i < resultSet.length; i++) {
labels.push(resultSet[i][0]);
locations.push({
lat: parseFloat(resultSet[i][3]),
lng: parseFloat(resultSet[i][4])
});
if (data[0] == '1') {
var marker = new google.maps.Marker({
position: locations[i],
map: map,
title: labels[i]
});
marker.setMap(map);
}
}
To:
for (var i = 0; i < resultSet.length; i++) {
labels.push(resultSet[i][0]);
locations.push(new google.maps.LatLng(
parseFloat(resultSet[i][3]),
parseFloat(resultSet[i][4])
));
if (data[0] == '1') {
var marker = new google.maps.Marker({
position: locations[i],
map: map,
title: labels[i]
});
marker.setMap(map);
}
}
来源:https://stackoverflow.com/questions/58529797/typeerror-b-lat-is-not-a-function-heatmap-layer