是否可以从onClick
函数获取lat和lon?
<a href="javascript:void(0)" onClick="findLocation('6.443663,3.419248');return false;" id="Lagos">Lagos, Nigeria</a>
<a href="javascript:void(0)" onClick="findLocation('3.15021, 101.707703');return false;" id="Kuala">Kuala Lupar, Malaysia</a>
<a href="javascript:void(0)" onClick="findLocation('24.9952447639, 55.0559356887');return false;" id="Dubai">Dubai, U.A.E.</a>
如果可能的话,我希望将findLocation
函数中的数字放入数组中。
添加了注释
findLocation
函数在地图上绘制我的位置并将焦点放在该位置上。我需要尝试从每个onClick获取lat和lon并以某种方式获取这些以在页面加载时绘制我的点。
function findLocation(address) {
var address = document.forms[0].q.value = address;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
// marker.setMap(null);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
答案 0 :(得分:2)
var obj = {};
$('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])];
});
console.log(obj)
演示:Fiddle