0

Ich habe ein großes Problem bei der Lokalisierung von GoogleApiClient. Ich habe implementieren infront GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener und die LocationListener funktioniert überhaupt nicht. Es scheint, als ob diese Schnittstellen nicht in meiner App registriert sind. Unten ist mein Code. Sehen Sie sich die Methode an: onLocationChanged, onConnectionSuspended, onConnected, onConnectionFailed. In jedem Beispiel habe ich hinzugefügt, um etwas Text von LOG zu drucken, und in keinem Beispiel wurde logs nicht gedruckt.GoogleApiClient in Android kann nicht in der Anwendung registriert werden

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener { 

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

    private GoogleMap mMap; 
    private Location location; 

    ///////////////////////////////////////////// 
    private GoogleApiClient mGoogleApiClient; 
    public static final String TAG = MapsActivity.class.getSimpleName(); 
    private LocationRequest mLocationRequest; 
    private LatLng latLng; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 


     // Create the LocationRequest object 
     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); // 1 second, in milliseconds 


     dataBaseHelper = new DataBaseHelper(getApplicationContext()); 

     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 


     /* some code here */ 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 
    } 


    @Override 
    public void onMapReady(final GoogleMap googleMap) { 
     mMap = googleMap; 


    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
     mGoogleApiClient.connect(); 

    } 

    private void setUpMapIfNeeded() { 

     if (mMap == null) { 
      ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMapAsync(this); 
      if (mMap != null) { 
       //setUpMap(); 
      } 
     } 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (mGoogleApiClient.isConnected()) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
      mGoogleApiClient.disconnect(); 
     } 
    } 


    private void handleNewLocation(Location location) { 
     Log.d(TAG, location.toString()); 

     double currentLatitude = location.getLatitude(); 
     double currentLongitude = location.getLongitude(); 
     final LatLng latLng = new LatLng(currentLatitude, currentLongitude); 
     setLatLng(latLng); 

     MarkerOptions options = new MarkerOptions() 
       .position(latLng) 
       .title("I am here!"); 
     mMap.addMarker(options); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); 

     generateMap(getLatLng(), "3000"); 

     sItems.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       washLocations.clear(); 
       washLocations.size(); 
       String latitude = String.valueOf(latLng.latitude); 
       String longitude = String.valueOf(latLng.longitude); 

       generateMap(getLatLng(), null); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 
      } 
     }); 
    } 


    private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     Log.d("location test", "tes1"); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (location == null) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } else { 
      handleNewLocation(location); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Log.i("faild", "Location services suspended. Please reconnect."); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     if (connectionResult.hasResolution()) { 
      try { 
       // Start an Activity that tries to resolve the error 
       connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 
      } catch (IntentSender.SendIntentException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.i("failds", "Location services connection failed with code " + connectionResult.getErrorCode()); 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.d("location test", "tes2"); 
     handleNewLocation(location); 
    } 
} 

Wie Sie sehen oben:

mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); 

die Karte sollte alle 10 Sekunden neu geladen - so bedeutet dies auch, sollte es Log.d("location test", "tes2"); alle 10 Sekunden drucken - aber es funktioniert nicht.

Und hier ist es meine build.gradle, wo ich alle benötigten libriaries setzen: anwenden Plugin: 'com.android.application'

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 
    defaultConfig { 
     applicationId "com.example.micha.locationtest" 
     minSdkVersion 15 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    useLibrary 'org.apache.http.legacy' 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:25.2.0' 
    compile 'com.google.android.gms:play-services-maps:10.2.0' 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:recyclerview-v7:25.2.0' 
    compile 'com.google.android.gms:play-services-location:10.2.0' 
} 

Antwort

0

Vor allem creat googleApiClient Objekt wie unter Methode

private synchronized void connectToGoolgeAPIClient() { 
    if (mGoogleApiClient == null) { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 
} 

und danach die Position wie unter

if (mGoogleApiClient.isConnected()) { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(2000); 
    mLocationRequest.setFastestInterval(10); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 


if (mLastLocation != null) { 
double lon = mLastLocation.getLongitude(); 
double lat = mLastLocation.getLatitude(); 

} 
+0

Funktioniert nicht. Was ist Utilis? Ich kann es überhaupt nicht importieren ... – bielas

+0

ignorieren, dass util man gerade meinen redigierten Posten sieht –

0

Schließlich habe ich den Fall gelöst, indem ich die Methode folgendermaßen geändert habe:

 @Override 
    public void onConnected(@Nullable Bundle bundle) { 

     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setInterval(10000); 
     mLocationRequest.setFastestInterval(10000); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     handleNewLocation(location); 
    } 
Verwandte Themen