【发布时间】:2013-03-31 11:00:58
【问题描述】:
我目前正在研究 Google 地图,现在我想从 API“生成”图钉。但后来我卡住了,因为图钉看起来像这样:
var imageDriver = 'marker.png';
var DriverLatLng = new google.maps.LatLng(location.lat, location.lng);
var driverMarker = new google.maps.Marker({
position: DriverLatLng,
map: map,
icon: imageDriver,
title: drivers[0].name
});
}
我想创建某种函数,它可以“生成”这些带有前缀的变量。检查以下示例:
var imageDriver0;
var DriverLatLng0;
var driverMarker0;
position: DriverLatLng0,
map: map,
icon: imageDriver0,
title: drivers[o].name
这些变量当然必须是唯一的,这样它们才不会“冲突”。我有这个 for 循环,它计算我的 API 中的所有“驱动程序”。我可以以某种方式制作这个 for 循环来创建这些引脚吗? 示例:
driversRealtime();
function driversRealtime() {
setInterval(function () {
var url = "http://blackcab.didair.se/api/drivers";
var lat;
var lon;
var name;
var id;
$.getJSON(url,
function (response) {
for (var i = 0; i < response.drivers.length; i++) //Counts data-elemements inside
lat = response.drivers[i].currentLat;
lon = response.drivers[i].currentLon;
name = response.drivers[i].name;
id = response.drivers[i].profileId;
console.log(lat);
console.log(lon);
console.log(name);
console.log(id);
});
}, 3000); //Delay in milliseconds
}
【问题讨论】:
标签: javascript jquery json google-maps-api-3