我想创建一个图像。图片应包含一张由相机拍摄的图片、当前地址和一张包含当前位置的googlemap
。
我可以截取带有位置文本的特定区域的屏幕截图,并将其作为图像获取。 得到的问题是 map 区域保持空白(黑色)
我试过了 this但不清楚能否成功
如何在图像中实现这一点。谢谢
最佳答案
获取 Google map 图像(位图)的最简单方法 - 使用 Google Maps Static API .要为您的 map 中心的纬度/经度坐标和缩放下载带有 map 的位图,您可以使用如下代码:
...
private FusedLocationProviderClient mFusedLocationClient;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}
...
// Somewhere, when you need map image
if (checkPermission()) {
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
String mapUrl = buildStaticApiUrl(new LatLng(location.getLatitude(), location.getLongitude()), zoom, mapWidthInPixels, mapHeightInPixels);
try {
Bitmap mapBitmap = new GetStaticMapAsyncTask().execute(mapUrl).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
});
}
buildStaticApiUrl()
可以像这样:
private String buildStaticApiUrl(LatLng center, int zoom, int width, int height) {
StringBuilder url = new StringBuilder();
url.append("http://maps.googleapis.com/maps/api/staticmap?");
url.append(String.format("center=%8.5f,%8.5f", center.latitude, center.longitude));
url.append(String.format("&zoom=%d", zoom));
url.append(String.format("&size=%dx%d", width, height));
url.append(String.format("&key=%s", getResources().getString(R.string.google_maps_key)));
return url.toString();
}
和 GetStaticMapAsyncTask
像:
private class GetStaticMapAsyncTask extends AsyncTask<String, Void, Bitmap> {
protected void onPreExecute() {
super.onPreExecute();
}
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
HttpURLConnection connection = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream stream = connection.getInputStream();
Bitmap mapBitmap = BitmapFactory.decodeStream(stream);
// draw blue circle on current location
Paint locaionMarkerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
locaionMarkerPaint.setColor(Color.BLUE);
bitmap = Bitmap.createBitmap(mapBitmap.getWidth(), mapBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(mapBitmap,0,0, null);
canvas.drawCircle(mapBitmap.getWidth()/ 2, mapBitmap.getHeight() / 2, 20, locaionMarkerPaint);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
}
}
然后,当您同时获得 pictureFromCameraBitmap
和 mapBitmap
位图时,您可以像这样将它们组合成一个 pictureBitmap
:
public Bitmap composeBitmap(Bitmap pictureBitmap, Bitmap mapBitmap, LatLng location) {
Bitmap wholeBitmap = Bitmap.createBitmap(pictureBitmap.getWidth(), pictureBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(wholeBitmap);
canvas.drawBitmap(pictureBitmap,0,0, null);
canvas.drawBitmap(mapBitmap,0,wholeBitmap.getHeight() - mapBitmap.getHeight(), null);
String text = getCurrentTimeStr() + getAddress(location);
canvas.drawText(text, 0, 0, null);
return wholeBitmap;
}
和getAddress()
可以这样
public String getAddress(LatLng location) {
StringBuilder addr = new StringBuilder();
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(location.latitude, location.longitude, 1);
Address obj = addresses.get(0);
addr.append(obj.getAddressLine(0));
} catch (IOException e) {
}
return addr.toString();
}
或者,如果您不想使用静态 map API,您可以使用基于 MapView
的解决方法来获取 map 快照,如 this 中所述或 that答案:
GoogleMapOptions options = new GoogleMapOptions()
.compassEnabled(false)
.mapToolbarEnabled(false)
.camera(CameraPosition.fromLatLngZoom(KYIV,15))
.liteMode(true);
mMapView = new MapView(this, options);
...
mMapView.setDrawingCacheEnabled(true);
mMapView.measure(View.MeasureSpec.makeMeasureSpec(mMapWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(mMapHeight, View.MeasureSpec.EXACTLY));
mMapView.layout(0, 0, mMapWidth, mMapHeight);
mMapView.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(mMapView.getDrawingCache()); // <- that is bitmap with map image
mMapView.setDrawingCacheEnabled(false);
关于android - 以编程方式截取谷歌地图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50680978/