使用Google Play服务和Wifi /数据网络的Android GPS位置

时间:2015-05-27 11:31:33

标签: java android gps google-play-services

我们实际上正在开发一个应用程序,我们需要确定我们的“当前”移动位置。

阅读文档,似乎Google Play服务是处理此问题的更好方法,而不是使用旧的LocationManager。

问题

即使我们在清单中指定了不同的访问权限,我们也无法通过WIFI或网络数据获取该位置。实际上,最后一个已知位置始终为空。

另一方面,如果我们激活GPS,最后一个位置是好的。

代码

清单包含:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

服务:

public class GoogleLocationService implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    private GoogleApiClient mGoogleApiClient;
    private Activity mActivity;
    private Location mLastLocation;
    private boolean mResolvingError;

    public GoogleLocationService(Activity mActivity) {
        this.mActivity = mActivity;
        this.mResolvingError = false;
    }

    public synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        Log.e("mLastLocation", mLastLocation + "");
        if (mLastLocation != null) {
            Log.e("lat/lng", mLastLocation.getLatitude() + " " + mLastLocation.getLongitude() + "");
        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (mResolvingError) {
            // Already attempting to resolve an error.
            return;
        } else if (connectionResult.hasResolution()) {
            try {
                mResolvingError = true;
                connectionResult.startResolutionForResult(mActivity, 1001);
            } catch (IntentSender.SendIntentException e) {
                // There was an error with the resolution intent. Try again.
                mGoogleApiClient.connect();
            }
        } else {
            // Show dialog using GooglePlayServicesUtil.getErrorDialog()
            Log.e("GoogleLocationService", connectionResult.getErrorCode() + "");
            mResolvingError = true;
        }
    }

    public boolean ismResolvingError() {
        return mResolvingError;
    }

    public void setmResolvingError(boolean mResolvingError) {
        this.mResolvingError = mResolvingError;
    }

    public GoogleApiClient getmGoogleApiClient() {
        return mGoogleApiClient;
    }

    public void setmGoogleApiClient(GoogleApiClient mGoogleApiClient) {
        this.mGoogleApiClient = mGoogleApiClient;
    }

    public Location getmLastLocation() {
        return mLastLocation;
    }

    public void setmLastLocation(Location mLastLocation) {
        this.mLastLocation = mLastLocation;
    }
}

活动:

 @Override
    protected void onStart() {
        super.onStart();
        if (!mServiceGoogle.ismResolvingError()) {  // more about this later
            mServiceGoogle.getmGoogleApiClient().connect();
        }
    }

    @Override
    protected void onStop() {
        mServiceGoogle.getmGoogleApiClient().disconnect();
        super.onStop();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_position);


        mServiceGoogle = new GoogleLocationService(this);
        mServiceGoogle.buildGoogleApiClient();

    }

我们是否遗漏了此代码中的某些内容以便以各种方式管理当前位置?

感谢您提前

0 个答案:

没有答案