2014-03-05 39 views
-1

我有以下位置服務:我如何從android位置服務訪問緯度和經度等位置數據?

GPSService.java:

public class GPSService extends SensorElement { 

    private static final String TAG = "GPSServive"; 
    private LocationManager mLocationManager = null; 
    private static final int LOCATION_INTERVAL = 1000; 
    private static final float LOCATION_DISTANCE = 10f; 

    private class LocationListener implements android.location.LocationListener { 
     Location mLastLocation; 

     public LocationListener(String provider) { 
      Log.e(TAG, "LocationListener " + provider); 
      mLastLocation = new Location(provider); 
     } 

     @Override 
     public void onLocationChanged(Location location) { 
      Log.e(TAG, "onLocationChanged: " + location); 
      mLastLocation.set(location); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      Log.e(TAG, "onProviderDisabled: " + provider); 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      Log.e(TAG, "onProviderEnabled: " + provider); 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      Log.e(TAG, "onStatusChanged: " + provider); 
     } 
    } 

    LocationListener[] mLocationListeners = new LocationListener[] { 
      new LocationListener(LocationManager.GPS_PROVIDER), 
      new LocationListener(LocationManager.NETWORK_PROVIDER) }; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.e(TAG, "onStartCommand"); 
     super.onStartCommand(intent, flags, startId); 
     return START_STICKY; 
    } 

    @Override 
    public void onCreate() { 
     Log.e(TAG, "onCreate"); 
     initializeLocationManager(); 
     try { 
      mLocationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, 
        LOCATION_DISTANCE, mLocationListeners[1]); 
     } catch (java.lang.SecurityException ex) { 
      Log.i(TAG, "fail to request location update, ignore", ex); 
     } catch (IllegalArgumentException ex) { 
      Log.d(TAG, "network provider does not exist, " + ex.getMessage()); 
     } 
     try { 
      mLocationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, 
        LOCATION_DISTANCE, mLocationListeners[0]); 
     } catch (java.lang.SecurityException ex) { 
      Log.i(TAG, "fail to request location update, ignore", ex); 
     } catch (IllegalArgumentException ex) { 
      Log.d(TAG, "gps provider does not exist " + ex.getMessage()); 
     } 
    } 

    @Override 
    public void onDestroy() { 
     Log.e(TAG, "onDestroy"); 
     super.onDestroy(); 
     if (mLocationManager != null) { 
      for (int i = 0; i < mLocationListeners.length; i++) { 
       try { 
        mLocationManager.removeUpdates(mLocationListeners[i]); 
       } catch (Exception ex) { 
        Log.i(TAG, "fail to remove location listners, ignore", ex); 
       } 
      } 
     } 
    } 

    private void initializeLocationManager() { 
     Log.e(TAG, "initializeLocationManager"); 
     if (mLocationManager == null) { 
      mLocationManager = (LocationManager) getApplicationContext() 
        .getSystemService(Context.LOCATION_SERVICE); 
     } 
    } 
} 

我怎麼可以在Android活動可以訪問地點的緯度和經度?

回答

0

我用谷歌播放服務的位置客戶端。

它是融合Wi-Fi和gps的最新和準確的位置。

縮小規模是我需要谷歌播放服務來運行它,但舊版本不再無錯誤有時只是開始提供空位置和智能手機重新啓動是必要的。

0

它位於Location對象中,請參閱Location docs。你可以這樣做:

@Override 
public void onLocationChanged(Location location) { 
     Log.e(TAG, "onLocationChanged: " + location); 
     mLastLocation.set(location); 
     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 
} 
+0

但這是否意味着該活動必須實現LocationListener?或者你是說我把它放在服務內部並創建訪問它的方法,然後在活動中調用它們? – JoaoFilipeClementeMartins

+0

把它放在你的活動中也是一個解決方案。這取決於你想達到什麼。例如,Google Maps會在Activity的onPause()中刪除GPS更新,在這種情況下 - 您的活動可以控制偵聽器的生命週期。如果您仍希望在服務中使用它,則可以使用聯編程序(onBind)並將位置數據從服務發送到您的活動。 – Mark

+0

你可以使用活頁夾提供一個android活動的特定實現嗎? – JoaoFilipeClementeMartins