HarmonyOS 鸿蒙Next 获取地理位置信息后,怎么把经纬度提取出来?
HarmonyOS 鸿蒙Next 获取地理位置信息后,怎么把经纬度提取出来?
以上代码,点击按钮之后运行之后会打印出:com.waylau.hmos.locator I 00001/MainAbilitySlice: onLocationReport, location: {“accuracy”:10,“altitude”:51,“direction”:0,“latitude”:30.495864,“longitude”:114.535703,“speed”:0,“timeSinceBoot”:3524495898440,“timeStamp”:1489717727000}
我要怎么样才能把里面的latitude":30.495864,“longitude”:114.535703,"提取出来,在Text里显示或者,赋值给String latitude,和String longitude?
更多关于HarmonyOS 鸿蒙Next 获取地理位置信息后,怎么把经纬度提取出来?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
package com.example.locationtest.slice;
import com.example.locationtest.GPSEventHandler;
import com.example.locationtest.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.location.Location;
import ohos.location.Locator;
import ohos.location.LocatorCallback;
import ohos.location.RequestParam;
public class MainAbilitySlice extends AbilitySlice {
//用来显示纬度
private Text txtX;
//用来显示经度
private Text txtY;
//用来显示当前位置
private Text txtAddress;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//初始化纬度文本控件
txtX = (Text) findComponentById(ResourceTable.Id_text_x);
//初始化经度文本控件
txtY = (Text) findComponentById(ResourceTable.Id_text_y);
//初始化位置文本控件
txtAddress = (Text) findComponentById(ResourceTable.Id_text_address);
//实例化locator对象
Locator locator = new Locator(getContext());
if (locator.isLocationSwitchOn()) {
//实例化定位服务场景
RequestParam requestParam = new RequestParam(
RequestParam.SCENE_NAVIGATION);
//实例化定位服务的回调对象
CurrentLocatorCallback locatorCallback = new CurrentLocatorCallback();
//启动一次定位请求
locator.requestOnce(requestParam, locatorCallback);
} else {
//txtX.setText("0");
//txtY.setText("0");
txtAddress.setText("请打开位置信息开关!");
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
/**
* 定位回调类,实现LocatorCallback接口
*/
class CurrentLocatorCallback implements LocatorCallback {
@Override
public void onLocationReport(Location location) {
int eventId = 0;
long param = 0;
//定义事件对象
InnerEvent event = InnerEvent.get(eventId, param, location);
//获取当前应用的主线程
EventRunner eventRunner = EventRunner.getMainEventRunner();
//定义事件处理对象,并绑定到主线程中
EventHandler eventHandler = new GPSEventHandler
(eventRunner, txtX, txtY, txtAddress);
//把事件发送到事件处理对象中
eventHandler.sendEvent(event);
}
@Override
public void onStatusChanged(int type) {
switch (type){
case Locator.SESSION_START:
//会话开始
break;
case Locator.SESSION_STOP:
//会话停止
break;
}
}
@Override
public void onErrorReport(int type) {
switch (type){
case Locator.ERROR_PERMISSION_NOT_GRANTED:
//没权限
break;
case Locator.ERROR_SWITCH_UNOPEN:
//位置开关没开
break;
}
}
}
更多关于HarmonyOS 鸿蒙Next 获取地理位置信息后,怎么把经纬度提取出来?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
package com.example.locationtest;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.location.GeoAddress;
import ohos.location.GeoConvert;
import ohos.location.Location;
import java.io.IOException;
public class GPSEventHandler extends EventHandler {
private Text txtX;
private Text txtY;
private Text txtAddress;
public GPSEventHandler(EventRunner eventRunner,
Text txtX, Text txtY,
Text txtAddress) {
super(eventRunner);
this.txtX = txtX;
this.txtY = txtY;
this.txtAddress = txtAddress;
}
@Override
protected void processEvent(InnerEvent event) {
super.processEvent(event);
//获取事件传递过来的位置信息
Location location = (Location) event.object;
//获取位置的纬度
double x = location.getLatitude();
//获取位置的经度
double y = location.getLongitude();
String addr = "";
//定义地理解码对象
GeoConvert geoConvert = new GeoConvert();
try {
//通过坐标位置获取地理位置
GeoAddress ga = geoConvert.getAddressFromLocation
(x, y, 1).get(0);
//获取地理位置的描述
addr = ga.getDescriptions(0);
} catch (IOException e) {
e.printStackTrace();
}
//设置文本显示内容
txtX.setText("纬度为:" + x);
txtY.setText("经度为:" + y);
txtAddress.setText("位置为:" + addr);
}
}
纬度为:x
。这里为什么会崩溃?app闪退,
闪退一般都是API版本问题,我还像是6吧,
希望HarmonyOS能继续优化系统稳定性,减少崩溃和重启的情况。
使用参数location的方法:
- location.getLatitude()获取维度
- location.getLongitude()获取经度
另:如果要更新到UI的话需要使用UI线程。
这个UI线程,到底加在哪里?callback内吗?怎么搞都是闪退,
Location-Class-ohos.location-Java API参考-HarmonyOS应用开发
Location的实例,
double类型的函数getLatitude()获得纬度,getLongitude()获得经度。
欢迎开发小伙伴们进来帮帮楼主
你打印的地方 location.getLatitude
location.getLongitude
有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html
学习
在HarmonyOS鸿蒙系统中,获取地理位置信息后提取经纬度通常涉及对返回地理位置数据结构的解析。假设你已经通过系统的API成功获取了地理位置信息,以下是如何提取经纬度的步骤:
-
确认数据结构:首先,确保你了解返回地理位置信息的具体数据结构。鸿蒙系统一般会返回一个包含经纬度等信息的对象。
-
访问经纬度字段:在获取到的地理位置信息对象中,直接访问表示经度和纬度的字段。通常,这些字段会被命名为
latitude
(纬度)和longitude
(经度)。 -
提取并存储:将访问到的经度和纬度值提取出来,并存储到你需要的变量或数据结构中。
示例代码(伪代码形式,具体实现需根据实际API返回的数据结构调整):
// 假设getLocationInfo()函数返回了地理位置信息对象
LocationInfo location = getLocationInfo();
// 提取经纬度
double latitude = location.latitude;
double longitude = location.longitude;
// 现在latitude和longitude变量中存储了经纬度信息
请注意,上述代码仅为示例,实际开发中需根据鸿蒙系统提供的API文档和返回的数据结构进行调整。
如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html,