2017-12-15 5 views
0

Ich muss eine Funktion implementieren, die die aktuelle Geo-Position des Benutzers mit einem der alle verfügbaren Sensor (z. B. WLAN, GPS, Daten usw.) zurückgibt.Fused Location Issue - Geo-Lokalisierung funktioniert nicht

Ich suchte nach Informationen darüber und ich fand mehrere Lösungen, die eine, die mich derzeit interessiert, ist die Verwendung der "fusionierten Stelle", aber ich fand keine Anleitung oder ein komplettes Anwendungsbeispiel.

Dies ist der Code, den ich zur Zeit verwenden:

public class LocationService extends Service { 

private FusedLocationProviderClient mFusedLocationClient; 
private SettingsClient mSettingsClient; 
private LocationRequest mLocationRequest; 
private LocationSettingsRequest mLocationSettingsRequest; 
private LocationCallback mLocationCallback; 
private Location mCurrentLocation; 
private Boolean mRequestingLocationUpdates; 
private WriteInLogFile wil = new WriteInLogFile(); 

@Override 
public void onCreate() { 
    try { 
     DbGest db = DbGest.getInstance(getApplicationContext()); 
     String deviceType = db.getSetting("deviceType"); 

     long updateInterval; 
     if (deviceType.compareToIgnoreCase("smartphone") == 0) { 
      updateInterval = Long.parseLong(db.getSetting("pollGPSPhone")); 
     } else { 
      updateInterval = Long.parseLong(db.getSetting("pollGPSTablet")); 
     } 

     long updateInMillisecond = updateInterval/2; 

     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(updateInterval); 
     mLocationRequest.setFastestInterval(updateInMillisecond); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 


     mRequestingLocationUpdates = false; 
     mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); 
     mSettingsClient = LocationServices.getSettingsClient(this); 

     mLocationCallback = new LocationCallback() { 
      @Override 
      public void onLocationResult(LocationResult locationResult) { 
       super.onLocationResult(locationResult); 
       mCurrentLocation = locationResult.getLastLocation(); 
       updateLocation(); 
      } 
     }; 


     LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); 
     builder.addLocationRequest(mLocationRequest); 
     builder.setNeedBle(true); 
     mLocationSettingsRequest = builder.build(); 
     startLocationUpdates(); 
    } catch (Exception e) { 
     wil.WriteFile("1)LocationService - Exception: " + e.toString()); 
    } 
} 


public void startLocationUpdates() { 
    if (!mRequestingLocationUpdates) { 
     mRequestingLocationUpdates = true; 
     mSettingsClient.checkLocationSettings(mLocationSettingsRequest).addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() { 
      @Override 
      public void onSuccess(LocationSettingsResponse locationSettingsResponse) { 
       if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper()); 
       } 
      } 
     }).addOnFailureListener(new OnFailureListener() { 
      @Override 
      public void onFailure(@NonNull Exception e) { 
       wil.WriteFile("2)LocationService - Exception: " + e.toString()); 
      } 
     }); 
    } 
} 


private void stopLocationUpdates() { 
    if (!mRequestingLocationUpdates) { 
     return; 
    } 
    mFusedLocationClient.removeLocationUpdates(mLocationCallback).addOnCompleteListener(new OnCompleteListener<Void>() { 
     @Override 
     public void onComplete(@NonNull Task<Void> task) { 
      mRequestingLocationUpdates = false; 
     } 
    }); 
} 


private void updateLocation() { 
    if (mCurrentLocation != null) { 
    DbGest.getInstance(getApplicationContext()).insertIntoLastPositionKnown(mCurrentLocation, getApplicationContext()); 
    } 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    stopLocationUpdates(); 
} 

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

Der Code arbeiten, wenn ich über Wi-Fi mit dem Netzwerk in diesem Fall alle arbeiten perfekt verbunden bin, aber wenn ich in 3g einen Fehler wechseln occours:

com.google.android.gms.common.api.ResolvableApiException: 6: RESOLUTION_REQUIRED

In gradle:

compile 'com.google.android.gms:play-services-gcm:11.6.2' 
compile 'com.google.android.gms:play-services-vision:11.6.2' 
compile 'com.google.android.gms:play-services-location:11.6.2' 
compile 'com.google.android.gms:play-services-maps:11.6.2' 

Ich habe keine Ahnung, dieses Problem zu lösen ...

Antwort

1

Erste Lage könnte einige Zeit bekommen, und ich würde vorschlagen, es im Hintergrund Handhabung würde die Erfahrung glatter. Überprüfen Sie, ob Sie die Berechtigung erhalten haben und die Berechtigung in Ihrer App aktiviert wurde. Bitte senden Sie das Protokoll, wenn Sie weitere Hilfe benötigen. Ich habe diesen Code ausprobiert und funktioniert gut!

private void getUserLocation() { 
    FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
     mFusedLocationClient.getLastLocation() 
       .addOnSuccessListener(new OnSuccessListener<Location>() { 
        @Override 
        public void onSuccess(Location location) { 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       }); 
    } 
} 
+0

Ich habe bereits gebucht den Inhalt meiner Log: com.google.android.gms.common.api.ResolvableApiException: 6: RESOLUTION_REQUIRED. Alle Genehmigungen sind erteilt. Der Code ist in einem Dienst implementiert, der im Hintergrund ausgeführt wird. – Max

+0

Probieren Sie den obigen Code! – Racer

+0

Mit Ihrem Code arbeiten, verstehe ich nicht den Unterschied zwischen Ihrem und meinem Code, können Sie mir erklären, warum in diesem Fall arbeiten? – Max

Verwandte Themen