【问题标题】:how to display location message in chat window same like whatsapp in android programmatically?如何以编程方式在聊天窗口中显示位置消息,就像android中的whatsapp一样?
【发布时间】:2018-06-04 06:51:52
【问题描述】:

我正在开发一个聊天应用程序。用户可以像whatsapp一样发送位置。 (我不是在谈论共享实时位置功能)。对于这个要求,我使用了 google place picker 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);
            }
        }
    }

现在,在选择该位置后,希望像这样显示它。

那么我该如何实现呢?

【问题讨论】:

    标签: android android-layout google-maps-api-3 android-mapview


    【解决方案1】:

    布局文件:

    <?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的item文件。

    地图图片:

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

    获取纬度和地名:

    place.getLatLng()获取经纬度

    place.getName() 让地名显示在地图底部

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

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

    2. RecyclerView for chat app

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

    【讨论】:

    • khub khub abhaar.
    • 你有什么想法吗,如何实现在whatsapp之类的聊天窗口中显示音频和视频的UI?
    • @KushPatel 抱歉不知道这一点.. 但你可以尝试使用 imageview、seekbar/progressbar、progressbar 以图像为中心。可能是这样的
    【解决方案2】:

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

    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"
    

    【讨论】:

    • 你有什么想法吗,如何实现在whatsapp之类的聊天窗口中显示音频和视频的UI?
    【解决方案3】:

    【讨论】:

      【解决方案4】:

      在您的项目视图中添加 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);
                  });
              }
      

      最终结果:

      【讨论】:

        猜你喜欢
        • 2015-01-21
        • 2021-10-04
        • 1970-01-01
        • 2022-11-01
        • 2015-11-23
        • 1970-01-01
        • 1970-01-01
        • 2017-06-01
        • 2022-01-12
        相关资源
        最近更新 更多