2013-05-09 67 views
1

我正在開發一個AIR for iOS應用程序,並且我剛剛在我要修復的錯誤列表上留下了這一個問題。基本上,當某個窗口打開時,用戶可以使用他們當前的位置進行搜索。我爲此使用Geolocation類,它工作正常,除了StatusEvent.STATUS沒有觸發的事實。系統會提示用戶授予使用其當前位置的權限,但在選擇時,我的StatusEvent處理程序從不會被調用。地理位置狀態事件永不會觸發

if (Geolocation.isSupported) { 
    var geo:Geolocation = new Geolocation(); 
    geo.addEventListener(StatusEvent.STATUS, this.geolocationStatusHandler); 
    this.geolocationStatusHandler(); 
} 

protected function geolocationStatusHandler(e:StatusEvent = null):void{ 
    var geo:Geolocation = new Geolocation(); 
    this.gpsButton.enabled = !geo.muted; 
} 

我能想到的唯一的事情是,警告窗口(其中凍結應用程序執行)與新的地理位置(打開),之前我添加了事件偵聽器。但是,如果是這種情況,手動調用處理程序將不會發生,直到用戶關閉警報(它發生在同一時間,粗略地,斷點在那裏停止應用程序,而警報是打開的)

Is there這是一個解決方案嗎?我總是可以將提示移到應用程序的開頭,但我個人更喜歡在需要之前不會提示。

詳情:

  • 與AIR 3.6 SDK
  • 測試的iPad 2,3,和Mini,內置所有運行iOS 6.1.3
  • 在發佈和調試模式
  • 測試ActionScript移動項目

回答

2

收聽GeolocationEvent.UPDATE事件。

另外,看起來你是在偵聽器之後立即手動調用處理程序的;那麼您的處理程序正在實例化一個新的Geolocation而不是獲取GeolocationEvent的經度和緯度。使用谷歌地理編碼API從XpenseIt教程

實現示例:

package 
{ 
    import flash.events.Event; 
    import flash.events.EventDispatcher; 
    import flash.events.GeolocationEvent; 
    import flash.sensors.Geolocation; 

    import mx.rpc.AsyncResponder; 
    import mx.rpc.AsyncToken; 
    import mx.rpc.events.FaultEvent; 
    import mx.rpc.events.ResultEvent; 
    import mx.rpc.http.HTTPService; 

    [Event(name="locationUpdate", type="flash.events.Event")] 
    public class GeolocationUtil extends EventDispatcher 
    { 
     protected var geo:Geolocation; 
     public var updateCount:int; 
     protected var service:HTTPService = new HTTPService(); 

     public var location:String; 
     public var longitude:Number; 
     public var latitude:Number; 

     public function GeolocationUtil() 
     { 
      service.url = "https://maps.googleapis.com/maps/api/geocode/xml"; 
     } 

     public function geoCodeAddress(address: String):AsyncToken 
     { 
      return service.send({address: address, sensor: Geolocation.isSupported}); 
     } 

     public function getLocation():void 
     { 
      if (Geolocation.isSupported) 
      { 
       geo = new Geolocation(); 
       geo.setRequestedUpdateInterval(500); 
       updateCount = 0; 
       geo.addEventListener(GeolocationEvent.UPDATE, locationUpdateHandler);     
      } 
     } 

     protected function locationUpdateHandler(event:GeolocationEvent):void 
     { 
      // Throw away the first location event because it's almost always the last known location, not current location 
      updateCount++; 
      if (updateCount == 1) return; 

      if (event.horizontalAccuracy <= 150) 
      { 
       trace("lat:" + event.latitude + " long:" + event.longitude + " horizontalAccuracy:" + event.horizontalAccuracy); 
       geo.removeEventListener(GeolocationEvent.UPDATE, locationUpdateHandler); 
       geo = null; 
      } 

      longitude = event.longitude; 
      latitude = event.latitude; 

      var token:AsyncToken = service.send({latlng: latitude+","+longitude, sensor: Geolocation.isSupported}); 
      token.addResponder(new AsyncResponder(
       function(event:ResultEvent, token:AsyncToken):void 
       { 
        // Map the location to city and state from the response address component 
        location = event.result.GeocodeResponse.result[0].address_component[3].long_name + ', '+ event.result.GeocodeResponse.result[0].address_component[5].long_name; 
        dispatchEvent(new Event("locationUpdate")); 
       }, 
       function (event:FaultEvent, token:AsyncToken):void 
       { 
        // fail silently 
        trace("Reverse geocoding error: " + event.fault.faultString); 
       })); 
     } 

    } 
} 
+0

你怎麼知道。創建第二個Geolocation對象覆蓋第一個對象上的偵聽器。切換到一個固定的對象。現在我應該明白這一點。是的,更新僅用於獲取緯度/經度位置,並且在用戶更改位置設置時不會發送。謝謝你指出我的愚蠢。 – 2013-05-09 21:56:31