如何在聊天窗口中像Android中的whatsapp一样显示位置消息? [英] how to display location message in chat window same like whatsapp in android programmatically?

查看:132
本文介绍了如何在聊天窗口中像Android中的whatsapp一样显示位置消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发聊天应用程序.用户可以在哪里 发送与whatsapp相同的位置 . (我不是在谈论共享实时位置功能).对于这个要求,我已经使用谷歌的地方选择器的API.在这里,我附上一个代码.

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

在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方法的处理方式

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.

那我该如何实现呢?

推荐答案

布局文件:

<?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的项目文件.

Use this layout file as item file of recyclerview.

对于地图图像:

使用此答案: https://stackoverflow.com/a/50674957/8089770

获取经度和地点名称:

place.getLatLng()获取纬度和经度

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

place.getName() to get Place name to show at bottom of map

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

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

用于聊天应用程序的RecyclerView

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

这篇关于如何在聊天窗口中像Android中的whatsapp一样显示位置消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆