儿童手表定位,活动范围监控—修改版

系统
孩子戴着手表去上补习班、在小区内玩耍等等,手表可以将实时位置发送给家长,超出设置的活动范围内时提醒家长,实现孩子安全防患于未然。

[[420005]]

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

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

https://harmonyos.51cto.com

前言

为什么还会有修改版的儿童手表定位,活动范围监控呢?因为上一个项目虽然已经实现了,但是不实用。原因就是如果不是特地去查询该地方的经纬度,没有谁会准确知道这个地方的经纬度,那么就不能设定活动范围了。再者,上一个项目的活动范围设置只能是矩形范围,但是只有极少数的地方是规则的矩形范围,所以并不实用。所以针对此原因,并且对场景进行细分,有了如下两个修改版:

场景一:小孩在小区玩,家长设定活动范围即为该小区,孩子离开小区时提醒家长。

场景二:孩子和家长一起外出,家长设定孩子离自己的距离,超过该设定距离时提醒家长。

概述

场景一效果图如下:

场景二效果图如下:

正文

一、实现场景一

1. 复制上一个项目代码

创建一个名为ChildrenLocation2的Empty Java Phone应用。

儿童手表定位,活动范围监控——修改版-鸿蒙HarmonyOS技术社区

儿童手表定位,活动范围监控完善ChildrenLocation2。

2. 修改家长端设备界面布局

在ability_phone.xml中编写以下代码。

删除三个文本输入框,并修改标识符id。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:alignment="top|center" 
  7.     ohos:orientation="vertical"
  8.  
  9.     <Text 
  10.         ohos:id="$+id:Longitude" 
  11.         ohos:height="match_content" 
  12.         ohos:width="match_parent" 
  13.         ohos:margin="10vp" 
  14.         ohos:padding="10vp" 
  15.         ohos:text="---" 
  16.         ohos:text_size="28fp" 
  17.         ohos:text_color="#000000" 
  18.         ohos:text_alignment="left|vertical_center" 
  19.         ohos:background_element="#78C6C5"/> 
  20.  
  21.     <Text 
  22.         ohos:id="$+id:Latitude" 
  23.         ohos:height="match_content" 
  24.         ohos:width="match_parent" 
  25.         ohos:margin="10vp" 
  26.         ohos:padding="10vp" 
  27.         ohos:text="---" 
  28.         ohos:text_size="28fp" 
  29.         ohos:text_color="#000000" 
  30.         ohos:text_alignment="left|vertical_center" 
  31.         ohos:background_element="#78C6C5"/> 
  32.  
  33.     <Text 
  34.         ohos:id="$+id:CountryName" 
  35.         ohos:height="match_content" 
  36.         ohos:width="match_parent" 
  37.         ohos:margin="10vp" 
  38.         ohos:padding="10vp" 
  39.         ohos:text="---" 
  40.         ohos:text_size="28fp" 
  41.         ohos:text_color="#000000" 
  42.         ohos:text_alignment="left|vertical_center" 
  43.         ohos:background_element="#78C6C5"/> 
  44.  
  45.     <Text 
  46.         ohos:id="$+id:PlaceName" 
  47.         ohos:height="match_content" 
  48.         ohos:width="match_parent" 
  49.         ohos:margin="10vp" 
  50.         ohos:padding="10vp" 
  51.         ohos:text="---" 
  52.         ohos:text_size="28fp" 
  53.         ohos:text_color="#000000" 
  54.         ohos:text_alignment="left|vertical_center" 
  55.         ohos:background_element="#78C6C5" 
  56.         ohos:multiple_lines="true"/> 
  57.  
  58.     <TextField 
  59.         ohos:id="$+id:tf_Location" 
  60.         ohos:height="match_content" 
  61.         ohos:width="match_parent" 
  62.         ohos:margin="10vp" 
  63.         ohos:padding="10vp" 
  64.         ohos:hint="请输入活动位置......" 
  65.         ohos:text_size="28fp" 
  66.         ohos:text_color="#000000" 
  67.         ohos:text_alignment="left|vertical_center" 
  68.         ohos:background_element="#BEBEBE" 
  69.         ohos:multiple_lines="true"/> 
  70.  
  71.     <Text 
  72.         ohos:id="$+id:IDIDID" 
  73.         ohos:height="match_content" 
  74.         ohos:width="match_parent" 
  75.         ohos:margin="10vp" 
  76.         ohos:padding="10vp" 
  77.         ohos:text="---" 
  78.         ohos:text_size="28fp" 
  79.         ohos:text_color="#000000" 
  80.         ohos:text_alignment="left|vertical_center" 
  81.         ohos:background_element="#78C6C5" 
  82.         ohos:multiple_lines="true"/> 
  83.  
  84. </DirectionalLayout> 

3. 重写getRange()函数。

在MainAbilitySlice.java中编写以下代码。

通过getText()方法获取设置的活动范围,通过contains()方法判断PlaceName与设置的活动范围的关系,如果PlaceName不在设置的活动范围内则通过startAbility方法播放音频,否则通过stopAbility方法停止播放音频。

  1. private void getRange(){ 
  2.       TextField tf_Location = (TextField) findComponentById(ResourceTable.Id_tf_Location); 
  3.  
  4.       Text text = (Text) findComponentById(ResourceTable.Id_IDIDID); 
  5.  
  6.       String Loc = tf_Location.getText(); 
  7.  
  8.       Intent serviceintent = new Intent(); 
  9.       Operation serviceOperation = new Intent.OperationBuilder() 
  10.               .withDeviceId(""
  11.               .withBundleName(getBundleName()) 
  12.               .withAbilityName(ServiceAbility.class.getName()) 
  13.               .build(); 
  14.       serviceintent.setOperation(serviceOperation); 
  15.  
  16.       if(!PlaceName.equals("null")){ 
  17.           if(Loc.equals("")){ 
  18.               text.setText("没有设定范围"); 
  19.               stopAbility(serviceintent); 
  20.           }else { 
  21.               if(PlaceName.contains(Loc)){ 
  22.                   text.setText("孩子没有超出设定范围"); 
  23.                   stopAbility(serviceintent); 
  24.               }else { 
  25.                   text.setText("孩子已超出设定范围"); 
  26.                   startAbility(serviceintent); 
  27.               } 
  28.           } 
  29.       }else { 
  30.           text.setText("无法获取孩子的位置"); 
  31.           stopAbility(serviceintent); 
  32.       } 
  33.   } 

二、实现场景二

1. 复制上一个项目代码

创建一个名为ChildrenLocation3的Empty Java Phone应用。

儿童手表定位,活动范围监控——修改版-鸿蒙HarmonyOS技术社区

儿童手表定位,活动范围监控完善ChildrenLocation2。

2. 修改家长端设备界面布局

在ability_phone.xml中编写以下代码。

删除三个文本输入框,并修改标识符id。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:alignment="top|center" 
  7.     ohos:orientation="vertical"
  8.  
  9.     <Text 
  10.         ohos:id="$+id:Longitude" 
  11.         ohos:height="match_content" 
  12.         ohos:width="match_parent" 
  13.         ohos:margin="10vp" 
  14.         ohos:padding="10vp" 
  15.         ohos:text="---" 
  16.         ohos:text_size="28fp" 
  17.         ohos:text_color="#000000" 
  18.         ohos:text_alignment="left|vertical_center" 
  19.         ohos:background_element="#78C6C5"/> 
  20.  
  21.     <Text 
  22.         ohos:id="$+id:Latitude" 
  23.         ohos:height="match_content" 
  24.         ohos:width="match_parent" 
  25.         ohos:margin="10vp" 
  26.         ohos:padding="10vp" 
  27.         ohos:text="---" 
  28.         ohos:text_size="28fp" 
  29.         ohos:text_color="#000000" 
  30.         ohos:text_alignment="left|vertical_center" 
  31.         ohos:background_element="#78C6C5"/> 
  32.  
  33.     <Text 
  34.         ohos:id="$+id:CountryName" 
  35.         ohos:height="match_content" 
  36.         ohos:width="match_parent" 
  37.         ohos:margin="10vp" 
  38.         ohos:padding="10vp" 
  39.         ohos:text="---" 
  40.         ohos:text_size="28fp" 
  41.         ohos:text_color="#000000" 
  42.         ohos:text_alignment="left|vertical_center" 
  43.         ohos:background_element="#78C6C5"/> 
  44.  
  45.     <Text 
  46.         ohos:id="$+id:PlaceName" 
  47.         ohos:height="match_content" 
  48.         ohos:width="match_parent" 
  49.         ohos:margin="10vp" 
  50.         ohos:padding="10vp" 
  51.         ohos:text="---" 
  52.         ohos:text_size="28fp" 
  53.         ohos:text_color="#000000" 
  54.         ohos:text_alignment="left|vertical_center" 
  55.         ohos:background_element="#78C6C5" 
  56.         ohos:multiple_lines="true"/> 
  57.  
  58.     <TextField 
  59.         ohos:id="$+id:tf_Distance" 
  60.         ohos:height="match_content" 
  61.         ohos:width="match_parent" 
  62.         ohos:margin="10vp" 
  63.         ohos:padding="10vp" 
  64.         ohos:hint="请输入距离......" 
  65.         ohos:text_size="28fp" 
  66.         ohos:text_color="#000000" 
  67.         ohos:text_alignment="left|vertical_center" 
  68.         ohos:background_element="#BEBEBE"/> 
  69.  
  70.     <Text 
  71.         ohos:id="$+id:IDIDID" 
  72.         ohos:height="match_content" 
  73.         ohos:width="match_parent" 
  74.         ohos:margin="10vp" 
  75.         ohos:padding="10vp" 
  76.         ohos:text="---" 
  77.         ohos:text_size="28fp" 
  78.         ohos:text_color="#000000" 
  79.         ohos:text_alignment="left|vertical_center" 
  80.         ohos:background_element="#78C6C5" 
  81.         ohos:multiple_lines="true"/> 
  82.  
  83. </DirectionalLayout> 

3. 重写getRange()函数。

在MainAbilitySlice.java中编写以下代码。

添加两个变量Longitude_phone和Latitude_phone,并初始化为“null”,用于记录家长端设备的经纬度信息。通过location.getLongitude()方法获取经度信息,通过location.getLatitude()方法获取纬度信息。

通过getText()方法获取设置的活动距离,通过两个位置的经纬度计算其距离,并与设置的活动距离进行比较大小。根据大小关系通过startAbility方法播放音频,通过stopAbility方法停止播放音频。

  1. private static String Longitude_phone = "null"
  2.    private static String Latitude_phone = "null"
  3.  
  4.    private void getLocation(){ 
  5.        writeData("Longitude", Longitude); 
  6.        writeData("Latitude", Latitude); 
  7.        writeData("PlaceName", PlaceName); 
  8.        writeData("CountryName", CountryName); 
  9.  
  10.        timer_phone = new Timer(); 
  11.        timer_phone.schedule(new TimerTask() { 
  12.            @Override 
  13.            public void run() { 
  14.                getUITaskDispatcher().asyncDispatch(new Runnable() { 
  15.                    @Override 
  16.                    public void run() { 
  17.                        getRange(); 
  18.                        getSingleLocation(); 
  19.                    } 
  20.                }); 
  21.            } 
  22.        },0,5000); 
  23.    } 
  24.  
  25.    private void getRange(){ 
  26.        TextField tf_Distance = (TextField) findComponentById(ResourceTable.Id_tf_Distance); 
  27.        Text text = (Text) findComponentById(ResourceTable.Id_IDIDID); 
  28.        double Dis; 
  29.  
  30.        if(location == null){ 
  31.            Longitude_phone = "null"
  32.            Latitude_phone = "null"
  33.            return
  34.        } 
  35.        Longitude_phone = Double.toString(location.getLongitude()); 
  36.        Latitude_phone = Double.toString(location.getLatitude()); 
  37.  
  38.        if(tf_Distance.getText().equals("")){ 
  39.            Dis = -1; 
  40.        }else { 
  41.            Dis = Double.parseDouble(tf_Distance.getText()); 
  42.        } 
  43.  
  44.        Intent serviceintent = new Intent(); 
  45.        Operation serviceOperation = new Intent.OperationBuilder() 
  46.                .withDeviceId(""
  47.                .withBundleName(getBundleName()) 
  48.                .withAbilityName(ServiceAbility.class.getName()) 
  49.                .build(); 
  50.        serviceintent.setOperation(serviceOperation); 
  51.  
  52.        if(!(Longitude.equals("null") && Latitude.equals("null") && Longitude_phone.equals("null") && Latitude_phone.equals("null"))){ 
  53.            double Lon = getRadian(Double.parseDouble(Longitude)); 
  54.            double Lat = getRadian(Double.parseDouble(Latitude)); 
  55.            double Lon_phone = getRadian(Double.parseDouble(Longitude_phone)); 
  56.            double Lat_phone = getRadian(Double.parseDouble(Latitude_phone)); 
  57.  
  58.            double distance = 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))); 
  59.            distance = Double.parseDouble(Double.toString(distance).substring(0,7)); 
  60.            if(Dis != -1){ 
  61.                if(distance <= Dis){ 
  62.                    text.setText("你距离你的孩子" + distance + "米,没有超出距离"); 
  63.                    stopAbility(serviceintent); 
  64.                }else { 
  65.                    text.setText("你距离你的孩子" + distance + "米,已经超出距离"); 
  66.                    startAbility(serviceintent); 
  67.                } 
  68.            }else { 
  69.                text.setText("你距离你的孩子" + distance + "米,你没有设定距离"); 
  70.                stopAbility(serviceintent); 
  71.            } 
  72.        }else { 
  73.            text.setText("没有获取位置"); 
  74.            stopAbility(serviceintent); 
  75.        } 
  76.    } 
  77.  
  78.    private double getRadian(double degree){//转化为弧度制 
  79.        return degree / 180 * Math.PI; 
  80.    } 

文章相关附件可以点击下面的原文链接前往下载

ChildrenLocation2.zip

ChildrenLocation3.zip

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

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

https://harmonyos.51cto.com

 

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

2010-05-11 10:15:19

Unix awk

2012-09-26 09:19:26

OfficeOutlook 201

2010-01-07 09:10:06

2014-11-05 16:37:54

2022-04-14 11:35:01

HarmonyOS手表Demo操作系统

2015-08-19 16:08:39

搜狗糖猫儿童手表

2022-04-13 11:49:37

HarmonyOS操作系统鸿蒙

2013-11-05 10:40:06

浏览器Pale MoonFirefox

2015-06-16 12:06:01

校园做公益

2015-04-13 10:13:29

2014-12-04 09:47:59

2017-08-28 15:39:53

物联网

2017-08-29 12:07:25

互联网

2011-12-07 21:10:37

iOS

2021-01-31 09:52:49

SSH监控网络攻击

2014-09-29 16:39:43

儿童智能手环智能设备安全

2023-10-19 11:27:22

Linux记账工具
点赞
收藏

51CTO技术栈公众号