我正在尝试使用
here提到的API获取Google Maps V2地图的屏幕截图,但这似乎总是返回底部带有Google徽标的白色图片.如果它不是徽标,我会确定这根本不起作用,但徽标意味着发生了一些事情并且地图没有显示.这大致就是我正在做的事情:
mv.setVisibility(View.VISIBLE); mv.getMap().snapshot(new GoogleMap.SnapshotReadyCallback() { public void onSnapshotReady(Bitmap snapshot) { synchronized(finished) { finished[0] = snapshot; finished.notify(); } } });
我尝试了多种不同的方法,包括将图像绘制到不同的图像和各种其他尝试.
唯一的主要区别是我使用MapView而不是MapFragment,因为我在这里无法切换到使用片段的一些技术问题.
解决方法
要拍摄快照,您需要等待地图加载然后拍摄快照.
细节:
实现SnapshotReadyCallback和OnMapLoadedCallback.
import com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback; import com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback; public class KmlReader extends ActionBarActivity implements SnapshotReadyCallback,OnMapLoadedCallback {
…
if (mMap == null) { // Try to obtain the map from the SupportMapFragment. // mMap = mMapFragment.getMap(); // // Check if we were successful in obtaining the map. // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap(); // use the settings maptype // Check if we were successful in obtaining the map. if (mMap != null) { mMap.setOnMapLoadedCallback(this);
…
@Override public void onMapLoaded() { if (mMap != null) { mMap.snapshot(this); } }
当它触发时你有一个真正的位图.
@Override public void onSnapshotReady(Bitmap bm) { if (CheckStorage.isExternalStorageWritable()) { int newHeight = 96; int newWidth = 96; int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // create a matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth,scaleHeight); // recreate the new Bitmap Bitmap resizedBitmap = Bitmap.createBitmap(bm,width,height,matrix,false);