Android Studio-GoogleAPIClient已弃用的修复程序



我正在构建一个GPS应用程序,但GoogleAPIClient已被弃用,我不知道如何修复它。此外,当我运行该应用程序时,我会收到一个登录按钮,该按钮不会导致任何结果,这可能与此有关,因为我知道GoogleSignInClient存在。此外,如果你能把我链接到GoogleApi的更详细的解释,我真的很感激!

public static final String MA = "MainActivity";
private final static int REQUEST_CODE = 100;
private GoogleApiClient gac;
private Location location;
private TextView locationTV;
private TextView distanceTV;
private TextView addressET;
private TextView timeLeftTV;
private String destinationAddress = "";
private TravelManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationTV = (TextView) findViewById(R.id.location_tv);
manager = new TravelManager();
addressET = (EditText) findViewById(R.id.destination_et);
distanceTV = (TextView) findViewById(R.id.distance_tv);
timeLeftTV = (TextView) findViewById(R.id.time_left_tv);

gac = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
}
public void updateTrip(View v) {
String address = addressET.getText().toString();
boolean goodGeoCoding = true;
if (!address.equals(destinationAddress)) {
destinationAddress = address;
Geocoder geocoder = new Geocoder(this);
try {
List<Address> addresses = geocoder.getFromLocationName(destinationAddress, 5);
if (address != null) {
double latitude = addresses.get(0).getLatitude();
double longitude = addresses.get(0).getLongitude();
Location destinationLocation = new Location("destination");
destinationLocation.setLatitude(latitude);
destinationLocation.setLongitude(longitude);
manager.setDestination(destinationLocation);
}
} catch (IOException ioException) {
goodGeoCoding = false;
}
}
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
//    ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
//                                          int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location current = fusedLocationProviderApi.getLastLocation(gac);
if (current != null && goodGeoCoding) {
distanceTV.setText(manager.milesToDestination(current));
timeLeftTV.setText(manager.timeToDestination(current));
}
}
public void displayLocation() {
FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
//    ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
//                                          int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
location = fusedLocationProviderApi.getLastLocation(gac);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
locationTV.setText(latitude + ", " + longitude);
Log.w(MA, "latutude = " + latitude + "; longitude = " + longitude);
}else
locationTV.setText("Error locating the device");
}
public void onConnected(Bundle hint) {
Log.w(MA, "connected");
displayLocation();
}
public void onConnectionSuspended(int cause) {
Log.w(MA, "connection suspended");
}
public void onConnectionFailed(ConnectionResult result) {
Log.w(MA, "connection failed");
if (result.hasResolution()){
try {
result.startResolutionForResult(this, REQUEST_CODE);
}catch (IntentSender.SendIntentException sendIntentException) {
Toast.makeText(this,"Google Play services problem, exiting", Toast.LENGTH_LONG).show();
finish();
}
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
gac.connect();
}
}
protected void onStart() {
super.onStart();
if (gac != null)
gac.connect();
}
}    

关于您对Google API相关参考的关注,此API可能会帮助您满足GPS应用程序的需求。

位置和上下文API

  • 位置和上下文API利用移动设备的传感器和信号来提供对用户行为及其环境的感知,实现令人愉快和引人入胜的体验,简化用户交互,提供帮助,并帮助用户更好地了解自己。

  • 这包含一个API和平台的列表,您可以根据所需内容使用这些API和平台。

在您的情况下,您可以检查以下内容:

  1. 地点API

    为用户提供有关他们在哪里、何时在那里的上下文信息。访问有关1亿个地点的详细信息,涵盖广泛的类别。

  2. 地理围栏

    地理围栏将对用户当前位置的感知与对用户接近可能感兴趣的位置的感知相结合。

  3. 融合定位提供程序API使用电池高效的API,根据设备传感器的组合信号获取应用程序的位置数据。



放置API

  • 在您开始使用Places API之前,您需要一个启用了账单帐户和Places API的项目。(请参阅有关如何使用计费帐户创建项目的链接(
  • 您需要获得一个API密钥来验证您的请求
  • 有关此API的更多详细信息,您可以访问提供的链接

以下位置请求可用:

地点搜索返回基于用户位置或搜索字符串的地点列表。

地点详细信息返回有关特定地点的更详细信息,包括用户评论。

地点照片可访问存储在谷歌地点数据库中的数百万张与地点相关的照片。

Place Autocomplete在用户键入时自动填写地点的名称和/或地址。

查询自动完成为基于文本的地理搜索提供查询预测服务,在用户键入时返回建议的查询。



融合定位提供商API

融合位置提供商是Google Play服务中的位置API之一。它管理底层定位技术,并提供一个简单的API,以便您可以指定高级别的要求,如高精度或低功耗。它还优化了设备对电池电量的使用。

支持常见位置场景:

  • 最后已知的位置

    包括设置和访问融合位置提供商的分步程序

  • 位置设置

  • 位置更新

您可以浏览此参考资料以了解更多信息:https://developer.android.com/training/location

相关内容

最新更新