gpt4 book ai didi

java - 如何在android中更改位置更改时更改标记位置

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:27 25 4
gpt4 key购买 nike

我在 map 上有一个标记,我需要更改标记的位置。

这是我的代码:

public void onLocationChanged(Location location) {

map.clear();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
if (Home_Activity.this.markerob != null) {
Home_Activity.this.markerob.remove();
markerob.setPosition(latLng);
}
latitude=location.getLatitude();
longitude=location.getLongitude();
CameraUpdate cameraUpdate =CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(18.0f).build());

MarkerOptions options = new MarkerOptions().position(latLng);
//options.title( getAddressFromLatLng( latLng ) );

options.icon(BitmapDescriptorFactory.fromBitmap(Bitmap.createScaledBitmap(
BitmapFactory.decodeResource(getResources(),
spmain.getavatarimage()), 90, 90, false)));

//map.addMarker(new MarkerOptions().position(latLng));
Toast.makeText(getApplicationContext(), "lat="+latLng,Toast.LENGTH_SHORT).show();

options.position(latLng);
map.getUiSettings().setRotateGesturesEnabled(true);
map.animateCamera(cameraUpdate);
map.moveCamera(cameraUpdate);
map.addMarker(options);


//locationManager.removeUpdates(this);
}

我添加了 onLocationChanged 方法,并在其中获取位置详细信息,但不会将标记移动到新位置。

最佳答案

检查您的 map 是否添加了标记(第一次)。如果没有,则添加标记并保留对该标记的引用。对于对 onLocationChanged 的​​后续调用,只需使用相同的引用来更新纬度和经度。

    Marker myMarker;
if(myMarker == null){
marker = map.addMarker(options);
}
else {
marker.setPosition(new LatLng(location.getLatitude(),location.getLongitude()));
}

希望这对您有所帮助。让我知道这是行不通的。将发布更多相关代码。

关于java - 如何在android中更改位置更改时更改标记位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38182424/

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