参考资料

​位置开发概述​

​基于HarmonyOS获取设备位置(Java)​

一、api讲解

1、准备工作

权限集成,需要在config.json文件中添加定位权限,代码如下所示

"reqPermissions": [
{"name": "ohos.permission.LOCATION"}
],

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI

在MainAbility界面进行动态申请定位权限,代码如下

String[] permissions = {
SystemPermission.LOCATION
};
requestPermissionsFromUser(permissions, 0);

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI_02

2、实例化Locator对象

实例化Locator对象,代码如下

Locator locator = new

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI_03

3、实例化LocatorCallback对象

实例化LocatorCallback对象,用于向系统提供位置上报的途径,代码如下

/**
* 定位回调
*/
public class MyLocatorCallback implements LocatorCallback
/**
* 定位成功的回调,用于开发者获取具体地理的经纬度详细信息
* @param
@Override
public void onLocationReport(Location location) {

}

/**
*定位的时候,定位服务状态发生改变的时候,触发这个函数
* @param
@Override
public void onStatusChanged(int type) {
HiLog.error(LABEL,"状态改变");
}

/**
* 定位失败触发此函数
* @param
@Override
public void onErrorReport(int type) {
HiLog.error(LABEL,"定位失败");
}
}

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI_04

4、实例化RequestParam对象,

实例化RequestParam对象,用于告知系统该向应用提供何种类型的位置服务,以及位置结果上报的频率,(具体参数参考​​位置开发概述​​)代码如下

RequestParam requestParam = new RequestParam(RequestParam.PRIORITY_ACCURACY, 0, 0);

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI_05

5、开始定位

locator.startLocating(requestParam, locatorCallback);

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI_06

6、停止定位

locator.stopLocating(locatorCallback);

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI_07

7、(逆)地理编码转化

实例化GeoConvert对象,代码如下

//todo 实例化GeoConvert对象
GeoConvert geoConvert = new

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI_08

8、获取转化结果

调用getAddressFromLocation(double latitude, double longitude, int maxItems),坐标转化地理位置信息,代码如下

/**
* 获取地理位置
* latitude:当前的经度
*longitude:当前的维度
* maxItems:指定返回的地址列表的最大长度。建议取值范围为1-5。如果指定的值小于1,则使用默认值1。
*/
geoConvert.getAddressFromLocation(40.0, 116.0, 1);

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI_09

二、代码实现

绘制xml界面

绘制xml界面,一个text组件用于基本的定位和并获取经纬度然后(逆)地理编码转化获取具体地理位置,一个text组件用于显示结果,代码如下

<?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"
ohos:orientation="vertical">
<!-- 定位按钮-->
<Text
ohos:text_alignment="center"
ohos:id="$+id:text_start_Location"
ohos:height="80vp"
ohos:width="match_parent"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="开始定位"
ohos:text_size="20vp"
<!--用于显示获取地理位置的详细信息结果-->
<Text
ohos:text_alignment="horizontal_center"
ohos:id="$+id:text_result"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#ed6262"
ohos:layout_alignment="horizontal_center"
ohos:multiple_lines="true"
ohos:text="显示结果"
ohos:text_size="20vp"

</DirectionalLayout>

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI_10

java代码实现

package com.newdemo.myapplication.slice;

import com.newdemo.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.location.*;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice
private Locator locator;
private GeoConvert geoConvert;
private Text Text_result;//todo 结果按钮
static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//todo 查找显示结果按钮
Text_result= (Text) findComponentById(ResourceTable.Id_text_result);
//todo 实例化locator对象
locator= new Locator(MainAbilitySlice.this);
//todo 构造requestParam参数
RequestParam requestParam = new RequestParam(RequestParam.PRIORITY_ACCURACY, 0, 0);

MyLocatorCallback locatorCallback = new MyLocatorCallback();
//todo 实例化geoConvert
geoConvert = new GeoConvert();
/**
* 实现按钮事件 开始定位
*/
findComponentById(ResourceTable.Id_text_start_Location).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//为开启定位服务方法
locator.requestOnce(requestParam, locatorCallback);
}
});
}


/**
* 定位回调
*/
public class MyLocatorCallback implements LocatorCallback
/**
* 定位成功的回调,用于开发者获取具体地理的经纬度详细信息
* @param
@Override
public void onLocationReport(Location location) {



try {
StringBuilder stringBuilder=new StringBuilder();
HiLog.error(LABEL, "定位成功" + "Latitude====>" + location.getLatitude() + "===Longitude====>" + location.getLongitude());
stringBuilder.append("定位成功").append("\n");
//todo 根据经纬度获取详细信息
List<GeoAddress> list = geoConvert.getAddressFromLocation(location.getLatitude(), location.getLongitude(), 1);
if(list.size()>0){
for (int i=0;i<list.size();i++){
stringBuilder.append( "Latitude====>" + list.get(i).getLatitude() ).append("\n");
stringBuilder.append("Longitude====>" + list.get(i).getLongitude()).append("\n");
stringBuilder.append("地理位置"+list.get(0).getDescriptions(0));
HiLog.error(LABEL, "地理位置" + list.get(0).toString());
//todo 显示结果
getUITaskDispatcher().syncDispatch(() -> {
Text_result.setText(stringBuilder.toString());
});
}
}
HiLog.error(LABEL, "geoConvert is over" );
}catch (Exception e){
HiLog.error(LABEL, "Exception" + e.toString());
}
}

/**
*定位的时候,定位服务状态发生改变的时候,触发这个函数
* @param
@Override
public void onStatusChanged(int type) {
HiLog.error(LABEL,"状态改变");
}

/**
* 定位失败触发此函数
* @param
@Override
public void onErrorReport(int type) {
HiLog.error(LABEL,"定位失败");
}
}

}

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI_11

三、运行效果

【JAVA UI】HarmonyOS 定位的功能基本实现_JAVA UI_12

欲了解更多更全技术文章,欢迎访问​​https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh​