【问题标题】:Now that FusedLocationApi is deprecated how can i implement FusedLocationProvideClient when i also have location listener?现在 FusedLocationApi 已被弃用,当我也有位置监听器时,如何实现 FusedLocationProvideClient?
【发布时间】:2018-09-07 14:25:52
【问题描述】:

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

我怎样才能在这里实现这个

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

【问题讨论】:

    标签: locationlistener fusedlocationproviderapi android-fusedlocation fusedlocationproviderclient


    【解决方案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());
    
            }
    
    
        }
    
    };
    

    【讨论】:

      猜你喜欢
      • 2011-12-18
      • 2010-12-20
      • 2018-11-26
      • 2011-08-21
      • 2011-02-02
      • 2019-12-16
      • 2013-07-08
      • 2019-04-01
      • 2012-06-06
      相关资源
      最近更新 更多