我正在尝试使用 Google Maps API 来获取用户提供的位置。为此,我设置了一个基于“点击”事件移动的标记。代码如下:
function initialize_google_map(div_id) {
var map = new GMap2(document.getElementById(div_id));
map.setCenter(new GLatLng(45, -105), 2);
map.setUIToDefault();
return map;
}
$(document).ready(function() {
// configure the google maps api
var map = initialize_google_map("map_canvas");
var marker = google.maps.Marker({map: map, title:"Location"});
google.maps.event.addListener(map, "click", function(evt) {
alert("got click event");
marker.setPosition(evt.latLng);
});
$(document).unload(function() {
// unload the google map
GUnload();
});
});
Run Code Online (Sandbox Code Playgroud)
“得到点击事件”警报永远不会触发,我的 Javascript 控制台(谷歌浏览器)说:
未捕获的类型错误:无法调用未定义的方法“addListener”
API 是这样包含的:
<script src="http://maps.google.com/maps?file=api&v=3&sensor=true" type="text/javascript"></script>