【问题标题】:How can i replace FusedLocationApi with FusedLocationProviderClient?如何用 FusedLocationProviderClient 替换 FusedLocationApi?
【发布时间】:2018-11-13 05:46:59
【问题描述】:

我想找到已连接的当前位置。我目前正在使用已弃用的 FusedLocationApi。我必须使用 requestLocationUpdate 方法,但在使用 FusedLocationProviderClient 时出现一些语法错误。我还必须在 onPause 和 onDestroy 中使用 removeLocationupdate。

这是我当前使用 FusedLocationProviderClient 的代码-

@Override
public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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;
    }
   LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleClientApi, mLocationRequest, this);
    if (mCurrentLocMarker == null) {
        Location loc = LocationServices.FusedLocationApi.getLastLocation(mGoogleClientApi);
        mLastLocation = loc;
        if (loc != null) {
            LatLng latLng = new LatLng(loc.getLatitude(), loc.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("Current Location");
            markerOptions.flat(true);
            int height = 150;
            int width = 150;
            BitmapDrawable bitmapDrawable = (BitmapDrawable)getResources().getDrawable(R.drawable.icon_navigation);
            Bitmap bitmap = bitmapDrawable.getBitmap();
            Bitmap marker = Bitmap.createScaledBitmap(bitmap, width, height, false);
            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(marker));
            markerOptions.anchor(0.5f, 0.5f);
            mCurrentLocMarker = mMap.addMarker(markerOptions);
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(latLng)
                    .zoom(17)
                    .build();
            mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }

    }
}
@Override
protected void onPause() {
    super.onPause();
    mapView.onPause();
    String trackid = SharedPreferenceManager.getmInstance(this).getTrackId();
    if (mGoogleClientApi != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleClientApi,this);
    }
    if (!trackid.equals("NO DATA")) {
        startService(new Intent(this, LocationService.class));
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
    String trackid = SharedPreferenceManager.getmInstance(this).getTrackId();
    if (mGoogleClientApi != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleClientApi, this);
    }
    if (!trackid.equals("NO DATA")) {
        startService(new Intent(this, LocationService.class));
    }
}

这是我尝试过但无法实现它的 requestLocationUpdate 和 removeLocationUpdate:

FusedLocationProviderClient fusedLocationProviderClient; //Global variable
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); //initiate in onCreate

【问题讨论】:

    标签: android google-maps location fusedlocationproviderapi


    【解决方案1】:

    你的开始就像你需要的那样。

    FusedLocationProviderClient fusedLocationProviderClient; //Global variable
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); //initiate in onCreate
    

    要从另一个应用程序获取位置作为 googlemaps,您可以使用 mFusedLocationClient.getLastLocation() 它有侦听器,addOnSuccessListener 为您提供位置,addOnCompleteListenertask.getResult() == null 表示您需要执行自己的请求,例如 mFusedLocationClient.requestLocationUpdates(getLocationRequest(), mLocationCallback, null);

    并且您需要删除您的监听器,例如在 onPause 中,例如 mFusedLocationClient.removeLocationUpdates(mLocationCallback)

    你可以看看这个例子https://github.com/googlesamples/android-play-locationhttps://developer.android.com/training/location/receive-location-updates?hl=es

    【讨论】:

    • 是的,我明白了,但我对 mLocationCallback 有点困惑
    • 它的对象 LocationCallback 来自com.google.android.gms.location.LocationCallback;,你需要让它像new LocationCallback(),它让你用locationResult覆盖方法onLocationResult。如果您的locationResult == null 位置服务已关闭,您需要解决它。在其他情况下,您需要通过 locationResult.getLocations() ,您将获得 location.getLatitude() 和 location.getLongitude()
    猜你喜欢
    • 2018-08-31
    • 2019-12-16
    • 1970-01-01
    • 2017-09-05
    • 2021-02-01
    • 2021-06-04
    • 2019-06-08
    • 1970-01-01
    • 2018-08-18
    相关资源
    最近更新 更多