谷歌地图 - 地图拖动不发布的问题

时间:2011-01-02 19:45:05

标签: javascript google-maps

我有一个谷歌地图,释放鼠标时没有被释放。当我单击鼠标时,拖动地图并释放单击,地图仍然用鼠标拖动。当用户试图放置标记时,这会导致一些奇怪的定位。这是我的代码:

    var initialLocation;
    var siberia = new google.maps.LatLng(44.9111410149792, -93.0537195);
    var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
    var browserSupportFlag =  new Boolean();



    function initialize() {
        var myOptions = {
            zoom: 6,
            mapTypeId: google.maps.MapTypeId.HYBRID
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        myListener = google.maps.event.addListener(map, 'click', function(event) {
            placeMarker(event.latLng);
            google.maps.event.removeListener(myListener);
        });
        google.maps.event.addListener(map, 'drag', function(event) {
            placeMarker(event.latLng);
            google.maps.event.removeListener(myListener);
        });

        // 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);
            }, 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);
            }, function() {
                handleNoGeoLocation(browserSupportFlag);
            });
            // Browser doesn't support Geolocation
        } else {
            browserSupportFlag = false;
            handleNoGeolocation(browserSupportFlag);
        }

        function handleNoGeolocation(errorFlag) {
            if (errorFlag === true) {
                alert("Geolocation service failed.");
                initialLocation = newyork;
            } else {
                alert("Your browser doesn't support geolocation. We've placed you in beautiful Minneapolis.");
                initialLocation = siberia;
            }
                map.setCenter(initialLocation);

        }

        function placeMarker(location) {
            var marker = new google.maps.Marker({
                position: location,
                map: map,
                draggable: true
            });
            map.setCenter(location);
            var markerPosition = marker.getPosition();
            populateInputs(markerPosition);
            google.maps.event.addListener(marker, "drag", function (mEvent) {
                populateInputs(mEvent.latLng);
            });
        }
        function populateInputs(pos) {
            document.getElementById("t1").value=pos.lat()
            document.getElementById("t2").value=pos.lng();
        }
    }

2 个答案:

答案 0 :(得分:2)

删除脚本的这一部分。它对我有用:

google.maps.event.addListener(map, 'drag', function(event) {
    placeMarker(event.latLng);
    google.maps.event.removeListener(myListener);
});

答案 1 :(得分:1)

当您说“正在用鼠标拖动”时,目前尚不清楚它是整个地图还是您正在谈论的标记。 据我所知,你的解决方案只适用于标记。

下面的代码显示了如何设置draggable属性,可以是true或false。 true表示可拖动的错误意味着没有。

function placeMarker(location) {
            var marker = new google.maps.Marker({
                position: location,
                map: map,
                draggable: true
            });