Android Studio获取用户的位置

时间:2018-03-05 16:58:15

标签: android google-maps android-studio android-fragments google-maps-android-api-2

LatLng Barcelona = new LatLng(Float.parseFloat(String.valueOf(41.3818)), Float.parseFloat(String.valueOf(2.1685)));
    // For zooming automatically to the location of the marker
                    CameraPosition cameraPosition = new CameraPosition.Builder().target(Barcelona).zoom(12).build();
                    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition
                            (cameraPosition ));

我在Android Studio Activity中使用Goole Maps API,我想获取用户的位置,并替换变量" Barcelona"使用用户的位置,以便相机专注于用户的位置,谢谢!!!!

1 个答案:

答案 0 :(得分:0)

如果您想在活动开始时只获取一次位置,可以使用以下代码。

首先创建FusedLocationProviderClient对象,如下所示:

FusedLocationProviderClient  fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(SetProfileOnlineActivity.this);

创建对象后,您可以调用getLastLocation()方法只获取一次用户位置。

fusedLocationProviderClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        if (location != null) {
                            CameraPosition.Builder builder = new CameraPosition.Builder();
                            builder.tilt(30);
                            builder.zoom(18f);
                            builder.target(new LatLng(location.getLatitude(), location.getLongitude()));
                            CameraPosition cameraPosition = builder.build();
                            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                        }
                    }
                });

如果您的应用需要持续跟踪位置,则可以向用户提供更多相关信息。

 LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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;
    }
    mFusedLocationClient.requestLocationUpdates(mLocationRequest,
            mLocationCallback,
            null /* Looper */);

你的onCreate看起来像那样

@Override
protected void onCreate(Bundle savedInstanceState) {
// ...

mLocationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
        for (Location location : locationResult.getLocations()) {
            // Update UI with location data
            // ...
        }
    };
};
}

有关更多信息,请浏览android文档

https://developer.android.com/training/location/receive-location-updates.html