gpt4 book ai didi

java - Android LocationServices.FusedLocationApi 已弃用

转载 作者:IT老高 更新时间:2023-10-28 13:14:34 27 4
gpt4 key购买 nike

我不明白为什么 LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, this); "FusedLocationApi"被划掉并指向它说已弃用。 Click here to view Image

import android.location.Location;
import android.location.LocationListener;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MaintainerMapActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener{

private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
Location mLastLocaton;
LocationRequest mLocationRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maintainer_map2);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

@Override
public void onLocationChanged(Location location) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}
}

最佳答案

原答案

发生这种情况是因为 FusedLocationProviderApi 在最新版本的 google play 服务中已弃用。您可以查看here .官方指南现在建议使用 FusedLocationProviderClient .您可以找到详细指南here .

例如在 onCreate()onViewCreated() 中创建一个 FusedLocationProviderClient 实例

Kotlin

val fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireContext())

而要请求最后一个已知位置,您只需调用电话

fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? ->
location?.let { it: Location ->
// Logic to handle location object
} ?: kotlin.run {
// Handle Null case or Request periodic location update https://developer.android.com/training/location/receive-location-updates
}
}

Java

FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireContext());

fusedLocationClient.getLastLocation().addOnSuccessListener(requireActivity(), location -> {
if (location != null) {
// Logic to handle location object
} else {
// Handle null case or Request periodic location update https://developer.android.com/training/location/receive-location-updates
}
});

很简单,不是吗?


重要更新(2017 年 10 月 24 日):

昨天,Google 更新了其官方开发者页面 a warning那就是

Please continue using the FusedLocationProviderApi class and don't migrate to the FusedLocationProviderClient class until Google Play services version 12.0.0 is available, which is expected to ship in early 2018. Using the FusedLocationProviderClient before version 12.0.0 causes the client app to crash when Google Play services is updated on the device. We apologize for any inconvenience this may have caused.

warning所以我认为我们应该继续使用已弃用的 LocationServices.FusedLocationApi,直到 Google 解决了这个问题。


最新更新(2017 年 11 月 21 日):

警告现已消失。 Google Play services 11.6 November 6, 2017, release note说:Fixed FusedLocationProviderClient issue that occasionally caused crashes when Google Play services updated.我认为 Play Services 在后台更新时不会崩溃。所以我们现在可以使用新的FusedLocationProviderClient了。

关于java - Android LocationServices.FusedLocationApi 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46481789/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com