2011-06-07 121 views
1

我對Google地圖有以下代碼。此代碼的目的是在中間顯示帶有標記的地圖。標記是可拖動的,當它被放下時,我需要它給出標記被放下的當前緯度/長度。該事件並不總是會發生。在Chrome中,如果我只是讓代碼運行,它不起作用。但是,如果我通過調試並停止附加事件的代碼來減慢它的速度,那麼讓它繼續下去,它就可以工作。似乎某處存在競爭條件,但我不知道在哪裏。有人可以看一看,看看你是否想出了一些東西。Google地圖標記事件

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); 
     }); 




    } 

回答

6

我沒有測試過這一點,但它看起來像正的getCurrentPosition功能之一內初始化。我猜這兩個函數都是異步的,這就是爲什麼他們需要一個回調函數。但是,您試圖將偵聽器函數同步附加到 - 因此在正常情況下,您可能試圖在初始化之前將偵聽器附加到。 (另外,我沒有看到對的聲明,所以除非您將其保留,否則位於全局名稱空間中 - 可能不是您想要的)。

解決此問題的方法是在初始化後,將偵聽程序附加在回調函數中。由於回調看起來與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); 
    } 

}