简体   繁体   English

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

[英]Camera moves back to original position everytime I scroll/pan/drag

I am creating an Android application and have set up a standard Google Maps activity.我正在创建一个 Android 应用程序并设置了一个标准的谷歌地图活动。 The application so far just displays the location of the user.到目前为止,该应用程序仅显示用户的位置。 The camera is set to focus on the users location initally.相机最初设置为聚焦于用户位置。 I am running into a problem when I try to scroll or drag to a new position on the emulator.当我尝试在模拟器上滚动或拖动到新位置时遇到问题。 I drag and once i lift up the mouse the camera resets to its original position.我拖动,一旦我抬起鼠标,相机就会重置到其原始位置。

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

        }
    }

This is the code I have to display a marker at current location.这是我必须在当前位置显示标记的代码。 I also have code to get AdminArea of current location.我还有代码来获取当前位置的 AdminArea。

I believe the problem is that my code is runnning constantly so that when I attempt to drag the screen, it just resets to where the camera was originally set.我认为问题在于我的代码一直在运行,因此当我尝试拖动屏幕时,它只会重置为最初设置相机的位置。 The same occurs for the Toast displaying the current location.显示当前位置的 Toast 也会发生同样的情况。 This does not update wth new information when i change new location当我更改新位置时,这不会更新新信息

You are correct.你是对的。 You are setting the camera location in your onLocationChange method, which fires constantly (the user's location doesn't have to change much for this method to fire).您正在 onLocationChange 方法中设置相机位置,该方法会不断触发(用户的位置无需更改太多即可触发此方法)。 So, by setting the camera location to the currentLocation, it will keep scrolling the map.因此,通过将相机位置设置为 currentLocation,它将继续滚动地图。 Try commenting that out and see what happens.尝试将其注释掉,看看会发生什么。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM