首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用FusedLocationProviderClient替换FusedLocationApi?

如何用FusedLocationProviderClient替换FusedLocationApi?
EN

Stack Overflow用户
提问于 2018-11-13 13:47:00
回答 1查看 3.2K关注 0票数 0

我想在connected上找到当前位置。我目前使用的是FusedLocationApi,它已被弃用。我必须使用requestLocationUpdate方法,但我在使用FusedLocationProviderClient时遇到一些语法错误。我还必须在onPause和onDestroy中使用removeLocationupdate。

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

代码语言:javascript
复制
@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:

代码语言:javascript
复制
FusedLocationProviderClient fusedLocationProviderClient; //Global variable
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); //initiate in onCreate
EN

回答 1

Stack Overflow用户

发布于 2018-11-13 16:23:16

你的开始就是你所需要的。

代码语言:javascript
复制
FusedLocationProviderClient fusedLocationProviderClient; //Global variable
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); //initiate in onCreate

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

你需要删除你的监听器,例如在像mFusedLocationClient.removeLocationUpdates(mLocationCallback)这样的onPause中

您可以查看此示例https://github.com/googlesamples/android-play-locationhttps://developer.android.com/training/location/receive-location-updates?hl=es

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53274574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档