2015-09-24 61 views
-1

我已經實現了一個使用GoogleApiClient的Android應用程序,它在每5米位移後發送位置更新並且工作正常。如何獲取GPS不可用時的經緯度?

考慮有時不會得到Gps Signal。在這種情況下,我們如何得到更新,任何人都可以請幫助我如何處理這種情況?

回答

0

試試這個,

  Location nwLocation = appLocationService 
         .getLocation(LocationManager.NETWORK_PROVIDER); 
       if (nwLocation != null) { 
        double latitude = nwLocation.getLatitude(); 
        double longitude = nwLocation.getLongitude(); 
        Toast.makeText(
          getApplicationContext(), 
          "Mobile Location (NW): \nLatitude: " + latitude 
            + "\nLongitude: " + longitude, 
          Toast.LENGTH_LONG).show(); 
       } 

完整的教程可以發現here

0

請看看這個link

,我用這個代碼Lat Long網獲取:

public void getCurruntLocation() { 
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    LocationListener ll = new mylocationlistener(); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 
} 

private class mylocationlistener implements LocationListener { 
    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
      Log.d("LOCATION CHANGED", location.getLatitude() + ""); 
      Log.d("LOCATION CHANGED", location.getLongitude() + ""); 
      latLng = location.getLatitude() + "," + location.getLongitude(); 
      Toast.makeText(RecentActivity.this, 
        location.getLatitude() + "" + location.getLongitude(), 
        Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
} 

,並要獲取簡單的通話getCurruntLocation()

0

GoogleApiClient使用LocationRequest類來設置多個參數。其中之一 - .setPrioryty。如果您在某些情況下使用LocationRequest.PRIORITY_HIGH_ACCURACY,由於缺少GPRS座標的準確性,因此googleApiClient不會調用onLocationChanged()而不使用gps。

0

試試下面的代碼,

創建appLocationService對象

AppLocationService appLocationService; 

appLocationService = new AppLocationService(
       AndroidLocationActivity.this); 

Location nwLocation = appLocationService 
         .getLocation(LocationManager.NETWORK_PROVIDER); 

       if (nwLocation != null) { 
        double latitude = nwLocation.getLatitude(); 
        double longitude = nwLocation.getLongitude(); 
        Toast.makeText(
          getApplicationContext(), 
          "Mobile Location (NW): \nLatitude: " + latitude 
            + "\nLongitude: " + longitude, 
          Toast.LENGTH_LONG).show(); 
       } else { 
        showSettingsAlert("NETWORK"); 
       } 

和你AppLocationService.java跟隨。

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 

public class AppLocationService extends Service implements LocationListener { 

    protected LocationManager locationManager; 
    Location location; 

    private static final long MIN_DISTANCE_FOR_UPDATE = 10; 
    private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2; 

    public AppLocationService(Context context) { 
     locationManager = (LocationManager) context 
       .getSystemService(LOCATION_SERVICE); 
    } 

    public Location getLocation(String provider) { 
     if (locationManager.isProviderEnabled(provider)) { 
      locationManager.requestLocationUpdates(provider, 
        MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this); 
      if (locationManager != null) { 
       location = locationManager.getLastKnownLocation(provider); 
       return location; 
      } 
     } 
     return null; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 

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

} 

和也的manifest.xml FIL添加下列權限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />