【发布时间】:2013-04-11 19:49:56
【问题描述】:
我有一个名为 value 的数组,当我 console.log(value) 时,我得到了大约 30 行代码,其中包含以下 [6.443663, 3.419248]
数字会随着纬度和经度而变化。但我需要以某种方式摆脱每个方括号。
我尝试了 var replaced = value.toString().replace(/\[.*\]/g,''); 但这将上面的示例更改为 6.443407,3.419035000000008 并且我的代码失败了。
换句话说,我需要帮助摆脱数组中的方括号,以便在我的地图上绘制点。
我的数组由以下代码组成,因为每个纬度和经度值都保存在 onClick 函数内 a 链接中。所以这里的一个成员好心地提供了下面的代码来将我的值放入一个数组中,但现在我正在努力在没有任何括号破坏它的情况下取出这些值。
var obj = {};
$('.choice-location-inner a').each(function(){
var $this = $(this);
var text = $this.attr('onClick').replace('findLocation(\'', '').replace('\');return false;', '');
var array = text.split(',')
obj[this.id]= [parseFloat(array[0]), parseFloat(array[1])];
});
该数组正在尝试使用以下代码在我的 Google 地图上绘制标记
$.each(obj , function(index, value){
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': value}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
icon: image,
position: results[0].geometry.location
});
} else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
wait = true;
setTimeout("wait = false", 1000);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
});
【问题讨论】:
-
什么确切地是
value?一个数组,一个字符串?console.log(typeof value)请。 -
你的意思是把lat/lng值作为字符串存储在数组中,比如array[0] = "[23.021321,18.213123]"?
-
如果
value是数组,那么value.toString()和value.join(',')一样。 -
@RocketHazmat
console.log(typeof value)返回Object -
@DonaldSutherland:那么,您究竟想对阵列做什么?显示吗?
console.log吗?什么? (记住,你的数组是多维的)
标签: javascript jquery google-maps google-maps-api-3