Reputation: 1433
I want to create an Image. Image should contain an image taken by camera, current address and a googlemap
with current location.
I can able to take an screenshot of a particular area with location text and getting it as an image. Issue geting is Map area remains blank(black)
I tried this but not clear to make it success
How to achieve this like in an Image. Thanks
Upvotes: 4
Views: 5783
Reputation: 13353
Easiest way to get image (Bitmap) of Google map - use Google Maps Static API. For downloading Bitmap with map for your lat/lng coordinates of map center and zoom you can use code like this:
...
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();
}
}
}
});
}
Where buildStaticApiUrl()
can be like:
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();
}
and GetStaticMapAsyncTask
like:
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);
}
}
Then, when you got both pictureFromCameraBitmap
and mapBitmap
Bitmaps you can combine them into one pictureBitmap
like that way:
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;
}
and getAddress()
can be like that
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();
}
Or, if you don't want to use Static Maps API, you can use MapView
-based workaround for map snapshot like in this or that answers:
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);
Upvotes: 2
Reputation: 195
You can use View and find view and drawing cache and take a screen shot.
View v = rootView.findViewById(R.id.frmMapView); v.setDrawingCacheEnabled(true);
Bitmap backBitmap = v.getDrawingCache();
Bitmap tripimage = Bitmap.createBitmap(
backBitmap.getWidth(), backBitmap.getHeight(),
backBitmap.getConfig());
Canvas canvas = new Canvas(tripimage);
canvas.drawBitmap(snapshot, new Matrix(), null);
canvas.drawBitmap(backBitmap, 0, 0, null);
bitmap = snapshot;
profilePath = "/mnt/sdcard/" + "MyMapScreen" + System.currentTimeMillis() + ".png";
FileOutputStream out = new FileOutputStream(profilePath);
tripimage.compress(Bitmap.CompressFormat.PNG, 100, out);
Upvotes: 0