2012-06-13 80 views
0

我有一種註冊偵聽器並請求位置更新的方法。我想在當前方法本身中的位置座標,而不是onLocationChanged方法。如何在方法中獲取GPS座標

我的代碼是: -

myclass { 

    onCreate { 
     location_Manager = (LocationManager) getSystemService (Context.LOCATION_SERVICE); 
     locLstnr = new myLocationlistener(); 
     locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, Loc_listener.MINIMUM_TIME_BETWEEN_UPDATES, Loc_listener.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, locLstnr); 
    } 

    class myLocationListener() { 
     onLocationChanged() { ... } 
     onProviderDisabled() { ... } 
     onProviderEnabled() { ... } 
    } 

} 

我知道我能得到getLastKnownLocation(),但櫃面它不可用,是不是有可能得到的位置呢? Thisthis是一個類似的問題,但不幫助我。我沒有使用onLocationChanged方法的問題,但我需要在onCreate方法中有位置。那可能嗎?一種方法可能是將位置存儲到myLocationListener中的變量並訪問它,但是有沒有其他替代方案或更好的解決方案?

回答

1

我想如果getLastKnownLocation()給出空,那麼你必須要等待回電....

僅僅能做到從link使請求更新

locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener); 

http://devdiscoveries.wordpress.com/2010/02/04/android-use-location-services/

getLastKnownLocation() is faster after a connection has been established with the GPS satellite. For the first time, it will return null, or no value till no connection is established. You can add a GpsListener to know when the location is obtained. Search about "how to get a gps fix" and you might get answer to your question 

and refer link