1

Ich versuche, eine App zu machen, die den GPS-Standort meines Fahrrades verfolgen kann. Um zu beginnen, habe ich versucht, die GPS-Position meines eigenen Telefons zu lesen. Meine App stürzt immer ab, wenn ich versuche, das folgende Fragment zu starten. Ich habe einen Google API-Schlüssel für die App eingegeben und die Zeile "" in meine Manifest-Datei eingefügt. Was sind einige gute Möglichkeiten, dieses Problem zu debuggen? Ich bin neu in Android Studio, also würde ich gerne jede Art von Feedback hören.Android App Absturz mit FusedLocationProvider

//FindBikeFragment.java 

import android.Manifest; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.google.android.gms.location.FusedLocationProviderClient; 
import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 


/** 
* A simple {@link Fragment} subclass. 
*/ 
public class FindBikeFragment extends Fragment implements OnMapReadyCallback { 

    /** 
    * GoogleMaps object 
    */ 
    private GoogleMap mMap; 
    /** 
    * Provides the entry point to the Fused Location Provider API. 
    */ 
    private FusedLocationProviderClient mFusedLocationClient; 
    /** 
    * Represents a geographical location. 
    */ 
    protected Location mLastLocation; 


    public FindBikeFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.fragment_find_bike, container, false); 
     return v; 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map1); 
     mapFragment.getMapAsync(this); 
    } 


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

     mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity()); 

     if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      mLastLocation = mFusedLocationClient.getLastLocation().getResult(); 
      LatLng pp = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()); 
      MarkerOptions option = new MarkerOptions(); 
      option.position(pp).title("Some City"); 
      mMap.addMarker(option); 
      mMap.moveCamera(CameraUpdateFactory.newLatLng(pp)); 
      return; 
     } 
    } 
} 

Antwort

0

Sie müssen GoogleApiClient für FusedLocationApi.Here verwenden, ist ein Arbeitscode

protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build();} 

    return; 
} 

Anruf buildGoogleApiClient Methode auf onCreate(). Und erforderlichen Schnittstellen implementieren. GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, Location

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 


     return; 
    } 
    Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
      mGoogleApiClient); 
    if (mLastLocation != null) { 
     //place marker at current position 
     // Log.e("x", mLastLocation.getLongitude() + ""); 
     //Log.e("y", mLastLocation.getLatitude() + ""); 
     currentLocation = mLastLocation; 


    } 

    LocationRequest mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(5000); //5 seconds 
    mLocationRequest.setFastestInterval(5000); //3 seconds 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
    mLocationRequest.setSmallestDisplacement(50F); //1/10 meter 

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

@Override 
public void onLocationChanged(Location location) { 

    currentLocation = location; 

} 

Und definieren diese Parameter

private GoogleApiClient mGoogleApiClient; 
private Location currentLocation; 

Btw, wenn Sie Android 6.x oder 7.x-Version verwenden. Sie müssen Erlaubnis zur Laufzeit gewähren. Überprüfen Sie here

0

@FnR Sie sprechen über die alte Version des fusionierten locationapi, ein neues kam mit der Version 11 der playservices heraus, es soll viel einfacher sein, schauen Sie hier : https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html

Ich habe das gleiche Problem, da scheint es einen Konflikt zwischen den neuen fusionlocationapi und die Kartenaktivitäten. Ich kann meinen aktuellen Standort mit dieser neuen Technik in einer leeren Aktivität ohne Map abrufen, aber der exakt gleiche Code, der in einer Map-Aktivität verwendet wird, gibt ein Null-Ergebnis zurück.

Google hat nicht einmal ein Beispiel dafür mit einer Karte zur Verfügung gestellt, die verrückt ist, da die Lage zu 99% der Zeit mit einer Karte

gekoppelt ist