兒童手表定位,活動范圍監控—修改版
前言
為什么還會有修改版的兒童手表定位,活動范圍監控呢?因為上一個項目雖然已經實現了,但是不實用。原因就是如果不是特地去查詢該地方的經緯度,沒有誰會準確知道這個地方的經緯度,那么就不能設定活動范圍了。再者,上一個項目的活動范圍設置只能是矩形范圍,但是只有極少數的地方是規則的矩形范圍,所以并不實用。所以針對此原因,并且對場景進行細分,有了如下兩個修改版:
場景一:小孩在小區玩,家長設定活動范圍即為該小區,孩子離開小區時提醒家長。
場景二:孩子和家長一起外出,家長設定孩子離自己的距離,超過該設定距離時提醒家長。
概述
場景一效果圖如下:
場景二效果圖如下:
正文
一、實現場景一
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;
- }
文章相關附件可以點擊下面的原文鏈接前往下載