1

我正在构建一个跟踪订单来源以交付包裹的类,我收到一条错误消息,指出FusedLocationProviderApi已弃用,现在看来我必须FusedLocationProviderClient改用,当我申请时,FusedLocationProviderApi我还实现了LocationListenera ConnectionCallbackGoogleaApiClient我想应该将其删除。

我怎么能在这里实现这个

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

    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        requestRuntimePermission();


    }
    else
    {
        if (checkPlayServices())
        {
            buildGoogleApiClient();
            createLocationRequest();
        }
    }
    displayLocation();




}

private void displayLocation() {
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        requestRuntimePermission();


    }
    else
    {
        mLastLocation = LocationServices.fused(null).getLastLocation(mGoogleApiClient);
        if (mLastLocation != null)
        {
            double latitude = mLastLocation.getLatitude();
            double longitude = mLastLocation.getLongitude();

            LatLng yourLocation = new LatLng(latitude,longitude);
            mMap.addMarker(new MarkerOptions().position(yourLocation).title("Tu ubicacion"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(yourLocation));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
        }
        else
        {
            Toast.makeText(this, "No se pudo obtener ubicacion", Toast.LENGTH_SHORT).show();
        }
    }
}
4

1 回答 1

1

最终找到了一种效果更好的新方法

 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());

        }


    }

};
于 2018-04-03T03:22:18.490 回答