【发布时间】:2013-05-14 04:55:55
【问题描述】:
我在尝试使用存储在数组中的信息将多个标记循环到地图上时遇到了一些麻烦。
代码创建地图没问题,但它没有显示我试图循环出来的标记...
从下面的代码可以看出,有两个函数正在创建标记。第一个是简单地使用两个值。此标记显示正常。
然而,第二个功能是从一个数组中获取数据(该数组已被设置为按 Google 地图要求的顺序将纬度和经度数据“挤压”在一起)并且不显示任何内容运行时。
有什么想法吗?我被难住了!
提前致谢!
代码如下:
初始“位置”数组:
var locations = new Array();
for (var i = 0; i < data.length; i++)
{
var row = data[i];
var longitude = row[0];
var latitude = row[1];
locations[i] = latitude + longitude;
}
callMap(locations, locationFilename, userLatitude, userLongitude);
谷歌地图功能
function callMap(locations, locationFilename, userLatitude, userLongitude) {
var mapOptions = {
zoom:16,
center: new google.maps.LatLng(userLatitude, userLongitude),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('mapView'),mapOptions);
setMarkers(map, locations, locationFilename);
currentPosition(map, userLatitude, userLongitude);
}
function currentPosition(map, userLatitude, userLongitude)
{
var userLatLng = new google.maps.LatLng(userLatitude, userLongitude);
var marker = new google.maps.Marker({
position: userLatLng,
map: map
});
}
function setMarkers(map, locations, locationFilename) {
for (var i = 0; i < locations.length; i++) {
var markerLatLng = new google.maps.LatLng(locations[i]);
var marker = new google.maps.Marker({
position: markerLatLng,
map: map
});
}
}
【问题讨论】:
-
我认为位置最终不会包含正确的 LatLng 对象,或者任何您可以从中初始化 LatLng 的东西...
-
感谢您的回复。怎么样?
-
不知何故,我认为纬度+经度无法满足您的需求。检查一下,并查看 LatLng 的文档。具体来说,它的构造函数。
-
更具体地说,我怀疑位置将包含字符串或数字(取决于您的数据的样子,您没有指定)并且 LatLng 至少需要两个数字来初始化纬度和经度。