自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

兒童手表定位,活動范圍監(jiān)控—修改版

系統(tǒng)
孩子戴著手表去上補習(xí)班、在小區(qū)內(nèi)玩耍等等,手表可以將實時位置發(fā)送給家長,超出設(shè)置的活動范圍內(nèi)時提醒家長,實現(xiàn)孩子安全防患于未然。

[[420005]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

前言

為什么還會有修改版的兒童手表定位,活動范圍監(jiān)控呢?因為上一個項目雖然已經(jīng)實現(xiàn)了,但是不實用。原因就是如果不是特地去查詢該地方的經(jīng)緯度,沒有誰會準(zhǔn)確知道這個地方的經(jīng)緯度,那么就不能設(shè)定活動范圍了。再者,上一個項目的活動范圍設(shè)置只能是矩形范圍,但是只有極少數(shù)的地方是規(guī)則的矩形范圍,所以并不實用。所以針對此原因,并且對場景進行細(xì)分,有了如下兩個修改版:

場景一:小孩在小區(qū)玩,家長設(shè)定活動范圍即為該小區(qū),孩子離開小區(qū)時提醒家長。

場景二:孩子和家長一起外出,家長設(shè)定孩子離自己的距離,超過該設(shè)定距離時提醒家長。

概述

場景一效果圖如下:

場景二效果圖如下:

正文

一、實現(xiàn)場景一

1. 復(fù)制上一個項目代碼

創(chuàng)建一個名為ChildrenLocation2的Empty Java Phone應(yīng)用。

兒童手表定位,活動范圍監(jiān)控——修改版-鴻蒙HarmonyOS技術(shù)社區(qū)

兒童手表定位,活動范圍監(jiān)控完善ChildrenLocation2。

2. 修改家長端設(shè)備界面布局

在ability_phone.xml中編寫以下代碼。

刪除三個文本輸入框,并修改標(biāo)識符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()函數(shù)。

在MainAbilitySlice.java中編寫以下代碼。

通過getText()方法獲取設(shè)置的活動范圍,通過contains()方法判斷PlaceName與設(shè)置的活動范圍的關(guān)系,如果PlaceName不在設(shè)置的活動范圍內(nèi)則通過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("沒有設(shè)定范圍"); 
  19.               stopAbility(serviceintent); 
  20.           }else { 
  21.               if(PlaceName.contains(Loc)){ 
  22.                   text.setText("孩子沒有超出設(shè)定范圍"); 
  23.                   stopAbility(serviceintent); 
  24.               }else { 
  25.                   text.setText("孩子已超出設(shè)定范圍"); 
  26.                   startAbility(serviceintent); 
  27.               } 
  28.           } 
  29.       }else { 
  30.           text.setText("無法獲取孩子的位置"); 
  31.           stopAbility(serviceintent); 
  32.       } 
  33.   } 

二、實現(xiàn)場景二

1. 復(fù)制上一個項目代碼

創(chuàng)建一個名為ChildrenLocation3的Empty Java Phone應(yīng)用。

兒童手表定位,活動范圍監(jiān)控——修改版-鴻蒙HarmonyOS技術(shù)社區(qū)

兒童手表定位,活動范圍監(jiān)控完善ChildrenLocation2。

2. 修改家長端設(shè)備界面布局

在ability_phone.xml中編寫以下代碼。

刪除三個文本輸入框,并修改標(biāo)識符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()函數(shù)。

在MainAbilitySlice.java中編寫以下代碼。

添加兩個變量Longitude_phone和Latitude_phone,并初始化為“null”,用于記錄家長端設(shè)備的經(jīng)緯度信息。通過location.getLongitude()方法獲取經(jīng)度信息,通過location.getLatitude()方法獲取緯度信息。

通過getText()方法獲取設(shè)置的活動距離,通過兩個位置的經(jīng)緯度計算其距離,并與設(shè)置的活動距離進行比較大小。根據(jù)大小關(guān)系通過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 + "米,已經(jīng)超出距離"); 
  66.                    startAbility(serviceintent); 
  67.                } 
  68.            }else { 
  69.                text.setText("你距離你的孩子" + distance + "米,你沒有設(shè)定距離"); 
  70.                stopAbility(serviceintent); 
  71.            } 
  72.        }else { 
  73.            text.setText("沒有獲取位置"); 
  74.            stopAbility(serviceintent); 
  75.        } 
  76.    } 
  77.  
  78.    private double getRadian(double degree){//轉(zhuǎn)化為弧度制 
  79.        return degree / 180 * Math.PI; 
  80.    } 

文章相關(guān)附件可以點擊下面的原文鏈接前往下載

ChildrenLocation2.zip

ChildrenLocation3.zip

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2010-05-11 10:15:19

Unix awk

2012-09-26 09:19:26

OfficeOutlook 201

2014-11-05 16:37:54

2010-01-07 09:10:06

2022-04-14 11:35:01

HarmonyOS手表Demo操作系統(tǒng)

2015-08-19 16:08:39

搜狗糖貓兒童手表

2013-11-05 10:40:06

瀏覽器Pale MoonFirefox

2015-06-16 12:06:01

校園做公益

2022-04-13 11:49:37

HarmonyOS操作系統(tǒng)鴻蒙

2015-04-13 10:13:29

2014-12-04 09:47:59

2021-01-31 09:52:49

SSH監(jiān)控網(wǎng)絡(luò)攻擊

2017-08-28 15:39:53

物聯(lián)網(wǎng)

2017-08-29 12:07:25

互聯(lián)網(wǎng)

2011-12-07 21:10:37

iOS

2024-08-20 20:17:47

Mibro

2023-10-19 11:27:22

Linux記賬工具
點贊
收藏

51CTO技術(shù)棧公眾號