现在 FusedLocationApi 已被弃用,当我也有位置监听器时,如何实现 FusedLocationProvideClient?

Posted

技术标签:

【中文标题】现在 FusedLocationApi 已被弃用,当我也有位置监听器时,如何实现 FusedLocationProvideClient?【英文标题】:Now that FusedLocationApi is deprecated how can i implement FusedLocationProvideClient when i also have location listener? 【发布时间】: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();
        
    

【问题讨论】:

【参考方案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());

        


    

;

【讨论】:

以上是关于现在 FusedLocationApi 已被弃用,当我也有位置监听器时,如何实现 FusedLocationProvideClient?的主要内容,如果未能解决你的问题,请参考以下文章

现在 Google 网络搜索 API 已被弃用,还都有哪些替代方案? [关闭]

现在 MPMoviePlayerPlaybackDidFinishReasonUserInfoKey 已被弃用,找到播放结束原因的最佳方法是啥?

NSDictionary init(contentsOfFile:) 已被弃用,现在怎么办?

FusedLocationApi 已被交叉或弃用

现在 SSLSocketFactory 在 Android 上已被弃用,处理客户端证书身份验证的最佳方法是啥?

Scala 2.10 - 八进制转义已被弃用 - 现在如何惯用八进制?