android studio-googleapiclient不推荐的修复程序

hts6caw3  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(345)

我正在建立一个gps应用程序,但GoogleAppClient已被弃用,我不知道如何修复它。另外,当我运行这个应用程序时,我得到一个登录按钮,它不会导致任何结果,这可能与此有关,因为我知道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();
}

}

lawou6xi

lawou6xi1#

关于您对谷歌api相关参考的关注,此api可能会帮助您满足gps应用程序的需求。
位置和上下文API
位置和上下文api利用移动设备的传感器和信号来提供对用户行为及其环境的感知,从而实现令人愉快和引人入胜的体验,简化用户交互,提供帮助,并帮助用户更好地了解自己。
这包含了一个API和平台的列表,您可以使用这些API和平台,具体取决于您要查找的内容。
在您的情况下,您可以检查以下各项:
位置api
给你的用户提供他们在哪里,什么时候在那里的上下文信息。访问各种类别的1亿个地点的详细信息。
地理围栏
地理围栏结合了对用户当前位置的感知和对用户接近感兴趣位置的感知。
融合的位置提供程序api使用电池效率高的api,根据设备传感器的组合信号获取应用程序的位置数据。
位置api
在开始使用places api之前,需要一个启用了计费帐户和places api的项目(请参阅有关如何使用帐单帐户创建项目的链接)
您需要获得一个api密钥来验证您的请求。
有关此api的更多详细信息,请访问提供的链接。
以下位置请求可用:
位置搜索根据用户的位置或搜索字符串返回位置列表。
placedetails返回有关特定地点的更详细信息,包括用户评论。
PlacePhotos提供了对谷歌PlaceDatabase中存储的数百万张与地点相关的照片的访问。
“地点自动完成”在用户键入时自动填充地点的名称和/或地址。
查询自动完成为基于文本的地理搜索提供查询预测服务,在用户键入时返回建议的查询。
融合位置提供程序api
融合位置提供者是googleplay服务中的位置api之一。它管理底层的定位技术,并提供一个简单的api,以便您可以在高级别上指定需求,比如高精度或低功耗。它还优化了设备对电池电量的使用。
支持公共位置场景:
最后已知位置
包括设置和访问位置提供程序的逐步过程
位置设置
位置更新
您可以浏览此参考资料以了解更多信息:https://developer.android.com/training/location

相关问题