Android的LocationClient类pcated德$ P $不过在文档中使用 [英] Android LocationClient class is deprecated but used in documentation

查看:96
本文介绍了Android的LocationClient类pcated德$ P $不过在文档中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们通过的<一个文档href="https://developer.android.com/reference/com/google/android/gms/location/LocationClient.html"><$c$c>LocationClient,我们可以看到,这个类去precated。

If we go through the documentation of the LocationClient, we can see that the class is deprecated.

但德precated类使用文档中获取当前位置

But the deprecated class is used in the documentation to get the current location.

我觉得这是有点误导还是我看不正确单证?

I think this is little bit misleading or am i looking at incorrect documentations?

推荐答案

此外谷歌已经发布了一个新的API,但他们还没有更新的文档:$花一些时间试图弄清楚它是如何工作我得到了它后,在这里你必须使用新的/最新位置服务API的一个完整的例子...享受发展:)

Again Google has released a new API but they haven't updated the documentation :$ After spend some time trying to figure out how it works I got it, here you have a full example using the new/latest Location Service API... Enjoy developing :)

import android.location.Location;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class MainActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    private final String TAG = "MyAwesomeApp";

    private TextView mLocationView;

    private GoogleApiClient mGoogleApiClient;

    private LocationRequest mLocationRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLocationView = new TextView(this);

        setContentView(mLocationView);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Connect the client.
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        // Disconnecting the client invalidates it.
        mGoogleApiClient.disconnect();
        super.onStop();
    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(1000); // Update location every second

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

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(TAG, "GoogleApiClient connection has been suspend");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.i(TAG, "GoogleApiClient connection has failed");
    }

    @Override
    public void onLocationChanged(Location location) {
        mLocationView.setText("Location received: " + location.toString());
    }
}

和不要忘了这个权限添加到您的Andr​​oidManifest.xml文件:

and do not forget to add this permissions to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

请注意:如果你只需要获得最后一个单元(无更新),您可以使用 LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)从OnConnected

Note: if you just need to get the last location (without updates), you can use LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient) from OnConnected

这篇关于Android的LocationClient类pcated德$ P $不过在文档中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆