如何用FusedLocationProviderClient替换FusedLocationApi? [英] How can i replace FusedLocationApi with FusedLocationProviderClient?

查看:693
本文介绍了如何用FusedLocationProviderClient替换FusedLocationApi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到所连接的当前位置.我当前正在使用不推荐使用的FusedLocationApi.我必须使用requestLocationUpdate方法,但是在使用FusedLocationProviderClient时遇到一些语法错误.我还必须在onPause和onDestroy中使用removeLocationupdate.

I want to find current location on connected. I am currently using FusedLocationApi which is deprecated. i have to use requestLocationUpdate method but i am getting some syntax error while using FusedLocationProviderClient. i also have to use removeLocationupdate in onPause and onDestroy.

这是我当前正在使用FusedLocationProviderClient-

This is my current code which is using 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:

This is i tried but cannot implement its requestLocationUpdate and removeLocationUpdate:

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

推荐答案

您的开始就像您需要的一样.

Your start is like you need.

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

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

For getting location from another app as googlemaps you can use mFusedLocationClient.getLastLocation() it have listeners, addOnSuccessListener gives to you location, addOnCompleteListener and task.getResult() == null means that you need to do your own request like mFusedLocationClient.requestLocationUpdates(getLocationRequest(), mLocationCallback, null);

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

and you need to remove your listener for example in onPause like mFusedLocationClient.removeLocationUpdates(mLocationCallback)

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

you can look this examples https://github.com/googlesamples/android-play-location and https://developer.android.com/training/location/receive-location-updates?hl=es

这篇关于如何用FusedLocationProviderClient替换FusedLocationApi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆