2012-02-03 51 views
0

目前我正在尝试根据用户点击设置地图上的标记。我尝试了所有我能想到的和没有作品的东西。看起来好像我的地图甚至没有检测到点击。目前,我试图让剧本尽可能的简单,我只是想在这点在地图上点击检测: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> 
+0

你有没有运行代码,我们可以看看,说明了这个问题?或者我们可以查看所有代码的jsfiddle? – 2012-02-03 22:53:28

+0

顺便说一句,您正在代码的第2行(即clickedLocation)中引用LatLng对象中的LatLng对象。您已经从点击传递LatLng作为locatin变量。 – andresf 2012-02-03 23:33:56

+0

好点andresf和马诺马克,我会更新代码,以包括我所有的脚本 – riqitang 2012-02-04 13:42:09

回答

1

我猜你放在不适当的地方这个代码(或做在了错误的时间)。最近我遇到了同样的问题,需要一些时间来解决它。看看我的作品:

var mapservice = {}; 

mapservice.map = {}; 
mapservice.options = {}; 
mapservice.putMarker = {}; 

mapservice.init = function (divName, textSearch) 
{ 
    this.options = 
    { 
     center: new google.maps.LatLng(-34.397, 150.644), 
     zoom: 8, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    this.map = new google.maps.Map($(divName)[0], this.options); 

    google.maps.event.addListener(this.map, 'click', function (event) 
    { 
     mapservice.putMarker(event.latLng); 
    }); 
    } 
mapservice.putMarker = function (location) 
{ 
//... 
} 
+0

感谢您的答复,这个骨架代码(编辑,以适应我的目的)可悲地没有工作:( – riqitang 2012-02-04 13:37:21