我面临的问题是我需要更新屏幕上的地图,以便用户所走路线的所有点都可见。
在下面的代码中,我计算了请求地图更新的次数,但我注意到有时请求的数量与回调的数量不匹配。因此,等待"mapLoaded"变为0不是一个好主意。
因此,我添加了 10 秒的时间限制,但这是任意的,有时还不够。那么,我如何确定所有地图更新都已完成?
private void adjustMapCompleteSO(LatLng from, LatLng to){//3.3.17 show all points for screenshot
double x1=(from.latitude+to.latitude)/2;
double x2=(from.longitude+to.longitude)/2;
LatLng del=new LatLng(x1,x2);
map.moveCamera(CameraUpdateFactory.newLatLng(del));
mapLoaded=0;
for(Polyline pol : allcrumbs){
List<LatLng> points = pol.getPoints();
for (LatLng point : points){
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(point);
LatLngBounds bounds = builder.build();
int padding = 40; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mapLoaded++;
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
public void onMapLoaded() {
mapLoaded--;
}
});
map.moveCamera(cu);
}
}
Date started = new Date();
while (mapLoaded !=0 && new Date().getTime() - started.getTime() < 10000){
try {//wait until map has loaded, but max 10 seconds
Thread.sleep(500);//wait half a second before tyring again
} catch (InterruptedException e) {}
}
}
显示地图上的所有折线。
创建构建器
LatLngBounds.Builder builder = new LatLngBounds.Builder();
遍历折线中的所有点,将它们发送到经度长边界生成器。
for(Polyline pol : allcrumbs){
List<LatLng> points = pol.getPoints();
for (LatLng point : points){
// dude never initialize variables in a loop again
// its automatic fail for speed of execution.
// String never = "Do this in a loop";
// int padding = 40; // offset from edges of the map in pixels
builder.include(point);
}
}
现在移动相机
LatLngBounds bounds = builder.build();
int padding = 40; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
map.moveCamera(cu);
IDK 你在映射加载的回调中做了什么,所以它不在上面的代码中。
提示:在创建折线时填充latlngbounds.builder,只需在完成加载折线后移动摄像机即可。
LatLngBounds bounds = builder.build();
int padding = 40; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
map.moveCamera(cu);
注意:沿路径移动摄像机类似于代码,但通常仅在摄像机完成每个点时更新摄像机。