我在设置html按钮时遇到问题,该按钮必须要求地理位置,显示并将标记放在正确的位置:
首先,我开始加载方法:
google.maps.event.addDomListener(window,'load',init_map);
然后:
function init_map()
{
var lima = new google.maps.LatLng(-12.043333,-77.028333);
var browserSupportFlag = new Boolean();
var mapDiv = document.getElementById('map_canvas');
map = new google.maps.Map(mapDiv,
{
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: lima
});
var distanceWidget = new DistanceWidget(map, initialLocation);
google.maps.event.addListener(distanceWidget, 'position_changed', function()
{
displayInfo(distanceWidget);
}
);
google.maps.event.addDomListener(document.getElementById('get_latlng'), 'click', get_location(map,distanceWidget.get_marker()));
}
HTML:
<button type="button" id="get_latlng">Get your current location</button>
但它的作用是,监听器加载自己,我想要它做的是当我点击按钮时它会执行。
答案 0 :(得分:0)
将调用包装成函数:
google.maps.event
.addDomListener(document.getElementById('get_latlng'),
'click',
function(){get_location(map,distanceWidget.get_marker());});