I\'m creating an app which picks places from the Google maps and stores the address in the database. I also want to store the snapshot of the picked place in the storage, so
You might look into maps Lite. It uses the google maps api and map views, but treats each one similar to an image rather than an interactive map. You can still choose your zoom level and add markers to the map.
declare the map in your xml like you would a regular google map fragment but include the tag map:liteMode="true"
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="13"
map:mapType="normal"
map:liteMode="true"/>
or if you're creating the map programmatically you can use
GoogleMapOptions options = new GoogleMapOptions().liteMode(true);
it looks from your Activity code like you know how set up a google map, so you can then modify your onActivityResult to look like this
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST
&& resultCode == Activity.RESULT_OK) {
final Place place = PlacePicker.getPlace(this, data);
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
String attributions = (String) place.getAttributions();
if (attributions == null) {
attributions = "";
}
tv4.setText(place.getLatLng().toString()+"\n"+name+"\n"+address+"\n"+attributions);
// Add this line to make the lite map show the location you just chose
// and set the zoom level (10f is arbitrary)
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 10f));
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
More information and citations:
You can start here if you're looking for more info, there are tons of good links on this page https://developers.google.com/maps/documentation/android-api/lite
you can also check out the Google Lite Maps Demo Activity here: https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/LiteDemoActivity.java
And here's a great video that explains Lite Maps really well: https://youtu.be/N0N1Xkc_1pU
I faced same issue. I've tried with API key and Server key in both case I faced same error like "The Google Maps API server rejected your request... ". Finally I noticed that "Google Static Maps API" was disable from console. I just enable and everything works fine.
Since you are able to get the long and lat coordinates, you can get the map image by using the Google maps api.
Here's the link with examples and docs:
https://developers.google.com/maps/documentation/static-maps/intro
I assume this is what the dialog is doing in the background.
If that does not work, or you want something more exact, I suggest using Wireshark to monitor exactly what data is being sent and received.
I just ran a test with your map locations, and with this url:
https://maps.googleapis.com/maps/api/staticmap?center=37.430610,%20-121.972090&zoom=17&size=400x400&key=[myAPIKey]
I got this image:
Using the coords from your dialog:
Using https://maps.googleapis.com/maps/api/staticmap?markers=37.414333,-122.076444&zoom=17&size=400x250&key=[myKey]
If you want to show snapshot of the picked place in the dialog first you have to save the screen of map view into bitmap.
Refer this code to converting your map view into bitmap
Bitmap screen;
View v1 = MyView.getRootView();
v1.setDrawingCacheEnabled(true);
screen= Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
From above code you can get Bitmap which You can further use in your Dialog box by setting this bitmap into image view. But keep in mind u have to perform all operation before generating dialog and on worker thread.
You can use the GoogleMap.SnapshotReadyCallback interface.
Here is a code example on how to use it:
SnapshotReadyCallback callback = new SnapshotReadyCallback() {
Bitmap bitmap;
@Override
public void onSnapshotReady(Bitmap snapshot) {
bitmap = snapshot;
try {
FileOutputStream out = new FileOutputStream("/some/where/to/save/it/thesnapshot.png");
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
};
map.snapshot(callback);
You can add this in your code when you show the dialog and in the same time save the snapshot. Read more about it in the link above.
I hope this will be of any help to you and good luck.