HarmonyOS H5中触发原生的调用定位相关的示例实现

HarmonyOS  H5中触发原生的调用定位相关的示例实现。


HarmonyOS
2024-09-04 11:19:46
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

以下是H5中触发调用ArkTS实现定位相关的demo

获取位置信息需先配置三个配置权限:

ohos.permission.LOCATION

ohos.permission.APPROXIMATELY_LOCATION

ohos.permission.LOCATION_IN_BACKGROUND

以下为 index.ets 代码

webController: webview.WebviewController = new webview.WebviewController(); 
@State latitude: number = 0; 
@State longitude: number = 0; 
@State testObj: testClass = new testClass(); 
 
build() { 
  Row() { 
    Column() { 
      Text("以下为ArkTS组件") 
        .fontSize(25) 
        .fontWeight(FontWeight.Bold) 
      Text("latitude" + this.latitude ) 
        .fontSize(16) 
        .fontWeight(FontWeight.Bold) 
      Text("longitude" + this.longitude ) 
        .fontSize(16) 
        .fontWeight(FontWeight.Bold) 
      Row(){ 
        LocationButton({icon:LocationIconStyle.LINES}) 
          .iconColor(Color.Red) 
          .onClick(async ()=>{ 
            //获取当前位置坐标 
            this.getLocation() 
          }) 
        Button("获取位置") 
          .onClick(()=>{ 
            this.getLocation() 
          }) 
        Button("重置位置") 
          .onClick(()=>{ 
            this.latitude=0 
            this.longitude=0 
          }) 
      } 
 
      Text("以下为H5页面") 
        .fontSize(25) 
        .fontWeight(FontWeight.Bold) 
      Web({ src: $rawfile("local/htmlTwo.html"), controller: this.webController }) 
        .height(500) 
        .padding(2) 
        .margin({ 
          left:20, 
          bottom:2 
        }) 
        .backgroundColor('#00000000') 
          //前端调用应用侧函数 
        .javaScriptProxy({ 
          object: this.testObj, 
          name: "testObjName", 
          methodList: ["getLocation"], 
          controller: this.webController 
        }) 
 
    } 
    .width('100%') 
  } 
  .height('100%') 
} 
 
async getLocation(): Promise<void> { 
  //获取当前位置坐标 
  await geoLocationManager.getCurrentLocation() 
  .then(async (result) => { 
  console.log('current location: ' + JSON.stringify(result)); 
  this.latitude = result.latitude; 
  this.longitude = result.longitude; 
}) 
.catch((error:number) => { 
  console.error('promise, getCurrentLocation: error=' + JSON.stringify(error)); 
}); 
}

index.ets页面导包信息。

import webview from '@ohos.web.webview'; 
import geoLocationManager from '@ohos.geoLocationManager'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
------------------------------------------------------------------------- 
 
//H5页面调用获取地理位置类和方法,可获取当前地理位置的 latitude 和 longitude 
class testClass { 
  constructor() { 
  } 
  async getLocation(): Promise<xy> { 
    return await geoLocationManager.getCurrentLocation() 
      .then(async (result) => { 
        console.log('current location: ' + JSON.stringify(result)); 
        let temp = new xy(); 
        temp.latitude = result.latitude; 
        temp.longitude = result.longitude; 
        //逆地理编码--暂时测试无法正常转回文字描述地理位置 
        //start 
        let reverseGeocodeRequest:geoLocationManager.ReverseGeoCodeRequest = {"latitude": 39.9, "longitude": 116.4, "maxItems": 1}; 
        let isAvailable = geoLocationManager.isGeocoderAvailable(); 
        let locationStr = ""; 
        if(isAvailable){ 
          try { 
            geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest, (err, data) => { 
              if (err) { 
                console.log('getAddressesFromLocation err: ' + JSON.stringify(err)); 
              } else { 
                console.log('getAddressesFromLocation data: ' + JSON.stringify(data)); 
              } 
            }); 
          } catch (err) { 
            console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 
          } 
        } 
        //end 
        locationStr = "latitude:"+result.latitude+"--locationStr:"+result.longitude; 
        return temp; 
      }) 
      .catch((error: number) => { 
        console.error('promise, getCurrentLocation: error=' + JSON.stringify(error)); 
        return new xy(); 
      }); 
 
  } 
} 
//操作对象实例类 
class xy{ 
  private latitude1: number = 0; 
 
  public set latitude(value: number) { 
    this.latitude1 = value; 
  } 
 
  public get latitude(): number { 
    return this.latitude1; 
  } 
 
  private longitude1: number = 0; 
 
  public set longitude(value: number) { 
    this.longitude1 = value; 
  } 
 
  public get longitude(): number { 
    return this.longitude1; 
  } 
  constructor() { 
  } 
 
}
<!-- H5页面代码 此部分代码路径在 rawfile/local/htmlTwo.html --> 
  <!DOCTYPE html> 
  <html> 
  <head> 
  <link rel="stylesheet" type="text/css"> 
  <meta charset="UTF-8"> 
  <title>获取定位信息</title> 
  <style> 
#prize { 
  border-radius: 16px 16px 16px 16px; 
  background-image: linear-gradient(180deg, #A2DAFF 0%, #EAF5FF 100%); 
  margin-left: 1.82%; 
  margin-top: 1.43%; 
  width: 96.5%; 
  height: 96.7%; 
} 
</style> 
  </head> 
  <body> 
  <div style="width:1000px;height:300px"> 
  <input style="width:1000px;height:100px;font-size:30px" id="inputText" /> 
  <p style="width:1000px;height:100px;font-size:30px" id="demo">p1</p> 
  <button style="width:1000px;height:100px" onclick="getLocation()"> 
  <span style="font-size:30px">获取定位</span> 
  </button> 
  </div> 
  <script> 
  function getLocation() { 
    <!-- ArtTS侧返回的如果是 promise对象,使用改方法处理 --> 
      <!-- 具体可参考前端页面调用应用侧函数API,包含简单类型和复杂类型--> 
      <!-- https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/web-in-page-app-function-invoking-0000001844832802--> 
    testObjName.getLocation() 
      .then((param)=>{ document.getElementById("demo").innerHTML = JSON.stringify(param) }) 
      .catch((param)=>{ document.getElementById("demo").innerHTML = JSON.stringify(param) }) 
  } 
  </script> 
  </body> 
  </html>

在测试时先操作ArkTS部分按钮,如果获取地理位置不成功:

1、请检查是否开启 真机/模拟器 定位服务;

2、点击《获取位置》左侧定位按钮,手动获取位置权限。获取到位置信息之后,位置编码会刷新;

之后操作H5页面获取定位操作,H5页面调用ArkTS侧方法获取位置信息,渲染到H5页面。

分享
微博
QQ
微信
回复
2024-09-04 16:13:58
相关问题
HarmonyOS web原生H5如何交互?
161浏览 • 1回复 待解决
HarmonyOS H5JS端调用应用端新问题
169浏览 • 0回复 待解决
HarmonyOS webview h5localstorage
117浏览 • 1回复 待解决
HarmonyOS web组件加载h5h5拉起摄像头
161浏览 • 1回复 待解决
Webwebview和H5交互
765浏览 • 1回复 待解决
HarmonyOS web与H5交互
210浏览 • 1回复 待解决
如何在webview中使用H5alert
835浏览 • 1回复 待解决
如何实现H5自定义事件
1964浏览 • 1回复 待解决
HarmonyOSH5如何跳转到应用市场
120浏览 • 1回复 待解决
原生调用htmljavascript实现
131浏览 • 1回复 待解决
如何在HarmonyOS调试h5页面
569浏览 • 1回复 待解决
HarmonyOS H5与应用侧数据交互Demo
152浏览 • 1回复 待解决