gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:45:42 26 4
gpt4 key购买 nike

我正在创建一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com