现在不推荐使用FusedLocationApi,当我还有位置监听器时,如何实现FusedLocationProvideClient?

时间:2018-03-28 19:07:30

标签: locationlistener fusedlocationproviderapi android-fusedlocation fusedlocationproviderclient

我正在构建一个跟踪订单来源以传递包的类,我收到一条错误消息,指出FusedLocationProviderApi已被弃用,现在看来我必须使用FusedLocationProviderClient代替,当我申请{ {1}}我还实施了一个FusedLocationProviderApi一个LocationListenerConnectionCallback,我认为应该删除它。

我怎么能在这里实现呢

GoogleaApiClient

1 个答案:

答案 0 :(得分:0)

结束了一种更精细的新方法

 protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tracking_order);

    mService = Common.geoCodeService();

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    mapFrag = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
    mapFrag.getMapAsync(this);
}

   public void onMapReady(GoogleMap googleMap)
{
    mMap=googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(120000); // two minute interval
    mLocationRequest.setFastestInterval(120000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            //Location Permission already granted
            mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
            mMap.setMyLocationEnabled(true);
        } else {
            //Request Location Permission
            checkLocationPermission();
        }
    }
    else {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
        mMap.setMyLocationEnabled(true);
    }
}

LocationCallback mLocationCallback = new LocationCallback(){
    @Override
    public void onLocationResult(LocationResult locationResult) {
        for (Location location : locationResult.getLocations()) {
            Log.i("MapsActivity", "Location: " + location.getLatitude() + " " + location.getLongitude());
            mLastLocation = location;
            if (mCurrLocationMarker != null) {
                mCurrLocationMarker.remove();
            }

            //Place current location marker
            LatLng yourLocation = new LatLng(location.getLatitude(), location.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(yourLocation);
            markerOptions.title("Current Position");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            mCurrLocationMarker = mMap.addMarker(markerOptions);

            //move map camera
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(yourLocation, 11));

            //after add marker for your location add marker for this order

            drawRoute(yourLocation,Common.currentRequest.getAddress());

        }


    }

};