Javascript 循环中的函数
我有一张(谷歌)地图,上面有几个标记。这些标记的坐标和描述存储在对象中,标记在循环中创建。现在,我想向每个标记添加一个单击事件,并将“key”值作为链接目标。以下是我的代码片段: 对象:Javascript 循环中的函数,javascript,angularjs,google-maps,Javascript,Angularjs,Google Maps,我有一张(谷歌)地图,上面有几个标记。这些标记的坐标和描述存储在对象中,标记在循环中创建。现在,我想向每个标记添加一个单击事件,并将“key”值作为链接目标。以下是我的代码片段: 对象: $localstorage.setObject('lsStations', { "STA": {lat: '50.93358', lng: '6.57', name: 'Station A'}, "STB": {lat: '50.9332', lng: '6.56690', name: 'Sta
$localstorage.setObject('lsStations', {
"STA": {lat: '50.93358', lng: '6.57', name: 'Station A'},
"STB": {lat: '50.9332', lng: '6.56690', name: 'Station B'},
"STC": {lat: '50.934', lng: '6.566', name: 'Station C'}
});
创建地图和标记:
.controller('GMapsCtrl', function ($scope, $localstorage) {
'use strict';
// initialize variables
var map,
myLatLng = new google.maps.LatLng(50.93358, 6.56692),
stationCoords = $localstorage.getObject('lsStations');
$scope.init = function () {
var mapOptions = {
zoom: 16,
// default center
center: myLatLng,
// disable google maps ui
disableDefaultUI: 1
},
map = new google.maps.Map(document.getElementById('map'), mapOptions),
marker = [],
index,
key;
for (key in stationCoords) {
if (stationCoords.hasOwnProperty(key)) {
// add marker for station
marker[key] = new google.maps.Marker({
position: new google.maps.LatLng(stationCoords[key].lat, stationCoords[key].lng),
map: map,
title: stationCoords[key].name
});
// add click event for each marker
google.maps.event.addListener(marker[key], 'click', function () {
$scope.startStation = key;
alert(key);
}
)}
}
};
$scope.init();
});
当我点击任何标记时,第一个标记的警报文本为“STC”,而不是“STA”,第二个标记的警报文本为“STB”,第三个标记的警报文本为“STC”。因此,我尝试将函数代码置于循环之外:
.controller('GMapsCtrl', function ($scope, $localstorage) {
'use strict';
// initialize variables
var map,
myLatLng = new google.maps.LatLng(50.93358, 6.56692),
stationCoords = $localstorage.getObject('lsStations');
$scope.init = function () {
var mapOptions = {
zoom: 16,
// default center
center: myLatLng,
// disable google maps ui
disableDefaultUI: 1
},
map = new google.maps.Map(document.getElementById('map'), mapOptions),
marker = [],
index,
key;
function createMarker() {
$scope.startStation = key;
alert(key);
//window.location.href = '#/station/' + key;
}
for (key in stationCoords) {
if (stationCoords.hasOwnProperty(key)) {
// add marker for station
marker[key] = new google.maps.Marker({
position: new google.maps.LatLng(stationCoords[key].lat, stationCoords[key].lng),
map: map,
title: stationCoords[key].name
});
// add click event for each marker
google.maps.event.addListener(marker[key], 'click', createMarker(key));
}
}
};
$scope.init();
});
通过该代码,警报对话获得了正确的文本(“STA”、“STB”和“STC”)。但当页面加载时,它们都会被触发,如果我点击标记,则不会发生任何事情
如何为每个标记分配具有不同键值的单击事件
谢谢 你可以使用闭包(这是基于发布建议然后将其删除的人的想法)
尝试在单击功能中进行此更改。这对我有用
google.maps.event.addListener(标记[key],'click',函数(){
对于(var j=0;j我稍微更改了Aqib Mapari的代码片段,现在它可以工作了:
google.maps.event.addListener(标记[key],'click',函数(){
var-this标记;
用于(标记中的此标记){
if(标记[thisMarker]==此){
//链接到所选标记的详细信息页面
window.location.href='#/station/'+thisMarker;
}
}
})实际上,我认为可能有一种更简单的方法——我认为你已经有了闭包,但只是没有正确地使用它
function createMarker(key) {
// ^^^
$scope.startStation = key;
alert(key);
//window.location.href = '#/station/' + key;
}
你能应用我的答案吗?Sry,我没能让你的代码正常工作,也许我没有正确理解。这似乎是一个非常干净的解决方案。不过,你的答案帮助我理解了我的问题-非常感谢!
function createMarker(key) {
// ^^^
$scope.startStation = key;
alert(key);
//window.location.href = '#/station/' + key;
}