2013-05-09 24 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