目前我正在尝试根据用户点击设置地图上的标记。我尝试了所有我能想到的和没有作品的东西。看起来好像我的地图甚至没有检测到点击。目前,我试图让剧本尽可能的简单,我只是想在这点在地图上点击检测:GoogleMaps:设置用户点击标记
<script type="text/javascript">
function initialize()
{
<!-- Set the initial location-->
var latlng = new google.maps.LatLng(44, -71);
<!-- initialization options -->
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.HYBRID
};
<!-- The map variable
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
<!-- END INITIALIZE -->
}
google.maps.event.addDomListener(window, 'load', initialize);
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
}
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
</script>
我已经尝试了这种代码变种非常多,它永远不会工作。如果我将它更改为“addDomListener(window,...)”,它可以工作,但从不使用地图作为侦听器。想法?
编辑:好的,解决它由有些改变功能和在脚本改变其位置:
<script type="text/javascript">
function initialize() {
<!-- Set the initial location -->
var latlng = new google.maps.LatLng(44, -71);
<!-- initialization options -->
var myOptions = {
minZoom: 3,
zoom: 8,
maxZoom: 9,
center: latlng,
mapTypeId: google.maps.MapTypeId.HYBRID
};
<!-- The map variable-->
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
<!-- Add the functionality of placing a marker on a click-->
google.maps.event.addListener(map, 'click', function(event) {
var marker = new google.maps.Marker({
position: event.latLng,
map: map
});
alert('You clicked the map.'+event.latLng);
});
<!-- END INITIALIZE -->
}
google.maps.event.addDomListener(window, 'load', initialize);
<!-- Add the functionality of placing a marker on a click-->
google.maps.event.addListener(map, 'click', function(event) {
var marker = new google.maps.Marker({
position: event.latLng,
map: map
});
alert('You clicked the map.'+event.latLng);
});
</script>
你有没有运行代码,我们可以看看,说明了这个问题?或者我们可以查看所有代码的jsfiddle? – 2012-02-03 22:53:28
顺便说一句,您正在代码的第2行(即clickedLocation)中引用LatLng对象中的LatLng对象。您已经从点击传递LatLng作为locatin变量。 – andresf 2012-02-03 23:33:56
好点andresf和马诺马克,我会更新代码,以包括我所有的脚本 – riqitang 2012-02-04 13:42:09