【问题标题】:Camera moves back to original position everytime I scroll/pan/drag每次滚动/平移/拖动时,相机都会移回原始位置
【发布时间】:2020-01-21 16:30:40
【问题描述】:

我正在创建一个 Android 应用程序并设置了一个标准的 Google 地图活动。到目前为止,该应用程序仅显示用户的位置。相机最初设置为专注于用户位置。当我尝试滚动或拖动到模拟器上的新位置时遇到问题。我拖动鼠标,一旦我抬起鼠标,相机就会重置到原来的位置。

onMapReady 代码

public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                mMap.clear();
                LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
                mMap.addMarker(new MarkerOptions().position(currentLocation).title("You Are Here"));
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(currentLocation)
                        .zoom(25)
                        .bearing(200)
                        .tilt(90)
                        .build();
                mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());

                try {
                    List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

                       if (listAddresses.get(0).getAdminArea() != null) {
                           String address += listAddresses.get(0).getAdminArea();
                       }

                        Toast.makeText(MapsActivity.this, address, Toast.LENGTH_SHORT).show();
                        Log.i("Address", address);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            mMap.clear();
            LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
            mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(userLocation)
                    .zoom(25)
                    .bearing(0)
                    .tilt(90)
                    .build();
            mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        }
    }

这是我必须在当前位置显示标记的代码。我也有获取当前位置 AdminArea 的代码。

我认为问题在于我的代码一直在运行,因此当我尝试拖动屏幕时,它只会重置到最初设置相机的位置。显示当前位置的 Toast 也是如此。当我更改新位置时,这不会更新新信息

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    你是对的。您正在 onLocationChange 方法中设置相机位置,该方法会不断触发(用户的位置不必更改太多即可触发此方法)。因此,通过将相机位置设置为 currentLocation,它将继续滚动地图。尝试将其注释掉,看看会发生什么。

                public void onLocationChanged(Location location) {
                mMap.clear();
                LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
                mMap.addMarker(new MarkerOptions().position(currentLocation).title("You Are Here"));
                //CameraPosition cameraPosition = new CameraPosition.Builder()
                //        .target(currentLocation)
                //        .zoom(25)
                //        .bearing(200)
                //        .tilt(90)
                //        .build();
                //mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 2015-12-16
      相关资源
      最近更新 更多