您的位置:首页 > 汽车 > 新车 > Flutter集成高德导航SDK(Android篇)(JAVA语法)

Flutter集成高德导航SDK(Android篇)(JAVA语法)

2024/11/17 12:15:32 来源:https://blog.csdn.net/yaochangliang159/article/details/140101100  浏览:    关键词:Flutter集成高德导航SDK(Android篇)(JAVA语法)

先上flutter doctor:
flutter sdk版本为:3.19.4
在这里插入图片描述

引入依赖:
在app的build.gradle下,添加如下依赖:

    implementation 'com.amap.api:navi-3dmap:10.0.700_3dmap10.0.700'

navi-3dmap里面包含了定位功能,地图功能,所以引入这一个包就行

1.在项目的android-app-src-包名 路径下,新建一个class,起名为:AMapNaviPlugin,
AMapNaviPlugin代码如下:

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.View;import androidx.annotation.NonNull;import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AmapNaviPage;
import com.amap.api.navi.AmapNaviParams;
import com.amap.api.navi.AmapNaviType;
import com.amap.api.navi.AmapPageType;
import com.amap.api.navi.INaviInfoCallback;
import com.amap.api.navi.NaviSetting;
import com.amap.api.navi.model.AMapNaviLocation;import java.util.HashMap;
import java.util.Map;import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;public class AMapNaviPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler, ActivityAware {private static MethodChannel channel;private static Context mContext = null;//通讯名称,回到手机桌面private static String CHANNEL = "com.kdcf.channel/aMapNavi";private Activity activity;@Overridepublic void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {if (null == mContext) {mContext = binding.getApplicationContext();}initMethodChannel(binding.getBinaryMessenger());}private void initMethodChannel(BinaryMessenger binaryMessenger) {if (null == binaryMessenger) {return;}channel = new MethodChannel(binaryMessenger, CHANNEL);channel.setMethodCallHandler(this);}@Overridepublic void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {channel.setMethodCallHandler(null);channel = null;}@Overridepublic void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {if (mContext == null) {result.error("-1", "context is null", null);}if (call.method.equals(NaviConstant.START_NAVI)) {try {Object arguments = call.arguments;Map<String,Object> map= (Map<String, Object>) arguments;Double endLongitude= (Double) map.get("endLongitude");Double endLatitude= (Double) map.get("endLatitude");String endPlaceName= (String) map.get("endPlaceName");String endPoiId= (String) map.get("endPoiId");Map<String,Object> extraParams= (Map<String, Object>) map.get("extraParams");Log.e("AMapNaviPlugin","endLongitude:"+endLongitude);Log.e("AMapNaviPlugin","endLatitude:"+endLatitude);Log.e("AMapNaviPlugin","endPlaceName:"+endPlaceName);Log.e("AMapNaviPlugin","endPoiId:"+endPoiId);Log.e("AMapNaviPlugin","extraParams参数如下:");for (Map.Entry<String, Object> entry : extraParams.entrySet()) {System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());}startNavi(endLongitude,endLatitude,endPlaceName,endPoiId,extraParams);} catch (Exception e) {Log.e("AMapNaviPlugin","异常:"+e.toString());result.error("ERROR","AMapNaviPlugin onMethodCall startNavi出错:"+e.toString(),null);}result.success(200);}else if (call.method.equals(NaviConstant.AGREE_PRIVACY)) {try {NaviSetting.updatePrivacyShow(mContext, true, true);NaviSetting.updatePrivacyAgree(mContext, true);} catch (Exception e) {Log.e("AMapNaviPlugin",e.toString());result.error("ERROR","AMapNaviPlugin onMethodCall agreePrivacy出错:"+e.toString(),null);}result.success(200);}else if (call.method.equals(NaviConstant.CANCEL_NAVI)) {try {AMapNavi.getInstance(mContext).playTTS("导航已结束",true);//退出导航组件Handler handler = new Handler();handler.postDelayed(new Runnable() {@Overridepublic void run() {// 这里是延时后需要执行的代码//退出导航组件AmapNaviPage.getInstance().exitRouteActivity();}}, 5000);} catch (Exception e) {Log.e("AMapNaviPlugin",e.toString());result.error("ERROR","AMapNaviPlugin onMethodCall CANCEL_NAVI出错:"+e.toString(),null);}result.success(200);}}@Overridepublic void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {activity = binding.getActivity();}@Overridepublic void onDetachedFromActivityForConfigChanges() {}@Overridepublic void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {}@Overridepublic void onDetachedFromActivity() {}void startNavi(Double endLongitude,Double endLatitude,String endPlaceName,String endPoiId,Map<String,Object> extraParams){//起点
//        Poi start = new Poi("北京首都机场", new LatLng(40.080525,116.603039), "B000A28DAE");
//途经点
//        List<Poi> poiList = new ArrayList();
//        poiList.add(new Poi("故宫", new LatLng(39.918058,116.397026), "B000A8UIN8"));
//终点Poi end = new Poi(endPlaceName, new LatLng(endLatitude,endLongitude),endPoiId);
// 组件参数配置AmapNaviParams params = new AmapNaviParams(null, null, end, AmapNaviType.DRIVER, AmapPageType.NAVI);
// 启动组件AmapNaviPage.getInstance().showRouteActivity(mContext.getApplicationContext(), params, new NaviInfoCallback(mContext,channel,extraParams));}
}

新建NaviConstant类,代码如下:

public class NaviConstant {//开始导航,flutter向原生传值public static String START_NAVI="startNavi";//同意隐私政策,flutter向原生传值public static String AGREE_PRIVACY="agreePrivacy";//到达目的地,原生向flutter传值public static String ARRIVE_Destination="arriveDestination";//取消导航public static String CANCEL_NAVI="cancelNavi";
}

新建NaviInfoCallback类,代码如下:

package com.daohe.kdchufadriver.navi;import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.View;import com.amap.api.navi.INaviInfoCallback;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.AmapNaviPage;import java.util.HashMap;
import java.util.Map;import io.flutter.plugin.common.MethodChannel;public class NaviInfoCallback implements INaviInfoCallback {Context context;MethodChannel channel;Map<String,Object> extraParams;public NaviInfoCallback(Context context,MethodChannel channel,Map<String,Object> extraParams) {this.context=context;this.channel=channel;this.extraParams=extraParams;}@Overridepublic void onInitNaviFailure() {Log.e("NaviInfoCallback","onInitNaviFailure");}@Overridepublic void onGetNavigationText(String s) {Log.e("onGetNavigationText","onGetNavigationText:"+s);// //模拟到达终点
//        Map<String,Object> result=new HashMap<>();
//        result.put("result",extraParams);
//        channel.invokeMethod(NaviConstant.ARRIVE_Destination,result);
//        //退出导航组件
//        Handler handler = new Handler();
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                // 这里是延时后需要执行的代码
//                //退出导航组件
//                AmapNaviPage.getInstance().exitRouteActivity();
//            }
//        }, 5000);}@Overridepublic void onLocationChange(AMapNaviLocation aMapNaviLocation) {Log.e("onGetNavigationText","onLocationChange");}//isEmulaterNavi - true代表是模拟导航到达目的地,false代表实时导航到达目的地@Overridepublic void onArriveDestination(boolean isEmulaterNavi) {Map<String,Object> result=new HashMap<>();result.put("result",extraParams);channel.invokeMethod(NaviConstant.ARRIVE_Destination,result);//退出导航组件Handler handler = new Handler();handler.postDelayed(new Runnable() {@Overridepublic void run() {// 这里是延时后需要执行的代码//退出导航组件AmapNaviPage.getInstance().exitRouteActivity();}}, 5000);}@Overridepublic void onStartNavi(int i) {Log.e("onGetNavigationText","onStartNavi "+String.valueOf(i));}@Overridepublic void onCalculateRouteSuccess(int[] ints) {Log.e("onGetNavigationText","onCalculateRouteSuccess "+String.valueOf(ints));}@Overridepublic void onCalculateRouteFailure(int i) {Log.e("onGetNavigationText","onCalculateRouteFailure "+String.valueOf(i));}@Overridepublic void onStopSpeaking() {Log.e("onGetNavigationText","onStopSpeaking");}@Overridepublic void onReCalculateRoute(int i) {Log.e("onGetNavigationText","onReCalculateRoute "+String.valueOf(i));}@Overridepublic void onExitPage(int i) {Log.e("onGetNavigationText","onExitPage "+String.valueOf(i));}@Overridepublic void onStrategyChanged(int i) {Log.e("onGetNavigationText","onStrategyChanged "+String.valueOf(i));}@Overridepublic void onArrivedWayPoint(int i) {Log.e("onGetNavigationText","onArrivedWayPoint "+String.valueOf(i));}@Overridepublic void onMapTypeChanged(int i) {Log.e("onGetNavigationText","onMapTypeChanged "+String.valueOf(i));}@Overridepublic void onNaviDirectionChanged(int i) {Log.e("onGetNavigationText","onNaviDirectionChanged "+String.valueOf(i));}@Overridepublic void onDayAndNightModeChanged(int i) {Log.e("onGetNavigationText","onDayAndNightModeChanged "+String.valueOf(i));}@Overridepublic void onBroadcastModeChanged(int i) {Log.e("onGetNavigationText","onBroadcastModeChanged "+String.valueOf(i));}@Overridepublic void onScaleAutoChanged(boolean b) {Log.e("onGetNavigationText","onScaleAutoChanged "+b);}@Overridepublic View getCustomMiddleView() {return null;}@Overridepublic View getCustomNaviView() {return null;}@Overridepublic View getCustomNaviBottomView() {return null;}
}

android端的代码到这里就完了

下面是Flutter端的代码

在Flutter新建dart文件,起名amap_navi_plugin.dart
代码如下:

import 'dart:convert';import 'package:flutter/services.dart';
import 'package:flutter/material.dart';//司机到达目的地附近的回调
typedef OnArriveCallBack=void Function(dynamic params);
//高德导航插件
class AMapNaviPlugin {//开始导航static const START_NAVI="startNavi";//同意隐私政策static const AGREE_PRIVACY="agreePrivacy";//到达目的地给flutter回调static const ARRIVE_Destination="arriveDestination";//取消导航static const CANCEL_NAVI="cancelNavi";//初始化通信管道static const String CHANNEL = "com.kdcf.channel/aMapNavi";static const platform = MethodChannel(CHANNEL);//开始导航//参数目的地经度、纬度、地点名称static Future<void> startAmapNavi(double endLongitude,double endLatitude,String endPlaceName,String poiId,Map extraParams) async {//通知安卓返回,到手机桌面try {Map map=Map();map['endLongitude']=endLongitude;map['endLatitude']=endLatitude;map['endPlaceName']=endPlaceName;map['extraParams']=extraParams;map['endPoiId']=poiId;// map['endLongitude']=103.85741;// map['endLatitude']=36.05407;// map['endPoiId']='';final dynamic result = await platform.invokeMethod(START_NAVI,map);if (result==200) {debugPrint("导航成功");}} on PlatformException catch (e) {debugPrint("通信失败 AMapNaviPlugin startAmapNavi:失败 ${e.toString()}");print(e.toString());}catch(e){debugPrint("导航失败:${e.toString()}");}}static void agreePrivacy() async {//通知安卓返回,到手机桌面try {final int result=await platform.invokeMethod(AGREE_PRIVACY);if (result==200) {debugPrint("同意导航隐私政策成功");}} on PlatformException catch (e) {debugPrint("通信失败 AMapNaviPlugin agreePrivacy:失败 ${e.toString()}");print(e.toString());}catch(e){debugPrint("同意导航隐私政策失败:${e.toString()}");}}//原生往flutter发消息,导航快到目的地的时候static void arriveDestination(OnArriveCallBack onArriveCallBack) async {//通知安卓返回,到手机桌面try {Future<dynamic> handler(MethodCall call) async {debugPrint("flutter收到消息:arriveDestination");switch (call.method) {case ARRIVE_Destination:Map result=call.arguments;debugPrint("ARRIVE_Destination参数:"+jsonEncode(result));onArriveCallBack(result);break;}}platform.setMethodCallHandler((call) => handler(call));} on PlatformException catch (e) {debugPrint("通信失败 AMapNaviPlugin arriveDestination:失败 ${e.toString()}");print(e.toString());}catch(e){debugPrint("arriveDestination失败:${e.toString()}");}}//flutter往原生发消息,取消订单的时候 取消导航static void cancelNavi() async {//通知安卓返回,到手机桌面try {final int result=await platform.invokeMethod(CANCEL_NAVI);if (result==200) {debugPrint("取消导航成功");}} on PlatformException catch (e) {debugPrint("通信失败 AMapNaviPlugin cancelNavi:失败 ${e.toString()}");print(e.toString());}catch(e){debugPrint("取消导航失败:${e.toString()}");}}}

最后,使用方法

AMapNaviPlugin.startAmapNavi(longitude,latitude,destinationTitle,poiId,extraParams);

longitude为终点经度,latitude为终点纬度,destinationTitle为目的地的汉字名称,比如北京XXXX。poiId可以不传,extraParams可以不传。

最后,附上一个运行视频:

Flutter集成高德导航Android端插件

以后还会完成的相关文章如下:

目前本人已完成项目里面以下技术的实现,但是还没时间写博客,工作太忙了,以后会完善。。。待续。。。

1. Flutter集成高德导航SDK(IOS篇)(OC语法)
2. Flutter集成高德导航SDK并且自定义导航UI页面(Android篇)(JAVA语法)
3. Flutter集成高德导航SDK并且自定义导航UI页面(IOS篇)(OC语法)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com