我实现了谷歌地图,并在地图上添加了带有动画的多个标记,但是当尝试进行动画处理时,它将添加新的标记,并且动画无法以正确的方式运行。
如果有人知道,我会添加一些代码,请帮助我。
在地图上添加标记:
private void addMarkerData(MapUserListBean mapUserListBean) {
double Newlatitude = mapUserListBean.lat;
double Newlongitude = mapUserListBean.lng;
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(Newlatitude, Newlongitude))
.icon(BitmapDescriptorFactory.
fromResource(R.mipmap.ic_location_green))
.title(mapUserListBean.name));
marker.setTag(mapUserListBean);
mHashMap.put(marker, mapUserListBean.user_id);
}
下面的代码,如果标记已经可用,则只是动画,或者如果不可用,则添加:
if (mHashMap.entrySet().size() > 0) {
for (Map.Entry<Marker, Integer> entry : mHashMap.entrySet()) {
double latitude = DashboardFragment.mapUserListBeanArrayList.get(i).lat;
double longitude = DashboardFragment.mapUserListBeanArrayList.get(i).lng;
if (entry.getValue() == DashboardFragment.mapUserListBeanArrayList.get(i).user_id) {
//animateMarker(marker, new LatLng(latitude, longitude), false);
MarkerAnimation.animateMarkerToGB(marker, new LatLng(latitude, longitude), new LatLngInterpolator.Spherical(), true);
} else {
addMarkerData(DashboardFragment.mapUserListBeanArrayList.get(i));
break;
}
}
} else {
addMarkerData(DashboardFragment.mapUserListBeanArrayList.get(i));
}
现在上面的代码添加了标记和动画,但是有时它会添加相同的标记2次,并且动画在更新的纬度和经度下无法正常工作。
根据您的代码示例很难找到问题,但是:1)Map.Entry条目-我认为最好使用“ user_id”作为键,并使用标记-作为值
2)关于此行
MarkerAnimation.animateMarkerToGB(marker, new LatLng(latitude, longitude), new LatLngInterpolator.Spherical(), true);
变量“标记”是类变量吗?检查在哪里初始化它或显示整个类代码。