Android 基于高德地图实现融云位置共享功能

Android 基于高德地图实现融云位置共享功能

开发准备:

1: 登录 融云开发者账号,提交 server 平台工单申请开通 实时位置共享功能。工单回复开通成功后、 2 小时生效。

2: 注册高德地图账号、申请成为开发者。获取高德地图相关 key 和 jar 包

LocationMessage_jar

3: Android 工程当中配置自己获取的 高德地图的 key

LocationMessage_key

注: 此处需要配置自己的 高德地图 key

code:

核心类: RealTimeLocationInputProvider 

RealTimeLocationMessageProvider 

LocationMapActivity 

RealTimeLocationActivity 

BasicMapActivity 抽象类

RealTimeLocationActivity 继承自 LocationMapActivity 继承自 

BasicMapActivity

此类代码可以在 demo 的 message provider 包下 copy 到 

需要注意的是 RealTimeLocationInputProvider, 继承自 LocationInputProvider (此类功用是发送当前位置的静态图给对方)。 

如果你没有开通 位置共享功能 的服务 他就通不过第一个 if 判断:

RealTimeLocationConstant.RealTimeLocationErrorCode errorCode = RongIMClient.getInstance().getRealTimeLocation(getCurrentConversation().getConversationType(), getCurrentConversation().getTargetId());

        if (errorCode != null && errorCode != RealTimeLocationConstant.RealTimeLocationErrorCode.RC_REAL_TIME_LOCATION_CONVERSATION_NOT_SUPPORT) {//服务端未开通}

———————————————END————————————————–

然后他就会去调用父类的发送位置的方法 ,也就是 LocationInputProvider 。需要注意会话类型,位置共享目前只支持单聊 和 讨论组的会话类型

核心接口: 

RongIMClient.RealTimeLocationListener 

目前 demo 是在 ConversationActivity 类中实现这个接口:

 publicinterface RealTimeLocationListener {

        void onStatusChange(RealTimeLocationStatus status);

        void onReceiveLocation(double latitude, double longitude, String userId);

        void onParticipantsJoin(String userId);

        void onParticipantsQuit(String userId);

        void onError(RealTimeLocationErrorCode errorCode);

    }

——————————————–END—————————————————

RealTimeLocationActivity 中的:

privatevoid addUserInfoToScrollView(final String userId) {

        DemoContext.getInstance().getDemoApi().getUserInfo(userId, new DemoApi.GetUserInfoListener() {

            @Override

            publicvoid onSuccess(final UserInfo userInfo) {

                runOnUiThread(new Runnable() {

                    @Override

                    publicvoid run() {

                        horizontalScrollView.addUserToView(userInfo);

                        setParticipantTextView(-1);

                    }

                });

            }

            @Override

            publicvoid onError(String userId, BaseException e) {

            }

        });

    }

   publicvoid onEventMainThread(final RongEvent.RealTimeLocationReceiveEvent event) {

        String userId = event.getUserId();

        DemoContext.getInstance().getDemoApi().getUserInfo(userId, new DemoApi.GetUserInfoListener() {

            @Override

            publicvoid onSuccess(final UserInfo userInfo) {

                runOnUiThread(new Runnable() {

                    @Override

                    publicvoid run() {

                        moveMarker(new LatLng(event.getLatitude(), event.getLongitude()), userInfo);

                    }

                });

            }

            @Override

            publicvoid onError(String userId, BaseException e) {

            }

        });

    }

——————————————-END—————————————————-

请求获取用户信息部分 需要换成自己的 网络请求 或者 缓存信息

经纬度的获取:

LocationMapActivity 中

@Override

    publicvoid onLocationChanged(AMapLocation amapLocation) {

        if (amapLocation != null && amapLocation.getAMapException().getErrorCode() == 0) {

            //获取位置信息

            Double geoLat = amapLocation.getLatitude();

            Double geoLng = amapLocation.getLongitude();

            RongIMClient.getInstance().updateRealTimeLocationStatus(conversationType, targetId, geoLat, geoLng);

        }

    }

———————————————END————————————————-

如果进入界面 一直定位在北京中心 你当在此处监听你的经纬度有没有成功获取到,如果经纬度没有获取到可能是因为高德地图的 appkey 或者 keystore 没有配置 (keystore 很重要 与 高德 appkey 必须是对应的 否则无法获取到 经纬度)

key 的配置: key 的值 从高德地图获取 此处只是 demo 的示例 

LocationMessage_Demo_Key

Use:

使用在会话界面 + 号扩展功能中使用:

 InputProvider.ExtendProvider[] singleProvider = {

                new ImageInputProvider(RongContext.getInstance()),

                new CameraInputProvider(RongContext.getInstance()),//相机

                new RealTimeLocationInputProvider(RongContext.getInstance()),//带位置共享的地理位置

//                new VoIPInputProvider(RongContext.getInstance()),// 语音通话

        };

        InputProvider.ExtendProvider[] muiltiProvider = {

                new ImageInputProvider(RongContext.getInstance()),

                new CameraInputProvider(RongContext.getInstance()),//相机

                new LocationInputProvider(RongContext.getInstance()),//地理位置

        };

        RongIM.getInstance().resetInputExtensionProvider(Conversation.ConversationType.PRIVATE, singleProvider);

        RongIM.getInstance().resetInputExtensionProvider(Conversation.ConversationType.DISCUSSION, muiltiProvider);

        RongIM.getInstance().resetInputExtensionProvider(Conversation.ConversationType.CUSTOMER_SERVICE, muiltiProvider);

        RongIM.getInstance().resetInputExtensionProvider(Conversation.ConversationType.GROUP, muiltiProvider);

———————————————–END————————————————

LocationMessage_Demo_Provider

至此 Android Kit 位置共享集成实现完毕

效果预览:

LocationMessage_Demo_1LocationMessage_Demo_2LocationMessage_Demo_3