我想根据我的GPS位置数量为谷歌地图标记添加标签。我在我的数据库中获取了数据我能够将标记添加到我的地图中但我无法做的是在标记内添加标记。
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; and no text property
。分配给label
property的值必须是字符串(或MarkerLabel
匿名对象)。代码目前正在分配一个号码。更改:
var labels = i + 1;
至:
var labels = ""+ (i + 1);
代码段:
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>