如何在不使用android中的map的情况下找到用户的位置

时间:2017-08-14 17:01:53

标签: android google-maps google-maps-api-3

是否可以在不使用mapFragment的情况下获取用户的纬度和经度。我只想用一个返回当前位置的方法构建一个类。

1 个答案:

答案 0 :(得分:0)

希望这会对你有帮助......

加入您的清单文件

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

获取API 23及更高版本的运行时权限。

然后将此代码放在您的java文件

private synchronized void buildGoogleApiClient() {
 Log.i("TAG", "Building GoogleApiClient");
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity().getApplicationContext())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    createLocationRequest();}

private void createLocationRequest() {
    Log.i("TAG", "CreateLocationRequest");
    mLocationRequest = new LocationRequest();
    long UPDATE_INTERVAL = 10 * 1000;
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    long FASTEST_INTERVAL = 10000;
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
    //**************************
    builder.setAlwaysShow(true); //this is the key ingredient
    //**************************

}

private void startLocationUpdates() {

    Log.i("TAG", "StartLocationUpdates");

    if (Build.VERSION.SDK_INT >= 23) {
        if (ContextCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                    mLocationRequest, this);

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

}

private void stopLocationUpdates() {
    Log.i("TAG", "StopLocationUpdates");
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

}
 @Override
public void onConnectionSuspended(int i) {
    Log.i("TAG", "onConnectionSuspended");
    if (i == CAUSE_SERVICE_DISCONNECTED) {
        Toast.makeText(getActivity().getApplicationContext(), "Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
    } else if (i == CAUSE_NETWORK_LOST) {
        Toast.makeText(getActivity().getApplicationContext(), "Network lost. Please re-connect.", Toast.LENGTH_SHORT).show();
    }
    mGoogleApiClient.connect();
}

@Override
public void onLocationChanged(Location location) {
    Log.i("TAG", "OnLocationChanged");
    Log.i("TAG", "Current Location==>" + location);
    currentlatitude = location.getLatitude();
    currentlongitude = location.getLongitude();
}


    @Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
        try {
            // Start an Activity that tries to resolve the error
            connectionResult.startResolutionForResult(getActivity(), connectionResult.RESOLUTION_REQUIRED);
        } catch (IntentSender.SendIntentException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("TAG", "Location services connection failed with code==>" + connectionResult.getErrorCode());
        Log.e("TAG", "Location services connection failed Because of==> " + connectionResult.getErrorMessage());
    }

}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mGoogleApiClient != null)
        mGoogleApiClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
    Location mCurrentLocation;

    Log.i("TAG", "OnConnected");
    if (Build.VERSION.SDK_INT >= 23) {
        if (ContextCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

            // Note that this can be NULL if last location isn't already known.
            if (mCurrentLocation != null) {
                // Print current location if not null
                Log.d("DEBUG", "current location: " + mCurrentLocation.toString());
                currentlatitude = mCurrentLocation.getLatitude();
                currentlongitude = mCurrentLocation.getLongitude();
            } else {
                startLocationUpdates();
            }


        }
    } else {
        mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

        // Note that this can be NULL if last location isn't already known.
        if (mCurrentLocation != null) {
            // Print current location if not null
            Log.d("DEBUG", "current location: " + mCurrentLocation.toString());
            currentlatitude = mCurrentLocation.getLatitude();
            currentlongitude = mCurrentLocation.getLongitude();
        }
        // Begin polling for new location updates.
        startLocationUpdates();
    }
}

有关详细信息,请参阅此链接:https://developers.google.com/android/guides/setup

https://developer.android.com/training/location/retrieve-current.html