1

我想制作一个应用程序,可以跟踪我的自行车的GPS位置。首先,我一直在尝试阅读我自己手机的GPS位置。每当我尝试启动下面的片段时,我的应用会一直崩溃。我已经为应用程序添加了一个Google API密钥,并将该行放在我的清单文件中。有什么好的方法来调试这个问题?我是Android Studio的新手,所以我很乐意听到任何反馈。Android应用程序与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; 
     } 
    } 
} 

回答

0

您需要使用GoogleApiClient为FusedLocationApi.Here是一个工作代码上的onCreate(

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

    return; 
} 

呼叫buildGoogleApiClient法)。而实现所需的接口。 GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener的

@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; 

} 

定义此参数

private GoogleApiClient mGoogleApiClient; 
private Location currentLocation; 

顺便说一句,如果您使用的是Android 6.x或7.x的版本。您需要在运行时授予权限。查询here

0

@FnR您正在谈论旧版本的fusedlocationapi,新版本与playservices版本11一起出现,应该会更简单一些,请看这里:https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html

我得到了同样的问题,似乎有新的fusedlocationapi和地图活动之间的冲突。 我能够使用这种新技术在没有地图的空活动中获得当前位置,但在地图活动中使用完全相同的代码会返回空结果。

谷歌甚至没有提供任何与地图的例子,这是疯狂的,因为得到的位置是99%的时间加上地图