java - 每次滚动/平移/拖动时,相机都会移回到原始位置

标签 java android google-maps

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

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 也会发生同样的情况。当我更改新位置时,这不会更新新信息

最佳答案

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

            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));

关于java - 每次滚动/平移/拖动时,相机都会移回到原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59845439/

相关文章:

java - 谷歌地图准确性问题

javascript - 用 20m x 20m 框包裹纬度和经度点(Google Maps API V3)

java - Fragment中TextView的空对象引用

Android TextView 文字背景颜色

java - android 媒体播放器 url 未找到

java - 自定义溢出菜单在 Android API 21 和 23+ 中不会给出相同的结果

java - 为什么 java7 对 double 的和给出错误输出

java - 从 bean factory 访问 injectee 组件

java - 凯撒密码 java 我的方式,不起作用

android - 在 Android 应用程序中集成语音导航