我有基于谷歌地图的应用程序,允许用户查看标记。我遇到的问题是,当我放大查看地图上的所有标记时,经过几秒钟后,地图会重置为原始缩放级别,我无法查看所有标记。
我希望地图保持在用户缩放级别,但我无法设法提出逻辑。这是我的代码:
@Override
protected void onPostExecute(final ArrayList<Item> arrayList) {
if(isCancelled()) return;
if(googleMap!=null) {
googleMap.clear();
mMarker2Item.clear();
LatLngBounds.Builder boundBuilder = new LatLngBounds.Builder();
for (Item item : arrayList) {
MarkerOptions opts = new MarkerOptions()
.position(item.location())
.title(item.name);
if(item.iconBitmap!=null){
opts = opts.icon(BitmapDescriptorFactory.fromBitmap(item.iconBitmap));
}
Marker newMarker = googleMap.addMarker(opts);
newMarker.setSnippet(item.vicinity);
mMarker2Item.put(newMarker, item);
boundBuilder.include(item.location());
}
try {
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(boundBuilder.build(), 200);
googleMap.moveCamera(cameraUpdate);
googleMap.animateCamera(cameraUpdate, 1000, null);
} catch (Exception ex) {
}
} else mHandler.postDelayed(new Runnable() {
@Override
public void run() {
onPostExecute(arrayList);
}
}, 500);
}
答案 0 :(得分:5)
只需在活动中将布尔标志定义为类成员变量(假设您的AsyncTask是活动的子类):
public boolean firstTime = true;
然后,在移动相机之前检查布尔标志:
if (firstTime) {
firstTime = false;
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(boundBuilder.build(), 200);
googleMap.moveCamera(cameraUpdate);
googleMap.animateCamera(cameraUpdate, 1000, null);
}