Google地图:在用户点击时设置标记

时间:2012-02-03 21:49:43

标签: google-maps

目前我正在尝试根据用户点击次数在地图上设置标记。我已经尝试了所有我能想到的并且没有工作的东西。似乎我的地图甚至没有检测到点击。目前,我正在努力使脚本尽可能简单,我只是想在此时检测地图上的点击次数:

<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>

1 个答案:

答案 0 :(得分: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)
{
//...
}