Android 应用程序与 FusedLocationProvider 崩溃



我正在尝试制作一个可以跟踪自行车GPS位置的应用程序。首先,我一直在尝试读取自己手机的GPS位置。每当我尝试启动下面的片段时,我的应用程序都会崩溃。我已经为该应用程序输入了一个谷歌API密钥,并在我的清单文件中放置了" "行。调试此问题的好方法有哪些?我是Android Studio的新手,所以我很想听到任何形式的反馈。

//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;
}
}
}

您需要使用 GoogleApiClient for FusedLocationApi.这是一个工作代码

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

在onCreate((上调用buildGoogleApiClient方法。并实现所需的接口。GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, 位置侦听器

@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版本。您需要在运行时授予权限。在这里查看

@FnR您谈论的是旧版本的 FusedLocationAPI,那么游戏服务版本 11 出现了一个新版本,它应该简单得多,请看这里:https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html

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

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

相关内容

  • 没有找到相关文章

最新更新