Google Maps API显示位置,但不居中

时间:2018-07-19 13:41:29

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

我正在使用Android Studio 2.2.3中的Google Maps API,由于某种原因,位置按钮可以工作,但是它不能居中。此外,我举杯祝酒,找不到位置。

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

    getLocationPermission();
}

private void getDeviceLocation() {
    Log.d(TAG, "getDeviceLocation: getting the devices current location");

    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    try {
        if (mLocationPermissionsGranted) {

            Task location = mFusedLocationProviderClient.getLastLocation();
            location.addOnCompleteListener(new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    if (task.isSuccessful() && task.getResult() != null) {
                        Log.d(TAG, "onComplete: found location");
                        Location currentLocation = (Location) task.getResult();

                        moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),
                                DEFAULT_ZOOM);


                    } else {
                        Log.d(TAG, "onComplete: current location is null");
                        Toast.makeText(MapActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();
                        Location currentLocation = (Location) task.getResult();
                    }
                }
            });
        }

    } catch (SecurityException e) {
    }
}

我相信上面的代码是找到位置,然后将相机定位到该给定位置的代码。我没有收到任何错误,当我将LocationButton设置为true,然后单击它时,它确实位于我的位置。

2 个答案:

答案 0 :(得分:0)

通过将该代码更改为

    private void getDeviceLocation(){
    try{
        if(mLocationPermissionGranted){
            Task locationResult = mFusedLocationProviderClient.getLastLocation();
            locationResult.addOnCompleteListener(this, new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    if(task.isSuccessful() && task.getResult() != null){
                        Log.d(TAG, "onComplete: location found!");
                        Location currentLocation = (Location) task.getResult();

                        float currentZoom = mMap.getCameraPosition().zoom;//edit (added line)

                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(),
                                currentLocation.getLongitude()), DEFAULT_ZOOM));//edit (changed line)

                    }else{
                        Log.d(TAG, "onComplete: current location is null");
                        Toast.makeText(MapActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();

                    }
                }
            });
        }

    }catch (SecurityException e){

    }
}

我设法在手动设置设备的位置后获得位置的回报,因为由于某种原因无法立即确定位置。 目前,问题已解决!

答案 1 :(得分:0)

尝试过您的代码。 它对我不起作用,但我解决了问题。 这是正确的代码。

private void getDeviceLocation(){
    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    try{
        if(mLocationPermissionGranted){
            Task location = mFusedLocationProviderClient.getLastLocation();
            location.addOnCompleteListener(new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    if(task.isSuccessful() && task.getResult() != null){
                        Toast.makeText(DriverMapActivity.this, "Task is successful", Toast.LENGTH_SHORT).show();
                        Location currentLocation = (Location) task.getResult();
                        mLatLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
                        moveCamera(mLatLng, DEFAULT_ZOOM);

                    }else{
                        Toast.makeText(DriverMapActivity.this, "Task is unsuccessful", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }catch (SecurityException e){
        Toast.makeText(this, "SecurityException Found", Toast.LENGTH_SHORT).show();
    }
}

因此,如果您的应用程序崩溃了,您可能想尝试一下。