Android Studio获取用户的位置

问题描述 投票:1回答:1
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”替换为用户的位置,以便相机关注用户的位置,谢谢!!!!

android google-maps android-studio android-fragments google-maps-android-api-2
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.