3

Preface

When using taxi-hailing software to take a taxi, we will be curious that when the driver is sending the passenger, the passenger's mobile phone is not navigating, so how exactly is this done? Today we will unveil its mysterious veil

Ready to work

To achieve this function, you still need the Tencent Location Service Family Bucket: Tencent Navigation SDK, Tencent Map SDK, Tencent Location SDK, Tencent Sicheng Tongxian SDK, specific permissions need to go to the official website console to operate In addition, you can contact the assistant for specific SDK permissions (as shown in the figure below), so I won’t discuss more here.
!

16222560693250.jpg

Implementation

Use the core map tool dependency library

//module 依赖
dependencies {
    // 地图库
    implementation 'com.tencent.map:tencent-map-vector-sdk:4.4.2'
    // 地图组件库,包括小车平移、点聚合等组件功能,详见开发指南。必须!!!
    implementation 'com.tencent.map:sdk-utilities:1.0.6' 
    // 定位sdk,可以从腾讯位置服务中心官网 联系小助手获取
    implementation files('libs/TencentLocationSDK_v8.4.14_ra0311232_20200103_125837_release.aar')
    // 司乘同显乘客端sdk,可以从腾讯位置服务中心官网 联系小助手获取
    implementation files('libs/lspassenger_v2.0.1_04.03.aar')
    implementation files('libs/lssupport_v2.0.1_04.03.aar')
    // 基础库。可以从腾讯位置服务中心官网 联系小助手获取
    implementation 'com.tencent.map:tencent-map-nav-surport:1.0.2.3'   
}

Flow chart display

16228857347642.jpg

According to the above flow chart, we know that to realize the smooth movement of the car, we need to continuously obtain the driver's points and current route in the last few seconds. The specific process is that when the driver starts the simultaneous display of the driver and the passenger, the driver will synchronize the route and the GPS points in the last few seconds through the driver and passenger simultaneous display SDK, and continue to train so that we can show the smooth movement of the car on the map. Of course, this is in actual production. If the card issuer wants to see the effect, the author can provide an idea here. You can build an ArrayList to contain the electrical string information of the entire line, and then continue to read it every 3 seconds. Take 3 points and transfer them to the map component library SDK, and then reciprocate so that you can see the effect.

Code

/**
     * 平滑移动
     * @param points
     */
    private void translateAnima(LatLng[] points) {
        if(points == null || points.length <= 0)
            return;
        // 当司机没有新数据上传,防止拉取回上个点串的最后一个点
        if(points.length == 2 && SHelper.equalOfLatlng(points[0], points[1]))
            return;
        if(carMarker != null)
            carMarker.remove();
        carMarker = mapView.getMap().addMarker(
                new MarkerOptions(points[0])
                        .anchor(0.5f, 0.5f)
                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.map_icon_driver))
                        //设置此属性 marker 会跟随地图旋转
                        .flat(true)
                        //marker 逆时针方向旋转
                        .clockwise(false));
        Log.e("tag1234", ">>>>>startAnimation()");
        // 小车平滑移动核心组件类,通过com.tencent.map:sdk-utilities:1.0.6获取
        MarkerTranslateAnimator mTranslateAnimator = new MarkerTranslateAnimator(
                //执行此平移动画的 marker
                carMarker,
                //动画持续时间
                animaTime,
                //平移动画点串
                points,
                //marker 是否会根据传入的点串计算并执行旋转动画, marker 方向将与移动方向保持一致
                true);
        mTranslateAnimator.startAnimation();
        mTranslateAnimator.setFloatValuesListener(new MarkerTranslateAnimator.IAnimaFloatValuesListener() {
            @Override
            public void floatValues(LatLng latLng) {
                // 小车走过的路线进行擦除
                eraseRoute(latLng);
            }
        });
    }
    
    // 路线擦除代码
    private void eraseRoute(LatLng latLng) {
        if(eraseThread == null) {
            eraseThread = new HandlerThread("car_erase_line");
            eraseThread.start();
        }
        if(eraseHandler == null ) {
            eraseHandler = new EraseHandler(eraseThread.getLooper());
        }

        Message message = Message.obtain();
        message.obj = latLng;
        message.what = ERASE_MSG;
        eraseHandler.sendMessage(message);
    }

    class EraseHandler extends Handler {

        public EraseHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try{
                switch (msg.what){
                    case ERASE_MSG:
                        LatLng latLng = (LatLng) (msg.obj);
                        if(latLng != null && polyline != null)
                            polyline.eraseTo(curEraseLatlng != null ? curEraseLatlng.getPointIndex() : 0, latLng);
                        eraseHandler.removeMessages(ERASE_MSG);
                        break;
                }
            }catch (Exception e){
                Log.e(LOG_TAG, "erase handler handle message error:" + e.getMessage());
            }
        }
    }

If you are interested, you can see the complete implementation demo here: https://github.com/tencentmap-mobility/mapmobilitydemo-passenger-Android/blob/master/app/src/main/java/com/map/ mobility/passenger/synchro_v2/psg/PsgActivity.java

Final effect display

AnnotationAnimation.gif


腾讯位置服务
1.7k 声望133 粉丝

立足生态,连接未来


引用和评论

0 条评论