如何以编程方式在聊天 window 中显示位置消息,就像 android 中的 whatsapp 一样?

how to display location message in chat window same like whatsapp in android programmatically?

我正在开发一个聊天应用程序。用户可以在哪里 发送位置,就像 whatsapp 一样。 (我不是在谈论共享实时位置功能)。对于此要求,我使用了 google 地点选择器 api。在这里,我附上了一个代码。

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" />

在 java 文件中

 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();
            }

这就是 onActivityResult 方法的处理方式

@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);
            }
        }
    }

现在选择那个位置后想要像这样显示它。

那么我该如何实现呢?

关注此 link google 提供地图缩略图 link

另外,你可以通过这种方式 https://maps.googleapis.com/maps/api/staticmap?center=latitude,longitude&zoom=12&size=600x300&maptype=normal

您可以为此使用静态地图。

http://maps.googleapis.com/maps/api/staticmap?center=9.9252,78.1198&zoom=14&markers=color:blue|label:A|9.9252,78.1198&size=500x400&sensor=false

您可以像下面这样以编程方式替换动态坐标:

String location = "http://maps.googleapis.com/maps/api/staticmap?center="
                                + Utils.CUR_LATTITUDE
                                + ","
                                + Utils.CUR_LONGITUDE
                                + "&zoom=14&markers=color:blue|label:A|"
                                + Utils.CUR_LATTITUDE
                                + ","
                                + Utils.CUR_LONGITUDE
                                + "&size=500x400&sensor=false"

布局文件:

<?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>

使用这个布局文件作为recyclerview的项目文件。

对于地图图像:

使用这个答案:

获取经纬度和地名:

place.getLatLng()获取经纬度

place.getName() 获取要显示在地图底部的地名

对于带有 recyclerview 的聊天应用程序演示,可以阅读以下教程:

  1. https://github.com/QuickBlox/ChatMessagesAdapter-android

  2. https://www.dev2qa.com/android-chat-app-example-using-recyclerview/

在项目视图中为 MapView 或 MapFragment 添加 XML 属性

<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);
        }
    }

在你的 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);
            });
        }

最终结果: