HarmonyOS分布式应用智能三角警示牌解读

开发 前端 分布式 OpenHarmony
我们设计了智能移动三角警示牌以解决该问题。本智能三角警示牌通过手机HAP可以与其相连,控制运动方向和速度,使其停放在事故现场后方合适的位置,从而能够保障用户的人身和财产安全。

[[439635]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

前言

HarmonyOS是 一款面向万物互联时代的、全新的分布式操作系统,其分布式技术能力(分布式软总线、分布式设备虚拟化、分布式数据管理、分布式任务调度)一直受到广大开发者的极大关注,使用户对HarmonyOS有着很高的赞许。

我们开发的《分布式智能三角警示牌应用》,以在日常生活中,公路上发生交通事故时通常是事故相关人员手持三角反光警示牌步行至目的地处放置,人为放置具有引发二次事故的风险,因此我们设计了智能移动三角警示牌以解决该问题。本智能三角警示牌通过手机HAP可以与其相连,控制运动方向和速度,使其停放在事故现场后方合适的位置,从而能够保障用户的人身和财产安全。

当在控制警示牌运动过程中,有紧急事情或其它情况,可以将当前的操作流转到另外一台设备中进行操作,其运动轨迹就是通过分布式数据服务来保证两台设备间数据的一致性。

效果展示

#星光计划2.0#HarmonyOS分布式应用智能三角警示牌解读-鸿蒙HarmonyOS技术社区
#星光计划2.0#HarmonyOS分布式应用智能三角警示牌解读-鸿蒙HarmonyOS技术社区

一、创建“智能三角警示牌”HAP工程

1、安装和配置DevEco Studio

2.1 Release

安装的链接:https://developer.harmonyos.com/cn/develop/deveco-studio

IDE的使用指南,很详细:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/tools_overview-0000001053582387

我的本案例使用的最新的 2.1.0.501版本,SDK:API Version 5

2、选择一个模版,创建一个Java Phone应用

#星光计划2.0#HarmonyOS分布式应用智能三角警示牌解读-鸿蒙HarmonyOS技术社区

==点击Next ==

#星光计划2.0#HarmonyOS分布式应用智能三角警示牌解读-鸿蒙HarmonyOS技术社区

点击Finish完成创建HAP工程

3、智能三角警示牌应用包结构

首先完成智能三角警示牌应用包结构设计,结构如下:

#星光计划2.0#HarmonyOS分布式应用智能三角警示牌解读-鸿蒙HarmonyOS技术社区 

二、智能三角警示牌应用核心代码实现

1、config.json权限配置

  1. "reqPermissions": [ 
  2.       { 
  3.         "name""ohos.permission.INTERNET" 
  4.       }, 
  5.       { 
  6.         "name""ohos.permission.GET_NETWORK_INFO" 
  7.       }, 
  8.       { 
  9.         "name""ohos.permission.MICROPHONE" 
  10.       }, 
  11.       { 
  12.         "name""android.permission.RECORD_AUDIO" 
  13.       }, 
  14.       { 
  15.         "name""ohos.permission.DISTRIBUTED_DATASYNC" 
  16.       }, 
  17.       { 
  18.         "name""ohos.permission.servicebus.ACCESS_SERVICE" 
  19.       }, 
  20.       { 
  21.         "name""com.huawei.hwddmp.servicebus.BIND_SERVICE" 
  22.       }, 
  23.       { 
  24.         "name""ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE" 
  25.       }, 
  26.       { 
  27.         "name""ohos.permission.GET_DISTRIBUTED_DEVICE_INFO" 
  28.       }, 
  29.       { 
  30.         "name""ohos.permission.GET_BUNDLE_INFO" 
  31.       }, 
  32.       { 
  33.         "name": "ohos.p 
  34. ermission.LOCATION" 
  35.       } 
  36.     ] 

2、分布式数据服务核心代码

  1. import com.google.gson.Gson; 
  2. import com.google.gson.reflect.TypeToken; 
  3. import com.isoftstone.smartcar.app.common.Constant; 
  4. import com.isoftstone.smartcar.app.model.DrivingMap; 
  5. import com.isoftstone.smartcar.app.model.DrivingRecord; 
  6. import com.isoftstone.smartcar.app.model.ReportRecord; 
  7. import com.isoftstone.smartcar.app.utils.DrivingReportComparator; 
  8. import com.isoftstone.smartcar.app.utils.ReportRecordComparator; 
  9. import com.isoftstone.smartcar.app.utils.StringUtils; 
  10. import ohos.app.Context; 
  11. import ohos.data.distributed.common.*; 
  12. import ohos.data.distributed.device.DeviceFilterStrategy; 
  13. import ohos.data.distributed.device.DeviceInfo; 
  14. import ohos.data.distributed.user.SingleKvStore; 
  15. import ohos.hiviewdfx.HiLog; 
  16. import ohos.hiviewdfx.HiLogLabel; 
  17. import java.util.ArrayList; 
  18. import java.util.List; 
  19. /** 
  20.  * 分布式数据库类服务 
  21.  */ 
  22. public class SmartShareService { 
  23.     private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "SmartshareService"); 
  24.     private static SmartShareService smartShareService; 
  25.     private static SingleKvStore singleKvStore; 
  26.     private static KvManager kvManager; 
  27.     private final Context context; 
  28.     public static SmartShareService getInstance(Context context) { 
  29.         if (smartShareService == null) { 
  30.             smartShareService = new SmartShareService(context); 
  31.         } 
  32.         return smartShareService; 
  33.     } 
  34.     private SmartShareService(Context context){ 
  35.             this.context = context; 
  36.     } 
  37.     /** 
  38.      * 分布式数据库初始化 
  39.      */ 
  40.     public void init() { 
  41.         KvManagerConfig config = new KvManagerConfig(context); 
  42.         kvManager = KvManagerFactory.getInstance().createKvManager(config); 
  43.         Options options = new Options();        options.setCreateIfMissing(true).setEncrypt(false).setKvStoreType(KvStoreType.SINGLE_VERSION).setAutoSync(true); 
  44.         singleKvStore = kvManager.getKvStore(options, Constant.SMART_SHARE_NAME); 
  45.         HiLog.info(label,"初始化成功!"); 
  46.         //return singleKvStore; 
  47.     } 
  48.     /** 
  49.      * 新增行驶记录 
  50.      * @param drivingRecord 行驶记录对象 
  51.      */ 
  52.     public void insertDrivingRecord(DrivingRecord drivingRecord){ 
  53.         Gson gson = new Gson(); 
  54.         String resultJson = gson.toJson(drivingRecord);        singleKvStore.putString(Constant.DRIVING_REPORT_PREFIX+drivingRecord.getId(),resultJson); 
  55.         HiLog.info(label,"新增行驶记录成功!"); 
  56.     } 
  57.     /** 
  58.      * 批量新增行驶记录轨迹 
  59.      * @param drivingId 行驶记录标识 
  60.      * @param drivingMapLst 行驶记录轨迹列表 
  61.      */ 
  62.     public void insertDrivingMap(String drivingId, List<DrivingMap> drivingMapLst){ 
  63.         Gson gson = new Gson(); 
  64.         String resultJson = gson.toJson(drivingMapLst);        singleKvStore.putString(Constant.DRIVING_MAP_PREFIX+drivingId,resultJson); 
  65.         HiLog.info(label,"批量新增行驶记录轨迹成功!"); 
  66.     } 
  67.     /** 
  68.      * 新增上报记录 
  69.      * @param drivingId 行驶记录标识 
  70.      * @param reportRecord 上报记录对象 
  71.      */ 
  72.     public void insertReportRecord(String drivingId, ReportRecord reportRecord){ 
  73.         Gson gson = new Gson(); 
  74.         List<Entry> entrys = singleKvStore.getEntries(Constant.REPORT_RECORD_PREFIX+drivingId); 
  75.         if (entrys == null || entrys.size() == 0){ 
  76.             List<ReportRecord> reportRecordLst = new ArrayList<>(); 
  77.             reportRecordLst.add(reportRecord); 
  78.             String resultJson1 = gson.toJson(reportRecordLst);            singleKvStore.putString(Constant.REPORT_RECORD_PREFIX+drivingId,resultJson1); 
  79.             HiLog.info(label,"新增上报记录成功!"); 
  80.         } else { 
  81.             String resultJson = entrys.get(0).getValue().getString(); 
  82.             List<ReportRecord> reportRecordLst = gson.fromJson(resultJson, new TypeToken<List<ReportRecord>>() {}.getType()); 
  83.             reportRecordLst.add(reportRecord); 
  84.             String resultJson1 = gson.toJson(reportRecordLst); 
  85.             singleKvStore.putString(Constant.REPORT_RECORD_PREFIX+drivingId,resultJson1); 
  86.             HiLog.info(label,"新增上报记录列表成功!"); 
  87.         } 
  88.     } 
  89.     /** 
  90.      * 设置保险电话 
  91.      * @param key       保险key 
  92.      * @param telphone  保险key对应的值 
  93.      */ 
  94.     public void setupInsuranceTelphone(String key,String telphone){ 
  95.         singleKvStore.putString(Constant.TEL_PREFIX+key,telphone); 
  96.         HiLog.info(label,"设置保险电话成功!"); 
  97.     } 
  98.     /** 
  99.      * 获取保险电话 
  100.      * @param key 保险电话key 
  101.      * @return 保险电话 
  102.      */ 
  103.     public String getInsuranceTelphone(String key){ 
  104.         String tel = ""
  105.         List<Entry> entrys = singleKvStore.getEntries(Constant.TEL_PREFIX+key); 
  106.         if (entrys != null && entrys.size()>0) { 
  107.             tel = entrys.get(0).getValue().getString(); 
  108.             HiLog.info(label,"获取保险电话成功!"+tel); 
  109.         } else { 
  110.             HiLog.info(label,"没有获取保险电话!"); 
  111.         } 
  112.         return tel; 
  113.     } 
  114.     /** 
  115.      * 设置IP 
  116.      * @param key  IP的key 
  117.      * @param value IP的value 
  118.      */ 
  119.     public void setIp(String key,String value){ 
  120.         singleKvStore.putString(Constant.IP_PREFIX+key,value); 
  121.         HiLog.info(label,"设置IP成功!"); 
  122.     } 
  123.     /** 
  124.      * 获取IP 
  125.      * @param key  IP的key 
  126.      * @return IP的值 
  127.      */ 
  128.     public String getIp(String key){ 
  129.         String tmpIp = ""
  130.         List<Entry> entrys = singleKvStore.getEntries(Constant.IP_PREFIX+key); 
  131.         if (entrys != null && entrys.size()>0) { 
  132.             tmpIp = entrys.get(0).getValue().getString(); 
  133.             HiLog.info(label,"获取IP成功!"+tmpIp); 
  134.         } else { 
  135.             HiLog.info(label,"没有获取到IP!"); 
  136.         } 
  137.         return tmpIp; 
  138.     } 
  139.     /** 
  140.      * 获取行驶记录列表 
  141.      * @return 行驶记录列表 
  142.      */ 
  143.     public List<DrivingRecord> getDrivingRecords(){ 
  144.         List<DrivingRecord> drivingReporList = new ArrayList<>(); 
  145.         List<Entry> entrys = singleKvStore.getEntries(Constant.DRIVING_REPORT_PREFIX); 
  146.         for(Entry entry:entrys){ 
  147.             String key = entry.getKey(); 
  148.             String value = entry.getValue().getString(); 
  149.             HiLog.info(label,"获取到行驶记录的数据:key:" + key+",value:"+value); 
  150.             Gson gson = new Gson(); 
  151.             DrivingRecord drivingRecord = gson.fromJson(value, DrivingRecord.class); 
  152.             //HiLog.info(label,drivingRecord.getId()); 
  153.             drivingReporList.add(drivingRecord); 
  154.         } 
  155.         //排序 
  156.         if (drivingReporList.size() > 0) { 
  157.             DrivingReportComparator drivingReportComparator = new DrivingReportComparator(); 
  158.             drivingReporList.sort(drivingReportComparator); 
  159.         } 
  160.         return drivingReporList; 
  161.     } 
  162.     /** 
  163.      * 获取行驶记录轨迹列表 
  164.      * @param drivingId 行驶记录标识 
  165.      * @return  行驶记录轨迹列表 
  166.      */ 
  167.     public List<DrivingMap> getDrivingMap(String drivingId){ 
  168.         String resultJson = singleKvStore.getString(Constant.DRIVING_MAP_PREFIX+drivingId); 
  169.         Gson gson = new Gson(); 
  170.  
  171.         return gson.fromJson(resultJson, new TypeToken<List<DrivingMap>>() {}.getType()); 
  172.     } 
  173.     /** 
  174.      * 获取上报记录列表 
  175.      * @param drivingId 行驶记录标识 
  176.      * @return 上报记录列表 
  177.      */ 
  178.     public List<ReportRecord> getReportRecords(String drivingId){ 
  179.         List<Entry> entrys = singleKvStore.getEntries(Constant.REPORT_RECORD_PREFIX+drivingId); 
  180.         if (entrys == null || entrys.size() == 0){ 
  181.             HiLog.info(label,"获取上报记录为空!"); 
  182.             return null
  183.         } else { 
  184.             Gson gson = new Gson(); 
  185.             Entry entry = entrys.get(0); 
  186.             String resultJson = entry.getValue().getString(); 
  187.             List<ReportRecord> reportRecordLst = gson.fromJson(resultJson, new TypeToken<List<ReportRecord>>() {}.getType()); 
  188.             HiLog.info(label,"获取上报记录成功!"+reportRecordLst.size()); 
  189.  
  190.             if (reportRecordLst!=null && reportRecordLst.size() > 0) { 
  191.                 //排序 
  192.                 ReportRecordComparator reportRecordComparator = new ReportRecordComparator(); 
  193.                 reportRecordLst.sort(reportRecordComparator); 
  194.             } 
  195.             return reportRecordLst; 
  196.         } 
  197.     } 
  198.     /** 
  199.      * 同步数据 
  200.      */ 
  201.     public void syncData() { 
  202.         List<DeviceInfo> deviceInfoList = kvManager.getConnectedDevicesInfo(DeviceFilterStrategy.NO_FILTER); 
  203.         List<String> deviceIdList = new ArrayList<>(); 
  204.         String deviceId; 
  205.         for (DeviceInfo deviceInfo : deviceInfoList) { 
  206.             deviceId = deviceInfo.getId(); 
  207.             HiLog.info(label,"deviceId = " + deviceId); 
  208.             deviceIdList.add(deviceId); 
  209.         } 
  210.         HiLog.info(label,"deviceIdList.size() = " + deviceIdList.size()); 
  211.         if (deviceIdList.size() > 0) { 
  212.             singleKvStore.sync(deviceIdList, SyncMode.PUSH_ONLY); 
  213.         } else { 
  214.             HiLog.error(label,"没有共享设备"); 
  215.         } 
  216.     } 
  217.     /** 
  218.      * 注册回调接口 
  219.      * @param kvStoreObserver  kvStore对象 
  220.      */ 
  221.     public void registerCallback(KvStoreObserver kvStoreObserver) { 
  222.         singleKvStore.subscribe(SubscribeType.SUBSCRIBE_TYPE_ALL, kvStoreObserver); 
  223.     } 
  224.     /** 
  225.      * 清空数据 
  226.      */ 
  227.     public void clearAllData() { 
  228.         List<Entry> entrys = singleKvStore.getEntries(""); 
  229.         if (entrys!=null && entrys.size()>0){ 
  230.             for(Entry entry:entrys){ 
  231.                 singleKvStore.delete(entry.getKey()); 
  232.             } 
  233.             HiLog.info(label,"清空数据成功"); 
  234.         } else { 
  235.             HiLog.info(label,"没有数据要清空"); 
  236.         } 
  237.     } 
  238.     /** 
  239.      * 从现有的行驶记录中获取drivingId 
  240.      * @return 返回行驶记录标识 
  241.      */ 
  242.     public String getDrivingId(){ 
  243.         String drivingId = ""
  244.         List<Entry> entrys = singleKvStore.getEntries(Constant.DRIVING_REPORT_PREFIX); 
  245.         if (entrys != null && entrys.size() > 0){ 
  246.             List<DrivingRecord> drivingReporList = new ArrayList<>(); 
  247.             for(Entry entry:entrys){ 
  248.                 String value = entry.getValue().getString(); 
  249.                 Gson gson = new Gson(); 
  250.                 DrivingRecord drivingRecord = gson.fromJson(value, DrivingRecord.class); 
  251.                 String dateStr = drivingRecord.getDrivingDate(); 
  252.                 if (StringUtils.isDiffHour(dateStr)){ 
  253.                     drivingReporList.add(drivingRecord); 
  254.                 } 
  255.                 HiLog.info(label,drivingRecord.getId()); 
  256.                 drivingReporList.add(drivingRecord); 
  257.             } 
  258.             if (drivingReporList.size() > 0) { 
  259.                 //排序 
  260.                 DrivingReportComparator drivingReportComparator = new DrivingReportComparator(); 
  261.                 drivingReporList.sort(drivingReportComparator); 
  262.  
  263.                 drivingId = drivingReporList.get(0).getId(); 
  264.                 HiLog.info(label,"找到符合条件的drivingId:"+drivingId); 
  265.             } else { 
  266.                 HiLog.info(label,"没有找到符合条件的drivingId"); 
  267.             } 
  268.         } else { 
  269.             HiLog.info(label,"行驶记录为空,没有找到符合条件的drivingId"); 
  270.         } 
  271.  
  272.         return drivingId; 
  273.     } 

3、行驶记录代码

  1. import com.isoftstone.smartcar.app.ResourceTable; 
  2. import com.isoftstone.smartcar.app.model.DrivingRecord; 
  3. import com.isoftstone.smartcar.app.provider.DrivingRecordProvider; 
  4. import com.isoftstone.smartcar.app.service.SmartShareService; 
  5. import ohos.aafwk.ability.AbilitySlice; 
  6. import ohos.aafwk.content.Intent; 
  7. import ohos.aafwk.content.Operation; 
  8. import ohos.agp.components.Component; 
  9. import ohos.agp.components.Image; 
  10. import ohos.agp.components.ListContainer; 
  11. import java.util.List; 
  12. /** 
  13.  * 行驶记录 
  14.  */ 
  15. public class DrivingRecordsAbilitySlice extends AbilitySlice implements Component.ClickedListener { 
  16.     private ListContainer lcRecords; 
  17.     private SmartShareService shareService; 
  18.     private List<DrivingRecord> drivingRecordList; 
  19.     private DrivingRecordProvider drivingRecordProvider; 
  20.     @Override 
  21.     public void onStart(Intent intent) { 
  22.         super.onStart(intent); 
  23.         super.setUIContent(ResourceTable.Layout_ability_driving_records); 
  24.         initUI(); 
  25.     } 
  26.     private void initUI() { 
  27.         shareService = SmartShareService.getInstance(this); 
  28.         shareService.init(); 
  29.         Image iBack = (Image) findComponentById(ResourceTable.Id_i_back); 
  30.         iBack.setClickedListener(this); 
  31.         lcRecords = (ListContainer) findComponentById(ResourceTable.Id_lc_records); 
  32.         drivingRecordList = getData(); 
  33.         drivingRecordProvider = new DrivingRecordProvider(drivingRecordList, this); 
  34.         lcRecords.setItemProvider(drivingRecordProvider); 
  35.         lcRecords.setItemClickedListener(new ListContainer.ItemClickedListener() { 
  36.             @Override 
  37.             public void onItemClicked(ListContainer listContainer, Component component, int i, long l) { 
  38.                 Intent intent = new Intent(); 
  39.                 DrivingRecord drivingRecord = (DrivingRecord) drivingRecordProvider.getItem(i); 
  40.                 intent.setParam("drivingId", drivingRecord.getId()); 
  41.                 intent.setParam("lat", drivingRecord.getLatitude()); 
  42.                 intent.setParam("lng", drivingRecord.getLongitude()); 
  43.                 Operation operation = new Intent.OperationBuilder() 
  44.                         .withDeviceId(""
  45.                         .withBundleName("com.isoftstone.smartcar.app"
  46.                         .withAbilityName("com.isoftstone.smartcar.app.DrivingRecordsDetailAbility"
  47.                         .build(); 
  48.                 intent.setOperation(operation); 
  49.                 startAbility(intent); 
  50.                 //terminate(); 
  51.             } 
  52.         }); 
  53.     } 
  54.     @Override 
  55.     public void onActive() { 
  56.         super.onActive(); 
  57.         drivingRecordList = getData(); 
  58.         drivingRecordProvider = new DrivingRecordProvider(drivingRecordList, this); 
  59.         lcRecords.setItemProvider(drivingRecordProvider); 
  60.     } 
  61.     @Override 
  62.     public void onForeground(Intent intent) { 
  63.         super.onForeground(intent); 
  64.     } 
  65.     @Override 
  66.     public void onClick(Component component) { 
  67.         if (component.getId() == ResourceTable.Id_i_back) { 
  68.             terminateAbility(); 
  69.         } 
  70.     } 
  71.     private List<DrivingRecord> getData() { 
  72.         return shareService.getDrivingRecords(); 
  73.     } 

4、行驶记录详情代码

  1. import com.isoftstone.smartcar.app.ResourceTable; 
  2. import com.isoftstone.smartcar.app.model.DrivingMap; 
  3. import com.isoftstone.smartcar.app.model.ReportRecord; 
  4. import com.isoftstone.smartcar.app.provider.ReportRecordProvider; 
  5. import com.isoftstone.smartcar.app.service.SmartShareService; 
  6. import com.isoftstone.smartcar.app.utils.CoordinateConverter; 
  7. import com.isoftstone.smartcar.app.widget.carmap.LatLng; 
  8. import com.isoftstone.smartcar.app.widget.carmap.TinyMap; 
  9. import ohos.aafwk.ability.AbilitySlice; 
  10. import ohos.aafwk.content.Intent; 
  11. import ohos.agp.components.Component; 
  12. import ohos.agp.components.Image; 
  13. import ohos.agp.components.ListContainer; 
  14. import ohos.agp.utils.Point; 
  15. import ohos.hiviewdfx.HiLog; 
  16. import ohos.hiviewdfx.HiLogLabel; 
  17. import ohos.multimodalinput.event.KeyEvent; 
  18. import java.util.ArrayList; 
  19. import java.util.List; 
  20. import java.util.Timer; 
  21. import java.util.TimerTask; 
  22.  
  23. /** 
  24.  * 行驶记录详情 
  25.  */ 
  26. public class DrivingRecordsDetailAbilitySlice extends AbilitySlice implements Component.ClickedListener { 
  27.     private static final HiLogLabel logLabel = new HiLogLabel(HiLog.LOG_APP, 0x00100, "DrivingRecordsDetailAbilitySlice"); 
  28.  
  29.     private TinyMap map; 
  30.     private String drivingId; 
  31.     private SmartShareService shareService; 
  32.  
  33.     @Override 
  34.     protected void onStart(Intent intent) { 
  35.         super.onStart(intent); 
  36.         super.setUIContent(ResourceTable.Layout_ability_driving_records_detail); 
  37.         initUI(intent); 
  38.     } 
  39.  
  40.     private void initUI(Intent intent) { 
  41.         Image iBack = (Image) findComponentById(ResourceTable.Id_i_back); 
  42.         iBack.setClickedListener(this); 
  43.         map = (TinyMap) findComponentById(ResourceTable.Id_map); 
  44.         ListContainer reportListContainer = (ListContainer) findComponentById(ResourceTable.Id_report_records); 
  45.         drivingId = intent.getStringParam("drivingId"); 
  46.         shareService = SmartShareService.getInstance(this); 
  47.         shareService.init(); 
  48.  
  49.         new Timer().schedule(new TimerTask() { 
  50.             @Override 
  51.             public void run() { 
  52.                 getUITaskDispatcher().asyncDispatch(new Runnable() { 
  53.                     @Override 
  54.                     public void run() { 
  55.                         setMap(); 
  56.                     } 
  57.                 }); 
  58.             } 
  59.         },500); 
  60.  
  61.         List<ReportRecord> reportRecords = shareService.getReportRecords(drivingId); 
  62.         ReportRecordProvider reportRecordProvider = new ReportRecordProvider(reportRecords, this); 
  63.         reportListContainer.setItemProvider(reportRecordProvider); 
  64.     } 
  65.  
  66.     @Override 
  67.     public void onClick(Component component) { 
  68.         if (component.getId() == ResourceTable.Id_i_back) {//present(new DrivingRecordsAbilitySlice(), new Intent()); 
  69.             terminate(); 
  70.         } 
  71.     } 
  72.  
  73.     @Override 
  74.     public boolean onKeyUp(int keyCode, KeyEvent keyEvent) { 
  75.         HiLog.error(logLabel,keyEvent.getKeyCode()+""); 
  76.         if (keyCode==KeyEvent.KEY_BACK){ 
  77.             //present(new DrivingRecordsAbilitySlice(), new Intent()); 
  78.             terminate(); 
  79.             return true
  80.         } 
  81.         return super.onKeyDown(keyCode, keyEvent); 
  82.     } 
  83.  
  84.     private LatLng WGS84ToMercator(LatLng latLng) { 
  85.         return CoordinateConverter.WGS84ToMercator(latLng.getLng(), latLng.getLat()); 
  86.     } 
  87.  
  88.     private void setMap() { 
  89.         map.initMap(); 
  90.         List<DrivingMap> drivingMaps = shareService.getDrivingMap(drivingId); 
  91.  
  92.         LatLng startLatLng0 = new LatLng(Double.parseDouble(drivingMaps.get(0).getLatitude()), Double.parseDouble(drivingMaps.get(0).getLongitude())); 
  93.         LatLng startLatLng = new LatLng((float) WGS84ToMercator(startLatLng0).getLat(), (float) WGS84ToMercator(startLatLng0).getLng()); 
  94.         map.setCenterPoint((float) startLatLng.getLng(), (float) startLatLng.getLat()); 
  95.  
  96.         LatLng endLatLng0 = new LatLng(Double.parseDouble(drivingMaps.get(drivingMaps.size() - 1).getLatitude()), Double.parseDouble(drivingMaps.get(drivingMaps.size() - 1).getLongitude())); 
  97.         LatLng endLatLng = new LatLng((float) WGS84ToMercator(endLatLng0).getLat(), (float) WGS84ToMercator(endLatLng0).getLng()); 
  98.         map.setStartElement((float) startLatLng.getLng(), (float) startLatLng.getLat(), ResourceTable.Media_start_location); 
  99.         map.setEndElement((float) endLatLng.getLng(), (float) endLatLng.getLat(), ResourceTable.Media_end_location); 
  100.         List<LatLng> latLngs = new ArrayList<>(); 
  101.         for (DrivingMap drivingMap : drivingMaps) { 
  102.             LatLng latLng = new LatLng(Double.parseDouble(drivingMap.getLatitude()), Double.parseDouble(drivingMap.getLongitude())); 
  103.             Point p = new Point((float) WGS84ToMercator(latLng).getLng(), (float) WGS84ToMercator(latLng).getLat()); 
  104.             latLngs.add(new LatLng(p.getPointY(), p.getPointX())); 
  105.         } 
  106.         map.setPaths(latLngs); 
  107.     } 

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-12-13 11:07:10

鸿蒙HarmonyOS应用

2021-12-14 14:47:18

鸿蒙HarmonyOS应用

2021-07-23 08:57:32

鸿蒙HarmonyOS应用

2021-10-21 10:03:09

鸿蒙HarmonyOS应用

2021-11-16 09:38:10

鸿蒙HarmonyOS应用

2021-07-22 10:20:21

鸿蒙HarmonyOS应用

2020-11-20 09:45:19

HarmonyOS

2020-09-29 19:20:05

鸿蒙

2021-01-21 09:45:36

鸿蒙HarmonyOS分布式

2019-02-13 13:41:07

MemCache分布式HashMap

2021-05-31 20:24:16

鸿蒙HarmonyOS应用

2020-11-06 12:12:35

HarmonyOS

2013-06-18 14:00:59

HDFS分布式文件系统

2021-05-28 09:52:00

鸿蒙HarmonyOS应用

2012-07-20 14:40:22

2019-04-30 14:17:56

中关村零售业创业者

2020-11-19 11:43:26

HarmonyOS

2022-07-15 16:26:58

Java分布式

2018-12-14 10:06:22

缓存分布式系统

2019-10-10 09:16:34

Zookeeper架构分布式
点赞
收藏

51CTO技术栈公众号