2017-01-22 5 views
0

Ich baue eine Anwendung, die Benutzer-Standort verwenden. Manchmal, wenn Benutzer Ausschalten von GPS oder gibt nicht über die Berechtigung für die App Benutzer Standort zu verwenden, in diesem Fall die App-Abstürze aufgrund von NULL-Wert in der Lage variabel.Set Anfangswert für Location in Android

Ich möchte einen Standardwert für Standort einzustellen (die Lat: 0 & Lon: 0)

ich versucht, dieses

Location _location = new Location(_locationManager.GPS_PROVIDER); 

aber _locationManager ist null selbst.

wie kann ich den Anfangswert für die Location?

Dank

Antwort

1

versuchen Sie dies:

Location defaultLocation = new Location("");//provider name is unecessary 
defaultLocation .setLatitude(0.0d);//your lat long 
defaultLocation .setLongitude(0.0d); 

für dynamische Standort Verwendung holen:

locationManager = (LocationManager) context 
      .getSystemService(Context.LOCATION_SERVICE); 


    Criteria locationCritera = new Criteria(); 
    String providerName = locationManager.getBestProvider(locationCritera, 
      true); 
    if(providerName!=null) 
     location = locationManager.getLastKnownLocation(providerName); 

    locationListener = new MyLocationListener(); 

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
      0, locationListener); 

in MyLocationlistener geben Sie Ihre location Objekt

private class MyLocationListener implements LocationListener { 

    public void onLocationChanged(Location loc) { 

     if (loc != null) { 
      location = loc; 

     } 
    } 

    public void onProviderDisabled(String provider) { 

    } 

    public void onProviderEnabled(String provider) { 
    } 

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