更多内容请访问:与华为官方共建的鸿蒙科技社区https://harmonyos.51cto.com前言为什么还有修改版的儿童手表定位呢和活动范围监测?因为之前的项目虽然实现了,但是不实用。原因是,如果不具体查看地方的经纬度,没有人会准确知道这个地方的经纬度,所以不能设置活动范围。另外,之前项目的activityrange设置只能是一个矩形范围,而且只有少数地方是规则的矩形范围,不实用。因此,基于此,对场景进行细分,修改后的版本有如下两种:场景一:孩子在小区玩耍,家长将活动范围设置为小区,在孩子离开小区时提醒家长社区。场景二:孩子和家长外出,家长设置孩子与自己的距离,超过设定距离时提醒家长。概览场景一效果图如下:场景二效果图如下:正文一、场景一的实现1、复制之前项目的代码,创建一个EmptyJavaPhone应用程序,命名为ChildrenLocation2。基于儿童手表定位、活动范围监测和ChildrenLocation2的改进。2.修改父设备界面布局,在ability_phone.xml中写入如下代码。删除三个文本输入框,修改标识符id。3.重写getRange()函数,在MainAbilitySlice.java中写入如下代码。使用getText()方法获取设置的活动范围,使用contains()方法判断PlaceName与设置的活动范围的关系。如果PlaceName不在设置的活动范围内,则使用startAbility方法播放音频,否则使用stopAbility方法停止播放音频。privatevoidgetRange(){TextFieldtf_Location=(TextField)findComponentById(ResourceTable.Id_tf_Location);Texttext=(Text)findComponentById(ResourceTable.Id_IDIDID);StringLoc=tf_Location.getText();Intentserviceintent=newIntent();OperationserviceOperation=newIntent.OperationBuilder().withDeviceId("").withBundleName(getBundleName()).withAbilityName(ServiceAbility.class.getName()).build();serviceintent.setOperation(serviceOperation);if(!PlaceName.equals("null")){if(Loc.equals("")){text.setText("没有设置范围");stopAbility(serviceintent);}else{if(PlaceName.contains(Loc)){text.setText("孩子没有超过设置range");stopAbility(serviceintent);}else{text.setText("孩子超出设定范围");startAbility(serviceintent);}}}else{text.setText("无法获取孩子位置");stopAbility(serviceintent);}}2.实现场景21.复制之前的项目代码,创建一个名为ChildrenLocation3的EmptyJavaPhone应用程序。基于儿童手表定位、活动范围监测和ChildrenLocation2的改进。2.修改父设备界面布局,在ability_phone.xml中写入如下代码。删除三个文本输入框,修改标识符id。3。重写getRange()函数,在MainAbilitySlice.java中写入如下代码。添加Longitude_phone和Latitude_phone两个变量,初始化为“null”,用于记录父设备的经纬度信息。通过location.getLongitude()方法获取经度信息,通过location.getLatitude()方法获取纬度信息。通过getText()方法获取设置的活动距离,通过两个位置的经纬度计算距离,与设置的活动距离进行比较。根据大小关系,通过startAbility方法播放音频,通过stopAbility方法停止音频。privatestaticStringLongitude_phone="null";privatestaticStringLatitude_phone="null";privatevoidgetLocation(){writeData("Longitude",Longitude);writeData("Latitude",Latitude);writeData("PlaceName",PlaceName);writeData("CountryName",CountryName);timer_phone=newTimer();timer_phone.schedule(newTimerTask(){@Overridepublicvoidrun(){getUITaskDispatcher().asyncDispatch(newRunnable(){@Overridepublicvoidrun(){getRange();getSingleLocation();}});}},0,5000);}privatevoidgetRange(){TextFieldtf_Distance=(TextField)findComponentById(ResourceTable.Id_tf_Distance);Texttext=(Text)findComponentById(ResourceTable.Id_IDIDID);doubleDis;if(location==null){Longitude_phone="null";Latitude_phone="null";return;}Longitude_phone=Double.toString(location.getLongitude());Latitude_phone=Double.toString(location.getLatitude());if(tf_Distance.getText().equals("")){Dis=-1;}else{Dis=Double.parseDouble(tf_Distance.getText());}Intentserviceintent=newIntent();OperationserviceOperation=newIntent.OperationBuilder().withDeviceId("").withBundleName(getBundleName()).withAbilityName(ServiceAbility.class.getName()).build();serviceintent.setOperation(serviceOperation);if(!(Longitude.equals("null")&&Latitude.equals("null")&&Longitude_phone.equals("null")&&Latitude_phone.equals("null"))){doubleLon=getRadian(Double.parseDouble(Longitude));doubleLat=getRadian(Double.parseDouble(Latitude));doubleLon_phone=getRadian(Double.parseDouble(Longitude_phone));doubleLat_phone=getRadian(Double.parseDouble(Latitude_phone));doubledistance=2*6371.393*1000*Math.asin(Math.sqrt(Math.sin((Lat_phone-Lat)/2)*Math.sin((Lat_phone-Lat)/2))+Math.cos(Lat)*Math.cos(Lat_phone)*Math.sqrt(Math.sin((Lon_phone-Lon)/2)*Math.sin((Lon_phone-Lon)/2)));distance=Double.parseDouble(Double.toString(distance).substring(0,7));if(Dis!=-1){if(distance<=Dis){text.setText("你离孩子远"+distance+"米,不超出距离");stopAbility(serviceintent);}else{text.setText("你离孩子远了"+distance+"米,距离已超");startAbility(serviceintent);}}else{text.setText("你离孩子远了"+distance+"meter,Youhavenotsetthedistance");stopAbility(serviceintent);}}else{text.setText("没有获取位置");stopAbility(serviceintent);}}privatedoublegetRadian(doubledegree){//转换为radianreturndegree/180*Math.PI;}文章相关附件,可点击下方链接下载原文。