Reputation: 1251
I am developing a chat application. Where user can send location same like whatsapp. (I am not talking about share live location functionality). For this requirement i have use google place picker api. Here I am attaching a code of that.
build.gradle
implementation 'com.google.android.gms:play-services-maps:10.2.0'
implementation 'com.google.android.gms:play-services:10.2.0'
AndroidManifest.xml
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
in java file
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
dismiss();
mActivity.startActivityForResult(builder.build(currentFragment.getActivity()), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
Log.e(this.getClass().getSimpleName(),"GooglePlayServicesRepairableException");
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
Log.e(this.getClass().getSimpleName(),"GooglePlayServicesNotAvailableException");
e.printStackTrace();
}
And this is how handled onActivityResult method
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(this, data);
}
}
}
Now after selecting that location want to display it similar to like this.
So how can I achieve this?
Upvotes: 8
Views: 6339
Reputation: 2411
You can make use of static maps for that.
You can replace the dynamic co-ordinates programmatically like below:
String location = "http://maps.googleapis.com/maps/api/staticmap?center="
+ LATTITUDE
+ ","
+ LONGITUDE
+ "&zoom=14&markers=color:blue|label:A|"
+ LATTITUDE
+ ","
+ LONGITUDE
+ "&size=500x400&sensor=false"
Upvotes: 3
Reputation: 5109
Add an XML attribute for a MapView or MapFragment in to you item view
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.apps.maps"
android:id="@+id/map"
android:layout_width="300dp"
android:layout_marginTop="5dp"
android:layout_height="250dp"
map:uiMapToolbar="false"
android:layout_marginBottom="20dp"
map:cameraZoom="13"
map:mapType="normal"
map:liteMode="true"/>
ViewHolder
public static class ViewHolder2 extends RecyclerView.ViewHolder {
public TextView showMessage;
public TextView ago, timeText;
public ImageView seen;
public MapView map;
public ViewHolder2(@NonNull View itemView) {
super(itemView);
showMessage= itemView.findViewById(R.id.show_message);
ago = itemView.findViewById(R.id.ago);
seen = itemView.findViewById(R.id.seen);
map = itemView.findViewById(R.id.map);
timeText = itemView.findViewById(R.id.chat_time);
}
}
In your onBindViewHolder
if (viewHolder2.map != null) {
// Initialise the MapView
viewHolder2.map.onCreate(null);
viewHolder2.map.onResume();
// Set the map ready callback to receive the GoogleMap object
viewHolder2.map.getMapAsync(googleMap -> {
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(chat.getLat(),chat.getLon()), 13f));
googleMap.addMarker(new MarkerOptions().position(new LatLng(chat.getLat(),chat.getLon())));
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.getUiSettings().setAllGesturesEnabled(false);
});
}
End result:
Upvotes: 1
Reputation: 5684
Layout file :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#e0ffc6"
app:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="200dp"
android:layout_height="150dp"
android:padding="5dp"
android:scaleType="fitXY"
tools:src="@tools:sample/backgrounds/scenic" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Location"
android:textColor="#000000" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginRight="5dp"
android:gravity="end"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="11:00 AM"
android:textColor="#000000"
android:textSize="10sp" />
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="center_vertical"
tools:src="@tools:sample/avatars" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Use this layout file as item file of recyclerview.
For map image :
Use this answer : https://stackoverflow.com/a/50674957/8089770
To get lat long and name of place :
place.getLatLng()
to get the latitude and longitude
place.getName()
to get Place name to show at bottom of map
For chat application demo with recyclerview can go through tutorials like :
Upvotes: 4
Reputation: 2559
follow this link google provide map thumbnail link
Also, you can get this way https://maps.googleapis.com/maps/api/staticmap?center=latitude,longitude&zoom=12&size=600x300&maptype=normal
Upvotes: 1