【问题标题】:Google Maps Marker Events谷歌地图标记事件
【发布时间】:2011-06-08 06:02:58
【问题描述】:

我有以下谷歌地图代码。这段代码的目的是显示一个中间有标记的地图。标记是可拖动的,当它被放下时,我需要它给出标记被放下的当前纬度/经度。该事件并不总是触发。在chrome中,如果我只是让代码运行,它就不起作用。但是,如果我通过调试并停止附加事件的代码来减慢它的速度,然后让它继续,它就可以工作。似乎某处存在竞争条件,但我不知道在哪里。有人可以看看,看看你有什么想法。

function initialize() {
        var myOptions = {
            zoom: 10,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var canvas = document.getElementById("MapCanvas");
        var map = new google.maps.Map(canvas, myOptions);

        // Try W3C Geolocation (Preferred)
        if (navigator.geolocation) {
            browserSupportFlag = true;
            navigator.geolocation.getCurrentPosition(function (position) {
                initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                map.setCenter(initialLocation);
                marker = new google.maps.Marker({
                    position: initialLocation,
                    draggable: true,
                    map: map,
                    title: "You are here"
                });  
            }, function () {
                handleNoGeolocation(browserSupportFlag);
            });
            // Try Google Gears Geolocation
        } else if (google.gears) {
            browserSupportFlag = true;
            var geo = google.gears.factory.create('beta.geolocation');
            geo.getCurrentPosition(function (position) {
                initialLocation = new google.maps.LatLng(position.latitude, position.longitude);
                map.setCenter(initialLocation);
                marker = new google.maps.Marker({
                    position: initialLocation,
                    draggable: true,
                    map: map,
                    title: "You are here"
                });  
            }, function () {
                handleNoGeoLocation(browserSupportFlag);
            });
            // Browser doesn't support Geolocation
        } else {
            browserSupportFlag = false;
            handleNoGeolocation(browserSupportFlag);
        }



        google.maps.event.addListener(marker, 'dragend', function () {
            $("#txtLat").val(marker.position.Ia);
            $("#txtLong").val(marker.position.Ja);
        });




    }   

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

    我尚未对此进行测试,但看起来marker 正在getCurrentPosition 函数之一中进行初始化。我猜这两个函数都是异步的,这就是它们采用回调函数的原因。但是,您正在尝试将侦听器函数同步附加到marker - 因此在正常情况下,您可能会尝试在初始化marker 之前将侦听器附加到marker。 (另外,我没有看到markervar 语句,所以除非你把它漏掉,否则marker 在全局命名空间中——可能不是你想要的)。

    解决这个问题的方法是在初始化marker 之后,在回调函数中附加监听器。由于 W3C Geolocation API 和 Gears API 的回调看起来是一样的,所以您可能希望将整个事情放在一个函数中:

    function initialize() {
        var myOptions = {
            zoom: 10,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(canvas, myOptions);
        var marker;
    
        // set up callback
        function initMap(initialLocation) {
            map.setCenter(initialLocation);
            // init marker
            marker = new google.maps.Marker({
                position: initialLocation,
                draggable: true,
                map: map,
                title: "You are here"
            });
            // now attach the event
            google.maps.event.addListener(marker, 'dragend', function () {
                // you know you'd be better off with 
                // marker.getPosition().lat(), right?
                $("#txtLat").val(marker.position.Ia);
                $("#txtLong").val(marker.position.Ja);
            });
        }
    
        // Try W3C Geolocation (Preferred)
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                // call the callback
                initMap(new google.maps.LatLng(
                    position.coords.latitude, position.coords.longitude
                ));
            }, function () {
                // I just assumed you weren't using 
                // browserSupportFlag anywhere else
                handleNoGeolocation(true); 
            });
        // Try Google Gears Geolocation
        } else if (google.gears) {
            var geo = google.gears.factory.create('beta.geolocation');
            geo.getCurrentPosition(function (position) {
                initMap(new google.maps.LatLng(
                    position.latitude, position.longitude
                ));
            }, function () {
                handleNoGeoLocation(true);
            });
        // Browser doesn't support Geolocation
        } else {
            handleNoGeolocation(false);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 2011-09-01
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多