我可以在循环中创建标记,但我无法在同一循环中添加标记。有没有办法做到这一点?有人可以共享代码吗?标签必须是SYD,MEL,PERTH,如数组中所示。
var cities = {
'SYD' : [-21.459866 , 119.498334 ],
'MEL' : [-26.233502 , 119.323756 ],
'PERTH' : [-31.283012 , 119.444157 ]
}
for (var key in cities) {
var data = cities[key];
var marker = new google.maps.Marker({
position: new google.maps.LatLng (data[0], data[1]),
map: map,
icon: image,
title: 'test',
});
不确定本节
var label = new Label({
map: map
});
label.bindTo('position', marker, 'position');
label.bindTo('text', marker, 'position');
答案 0 :(得分:1)
var cities = {
'SYD' : [-21.459866 , 119.498334 ],
'MEL' : [-26.233502 , 119.323756 ],
'PERTH' : [-31.283012 , 119.444157 ]
}
function initialize() {
var bangalore = { lat: -21.459866, lng: 119.498334 };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: bangalore
});
// This event listener calls addMarker() when the map is clicked.
google.maps.event.addListener(map, 'click', function(event) {
addMarker(event.latLng, map);
});
var cities = {
'SYD' : [-21.459866 , 119.498334 ],
'MEL' : [-21.456866 , 119.518334 ],
'PERTH' : [-21.457866 , 119.528334 ]
};
for (var key in cities) {
var data = cities[key];
var t={ lat: data[0], lng: data[1] };
var marker = new google.maps.Marker({
position: t,
label: key,
map: map
});
}
// Add a marker at the center of the map.
}
您忘记在创建标记时添加标签参数。 请参阅此小提琴以获取参考https://jsfiddle.net/6ou0n2fk/
答案 1 :(得分:-1)
试试这段代码
function addMarker(myLatLng) {
var marker = new google.maps.Marker({
position: myLatLng,
icon: icons['info'].icon,
map: map
});
}
for(i=0; i<data.length; i++){
var myLatLng = {lat: parseFloat(data[i].lat), lng: parseFloat(data[i].lng)};
addMarker(myLatLng);
// features.push([position: new google.maps.LatLng(23.0264288,72.5207211),type: 'info'];
}