gpt4 book ai didi

javascript - 如何向谷歌地图标记添加标签? (错误: InvalidValueError: setLabel: not a string; and no text property)

转载 作者:行者123 更新时间:2023-12-01 01:43:41 27 4
gpt4 key购买 nike

我想根据我的 GPS 位置数量向 Google map 标记添加标签。我在数据库中获取了数据,我可以向 map 添加标记,但我无法在标记内添加标记。

for(var i = 0; i < data.length; i++) {
var coords = data[i].GPSCoordinates.split(',');
var position = new google.maps.LatLng(coords[0], coords[1]);
var labels = i + 1;
addMarker(position, map, labels);
}

function addMarker(location, map, label) {
var marker = new google.maps.Marker({
position: location,
map: map,
label: label
});
}

最佳答案

我在您的代码中遇到了 javascript 错误:InvalidValueError: setLabel: not a string;并且没有文本属性。分配给 label property 的值必须是一个字符串(或 MarkerLabel 匿名对象)。该代码当前正在分配一个号码。变化:

var labels = i + 1;

至:

var labels = ""+ (i + 1);

proof of concept fiddle

screenshot of resulting map

代码片段:

function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1660756),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var data = [{GPSCoordinates: "37.4419, -122.1419"},
{GPSCoordinates: "37.4529598, -122.1817252"},
{GPSCoordinates: "37.4335499, -122.2030209"},
{GPSCoordinates: "37.424106, -122.1660756"}
]
for (var i = 0; i < data.length; i++) {
var coords = data[i].GPSCoordinates.split(',');
var position = new google.maps.LatLng(coords[0], coords[1]);
var labels = "" + (i + 1);
addMarker(position, map, labels);
}

function addMarker(location, map, label) {
var marker = new google.maps.Marker({
position: location,
map: map,
label: label
});
}
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

关于javascript - 如何向谷歌地图标记添加标签? (错误: InvalidValueError: setLabel: not a string; and no text property),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52142299/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com