Google maps 在循环中向谷歌地图添加标记不会';行不通
我正在尝试将几个随机标记加载到谷歌地图。当我运行下面的脚本时,它只生成一个在map init函数中引用的标记。我完全被难住了。非区域位于addMarker函数声明之后。我知道循环是通过console.log运行的,但它没有在地图上放置标记Google maps 在循环中向谷歌地图添加标记不会';行不通,google-maps,for-loop,Google Maps,For Loop,我正在尝试将几个随机标记加载到谷歌地图。当我运行下面的脚本时,它只生成一个在map init函数中引用的标记。我完全被难住了。非区域位于addMarker函数声明之后。我知道循环是通过console.log运行的,但它没有在地图上放置标记 var map; var northeast; var southeast; var northwest; var southwest; var markers = []; var lat = (Math.random() * (80 - -80) + -80
var map;
var northeast;
var southeast;
var northwest;
var southwest;
var markers = [];
var lat = (Math.random() * (80 - -80) + -80).toFixed(7) * 1;
var lng = (Math.random() * (170 - -170) + -170).toFixed(7) * 1;
function initMap() {
var randomMarker = {lat: lat, lng: lng};
map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: {lat: 0, lng: 0},
mapTypeId: 'terrain',
disableDefaultUI: true,
zoomControl: false,
scrollwheel: false,
draggable: false
});
// Adds a marker at the center of the map.
addMarker(randomMarker);
}
// Adds a marker to the map and push to the array.
function addMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map,
animation: google.maps.Animation.DROP,
icon: "https://maps.google.com/mapfiles/ms/icons/blue.png",
scaledSize: new google.maps.Size(22, 22)
});
for (var i = 0; i < 10; i++) {
markers.push(marker);
}
}
// Test loop
function testHello() {
for (var i = 0; i < 10; i++) {
console.log("hello world");
}
}
// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Checkbox function for Northeast quadrant
$('#neVisible').change(function() {
// this will contain a reference to the checkbox
if (this.checked) {
setMapOnAll(map);
} else {
setMapOnAll(null);
}
});
// Checkbox function for Southeast quadrant
$('#seVisible').change(function() {
// this will contain a reference to the checkbox
if (this.checked) {
setMapOnAll(map);
} else {
setMapOnAll(null);
}
});
// Checkbox function for Northwest quadrant
$('#nwVisible').change(function() {
// this will contain a reference to the checkbox
if (this.checked) {
setMapOnAll(map);
} else {
setMapOnAll(null);
}
});
// Checkbox function for Southwest quadrant
$('#swVisible').change(function() {
// this will contain a reference to the checkbox
if (this.checked) {
setMapOnAll(map);
} else {
setMapOnAll(null);
}
});
var映射;
var东北部;
var东南部;
西北风;
西南部;
var标记=[];
var lat=(Math.random()*(80--80)+-80).toFixed(7)*1;
var lng=(数学随机()*(170--170)+-170).toFixed(7)*1;
函数initMap(){
var randomMarker={lat:lat,lng:lng};
map=new google.maps.map(document.getElementById('map'){
缩放:2,
中心:{lat:0,lng:0},
mapTypeId:'地形',
disableDefaultUI:true,
动物控制:错误,
滚轮:错误,
可拖动:错误
});
//在地图的中心添加标记。
添加标记(随机标记);
}
//将标记添加到地图并推送到阵列。
功能添加标记(位置){
var marker=new google.maps.marker({
位置:位置,,
地图:地图,
动画:google.maps.animation.DROP,
图标:“https://maps.google.com/mapfiles/ms/icons/blue.png",
scaledSize:新的google.maps.Size(22,22)
});
对于(变量i=0;i<10;i++){
标记器。推(标记器);
}
}
//测试回路
函数testHello(){
对于(变量i=0;i<10;i++){
log(“你好世界”);
}
}
//在阵列中的所有标记上设置贴图。
函数setMapOnAll(映射){
对于(var i=0;i
只从initMap
函数调用一次addMarker
函数
// Adds a marker at the center of the map.
addMarker(randomMarker);
addMarker
功能将只创建一个标记
// Adds a marker to the map and push to the array.
function addMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map,
animation: google.maps.Animation.DROP,
icon: "https://maps.google.com/mapfiles/ms/icons/blue.png",
scaledSize: new google.maps.Size(22, 22)
});
for (var i = 0; i < 10; i++) {
markers.push(marker);
}
}
这将创建一个新标记并将其添加到标记数组中。
编辑initMap函数以创建随机坐标并在这些位置添加标记。尝试缩小地图以同时查看所有标记
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: {lat: 0, lng: 0},
mapTypeId: 'terrain',
disableDefaultUI: true,
zoomControl: false,
scrollwheel: false,
draggable: false
});
for (var i = 0; i < 10; i++) {
var lat = (Math.random() * (80 - -80) + -80).toFixed(7) * 1;
var lng = (Math.random() * (170 - -170) + -170).toFixed(7) * 1;
var randomMarker = {lat: lat, lng: lng};
// Adds a marker at the center of the map.
addMarker(randomMarker);
}
}
函数initMap(){
map=new google.maps.map(document.getElementById('map'){
缩放:2,
中心:{lat:0,lng:0},
mapTypeId:'地形',
disableDefaultUI:true,
动物控制:错误,
滚轮:错误,
可拖动:错误
});
对于(变量i=0;i<10;i++){
var lat=(Math.random()*(80--80)+-80).toFixed(7)*1;
var lng=(数学随机()*(170--170)+-170).toFixed(7)*1;
var randomMarker={lat:lat,lng:lng};
//在地图的中心添加标记。
添加标记(随机标记);
}
}
请只添加代码的相关部分,这样需要额外的努力才能完成所有代码。您似乎多次向地图添加相同的标记,而不是每次都创建一个新标记。对此表示抱歉。你是对的,它多次添加相同的标记,没问题,只是下次你提问时的提示。很高兴我能帮忙。干杯!:)如果你能看看我在底部的评论。我仍然无法让它工作:(.谢谢你的回复。仍然无法让它工作。我添加了(I=0;I<5;I++){addMarker(locations);}和(I=0;I<5;I++){addMarker();}在函数initMap中,没有出现任何标记。如果有任何其他帮助,我们将不胜感激。我已在上面的答案中更新了initMap代码。进行这些更改并重试。
function addMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map,
animation: google.maps.Animation.DROP,
icon: "https://maps.google.com/mapfiles/ms/icons/blue.png",
scaledSize: new google.maps.Size(22, 22)
});
markers.push(marker);
}
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: {lat: 0, lng: 0},
mapTypeId: 'terrain',
disableDefaultUI: true,
zoomControl: false,
scrollwheel: false,
draggable: false
});
for (var i = 0; i < 10; i++) {
var lat = (Math.random() * (80 - -80) + -80).toFixed(7) * 1;
var lng = (Math.random() * (170 - -170) + -170).toFixed(7) * 1;
var randomMarker = {lat: lat, lng: lng};
// Adds a marker at the center of the map.
addMarker(randomMarker);
}
}