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

木棉花HOS
发布于 2021-8-23 12:52
浏览
5收藏

前言

为什么还会有修改版的儿童手表定位,活动范围监控呢?因为上一个项目虽然已经实现了,但是不实用。原因就是如果不是特地去查询该地方的经纬度,没有谁会准确知道这个地方的经纬度,那么就不能设定活动范围了。再者,上一个项目的活动范围设置只能是矩形范围,但是只有极少数的地方是规则的矩形范围,所以并不实用。所以针对此原因,并且对场景进行细分,有了如下两个修改版:
场景一:小孩在小区玩,家长设定活动范围即为该小区,孩子离开小区时提醒家长。
场景二:孩子和家长一起外出,家长设定孩子离自己的距离,超过该设定距离时提醒家长。

概述

场景一效果图如下:

儿童手表定位,活动范围监控——修改版-鸿蒙开发者社区 儿童手表定位,活动范围监控——修改版-鸿蒙开发者社区 儿童手表定位,活动范围监控——修改版-鸿蒙开发者社区 儿童手表定位,活动范围监控——修改版-鸿蒙开发者社区
场景二效果图如下:
- - - -
儿童手表定位,活动范围监控——修改版-鸿蒙开发者社区 儿童手表定位,活动范围监控——修改版-鸿蒙开发者社区 儿童手表定位,活动范围监控——修改版-鸿蒙开发者社区 儿童手表定位,活动范围监控——修改版-鸿蒙开发者社区

正文

一、实现场景一

1. 复制上一个项目代码

创建一个名为ChildrenLocation2的Empty Java Phone应用。
儿童手表定位,活动范围监控——修改版-鸿蒙开发者社区
儿童手表定位,活动范围监控完善ChildrenLocation2。

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

ability_phone.xml中编写以下代码。
删除三个文本输入框,并修改标识符id。

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="top|center"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:Longitude"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:text="---"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#78C6C5"/>

    <Text
        ohos:id="$+id:Latitude"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:text="---"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#78C6C5"/>

    <Text
        ohos:id="$+id:CountryName"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:text="---"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#78C6C5"/>

    <Text
        ohos:id="$+id:PlaceName"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:text="---"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#78C6C5"
        ohos:multiple_lines="true"/>

    <TextField
        ohos:id="$+id:tf_Location"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:hint="请输入活动位置......"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#BEBEBE"
        ohos:multiple_lines="true"/>

    <Text
        ohos:id="$+id:IDIDID"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:text="---"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#78C6C5"
        ohos:multiple_lines="true"/>

</DirectionalLayout>

3. 重写getRange()函数。

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

    private void getRange(){
        TextField tf_Location = (TextField) findComponentById(ResourceTable.Id_tf_Location);

        Text text = (Text) findComponentById(ResourceTable.Id_IDIDID);

        String Loc = tf_Location.getText();

        Intent serviceintent = new Intent();
        Operation serviceOperation = new Intent.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("孩子没有超出设定范围");
                    stopAbility(serviceintent);
                }else {
                    text.setText("孩子已超出设定范围");
                    startAbility(serviceintent);
                }
            }
        }else {
            text.setText("无法获取孩子的位置");
            stopAbility(serviceintent);
        }
    }

二、实现场景二

1. 复制上一个项目代码

创建一个名为ChildrenLocation3的Empty Java Phone应用。
儿童手表定位,活动范围监控——修改版-鸿蒙开发者社区
儿童手表定位,活动范围监控完善ChildrenLocation2。

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

ability_phone.xml中编写以下代码。
删除三个文本输入框,并修改标识符id。

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="top|center"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:Longitude"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:text="---"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#78C6C5"/>

    <Text
        ohos:id="$+id:Latitude"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:text="---"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#78C6C5"/>

    <Text
        ohos:id="$+id:CountryName"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:text="---"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#78C6C5"/>

    <Text
        ohos:id="$+id:PlaceName"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:text="---"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#78C6C5"
        ohos:multiple_lines="true"/>

    <TextField
        ohos:id="$+id:tf_Distance"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:hint="请输入距离......"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#BEBEBE"/>

    <Text
        ohos:id="$+id:IDIDID"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:margin="10vp"
        ohos:padding="10vp"
        ohos:text="---"
        ohos:text_size="28fp"
        ohos:text_color="#000000"
        ohos:text_alignment="left|vertical_center"
        ohos:background_element="#78C6C5"
        ohos:multiple_lines="true"/>

</DirectionalLayout>

3. 重写getRange()函数。

MainAbilitySlice.java中编写以下代码。
添加两个变量Longitude_phone和Latitude_phone,并初始化为“null”,用于记录家长端设备的经纬度信息。通过location.getLongitude()方法获取经度信息,通过location.getLatitude()方法获取纬度信息。
通过getText()方法获取设置的活动距离,通过两个位置的经纬度计算其距离,并与设置的活动距离进行比较大小。根据大小关系通过startAbility方法播放音频,通过stopAbility方法停止播放音频。

    private static String Longitude_phone = "null";
    private static String Latitude_phone = "null";

    private void getLocation(){
        writeData("Longitude", Longitude);
        writeData("Latitude", Latitude);
        writeData("PlaceName", PlaceName);
        writeData("CountryName", CountryName);

        timer_phone = new Timer();
        timer_phone.schedule(new TimerTask() {
            @Override
            public void run() {
                getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        getRange();
                        getSingleLocation();
                    }
                });
            }
        },0,5000);
    }

    private void getRange(){
        TextField tf_Distance = (TextField) findComponentById(ResourceTable.Id_tf_Distance);
        Text text = (Text) findComponentById(ResourceTable.Id_IDIDID);
        double Dis;

        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());
        }

        Intent serviceintent = new Intent();
        Operation serviceOperation = new Intent.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"))){
            double Lon = getRadian(Double.parseDouble(Longitude));
            double Lat = getRadian(Double.parseDouble(Latitude));
            double Lon_phone = getRadian(Double.parseDouble(Longitude_phone));
            double Lat_phone = getRadian(Double.parseDouble(Latitude_phone));

            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)));
            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 + "米,你没有设定距离");
                stopAbility(serviceintent);
            }
        }else {
            text.setText("没有获取位置");
            stopAbility(serviceintent);
        }
    }

    private double getRadian(double degree){//转化为弧度制
        return degree / 180 * Math.PI;
    }

写在最后

更多资料请关注我们的项目 : Awesome-Harmony_木棉花
本项目会长期更新 ,希望随着鸿蒙一同成长变强的既有我们,也有正在看着这个项目的你。明年3月,深大校园内的木棉花会盛开,那时,鸿蒙也会变的更好,愿这花开,有你我的一份。

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
ChildrenLocation2.zip 3.19M 11次下载
ChildrenLocation3.zip 3.19M 18次下载
已于2021-8-25 09:50:31修改
6
收藏 5
回复
举报
6条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

很实用的设计,修改的地方也确实是为家长考虑。

回复
2021-8-23 13:42:15
木棉花HOS
木棉花HOS 回复了 红叶亦知秋
很实用的设计,修改的地方也确实是为家长考虑。

谢谢(#^.^#)

回复
2021-8-23 14:34:27
鸿联
鸿联

这个电子围栏功能可以扩展到很多场景

回复
2021-8-23 15:02:45
ltkk12334
ltkk12334

谢谢侬

回复
2021-8-23 20:36:25
丨张明亮丨
丨张明亮丨

好实用的场景

回复
2021-8-24 08:47:03
爱吃土豆丝的打工人
爱吃土豆丝的打工人

支持支持、、、

回复
2021-8-24 08:49:14
回复
    相关推荐