当前位置: 首页 > wzjs >正文

服装公司 网站怎么做千川广告投放平台

服装公司 网站怎么做,千川广告投放平台,如何编写一个软件,能挣钱的平台 正规的前言 在开发运动类应用时,集成地图功能以及实时记录运动轨迹和公里数是核心需求之一。本文将详细介绍如何在 HarmonyOS 应用中集成百度地图 SDK,实现运动跟随以及运动公里数的记录。 一、集成百度地图 SDK 1.引入依赖 首先,需要在项目的文…

前言

在开发运动类应用时,集成地图功能以及实时记录运动轨迹和公里数是核心需求之一。本文将详细介绍如何在 HarmonyOS 应用中集成百度地图 SDK,实现运动跟随以及运动公里数的记录。

一、集成百度地图 SDK

1.引入依赖

首先,需要在项目的文件中引入百度地图相关的依赖包:

"dependencies": {"@bdmap/base": "1.2.6","@bdmap/search": "1.2.6","@bdmap/map": "1.2.6","@bdmap/locsdk": "1.1.4"
}

2.初始化百度地图

为了使用百度地图的功能,我们需要进行初始化操作。这包括设置 API Key 和初始化定位客户端。

MapUtil 类


export class MapUtil{public static initialize(context:Context){Initializer.getInstance().initialize("你的key");// 设置是否同意隐私合规政策接口// true,表示同意隐私合规政策// false,表示不同意隐私合规政策LocationClient.checkAuthKey("你的key", (result: string) => {console.debug("result = " + result); // 可打印出是否鉴权成功的结果});LocationClient.setAgreePrivacy(true);LocManager.getInstance().init(context);}}

LocManager 类


export class LocManager {private client: LocationClient | null = null;private static instance: LocManager;public static getInstance(): LocManager {if (!LocManager.instance) {LocManager.instance = new LocManager();}return LocManager.instance;}constructor() {}init(context: Context) {if (this.client == null) {try {this.client = new LocationClient(context);} catch (error) {console.error("harmony_baidu_location error: " + error.message);}}if (this.client != null) {this.client.setLocOption(this.getDefaultLocationOption());}}start() {if (this.client != null) {this.client.start();}}stop() {if (this.client != null) {this.client.stop();}}requestSingleLocation() {if (this.client != null) {this.client.requestSingleLocation();}}registerListener(listener: BDLocationListener): boolean {let isSuccess: boolean = false;if (this.client != null && listener != null) {this.client.registerLocationListener(listener);isSuccess = true;}return isSuccess;}unRegisterListener(listener: BDLocationListener) {if (this.client != null && listener != null) {this.client.unRegisterLocationListener(listener);}}getSDKVersion(): string {let version: string = "";if (this.client != null) {version = this.client.getVersion();}return version;}enableLocInBackground(wantAgent: WantAgent) {if (this.client != null) {this.client.enableLocInBackground(wantAgent);}}disableLocInBackground() {if (this.client != null) {this.client.disableLocInBackground();}}getDefaultLocationOption() {let option = new LocationClientOption();option.setCoorType("bd09ll"); // 可选,默认为gcj02,设置返回的定位结果坐标系option.setTimeInterval(3); // 可选,默认1秒,设置连续定位请求的时间间隔option.setDistanceInterval(0); // 可选,默认0米,设置连续定位的距离间隔option.setIsNeedAddress(true); // 可选,设置是否需要地址信息,默认不需要option.setIsNeedLocationDescribe(true); // 可选,默认为false,设置是否需要地址描述option.setIsNeedLocationPoiList(true); // 可选,默认能为false,设置是否需要POI结果option.setLocationMode(LocationMode.High_Accuracy); // 可选,默认高精度,设置定位模式,高精度、低功耗、仅设备option.setSingleLocatingTimeout(3000); // 可选,仅针对单次定位生效,设置单次定位的超时时间return option;}}

3.定位监听器

为了处理定位数据,我们需要实现一个定位监听器:


export class MapLocationListener extends BDLocationListener {private callback: (location: BDLocation) => void;constructor(callback: (location: BDLocation) => void) {super();this.callback = callback;}onReceiveLocation(bdLocation: BDLocation): void {this.callback(bdLocation);}
}

二、页面使用

1.权限申请

在文件中声明所需的权限:

"requestPermissions": [{"name": "ohos.permission.LOCATION","reason": "$string:location_permission","usedScene": {"abilities": ["EntryAbility"],"when": "inuse"}},{"name": "ohos.permission.LOCATION_IN_BACKGROUND","reason": "$string:background_location_permission","usedScene": {"abilities": ["EntryAbility"],"when": "inuse"}},{"name": "ohos.permission.APPROXIMATELY_LOCATION","reason": "$string:fuzzy_location_permission","usedScene": {"abilities": ["EntryAbility"],"when": "inuse"}},{"name": "ohos.permission.APP_TRACKING_CONSENT","reason": "$string:get_oaid_permission","usedScene": {"abilities": ["EntryAbility"],"when": "inuse"}},{"name": "ohos.permission.KEEP_BACKGROUND_RUNNING","reason": "$string:keep_background_running_permission","usedScene": {"abilities": ["EntryAbility1"],"when": "inuse"}}]

2.请求权限

在页面中请求权限:

private async requestPermissions(): Promise<boolean> {const permissions : Permissions[]= ['ohos.permission.LOCATION','ohos.permission.APPROXIMATELY_LOCATION','ohos.permission.APP_TRACKING_CONSENT',]return LibPermission.requestPermissions(permissions)}

3.页面调用

方向感应

使用鸿蒙系统自带的方向传感器来获取设备的朝向角度:

// 初始化方向传感器sensor.on(sensor.SensorId.ORIENTATION, (data) => {// 获取设备朝向角度(绕Z轴旋转角度)this.currentRotation = data.alpha;if(this.loc){this.loc.location = new LatLng(this.currentLatitude, this.currentLongitude);this.loc.direction = this.currentRotation;this.loc.radius = 0;}});// 用完记得取消监听
sensor.off(sensor.SensorId.ORIENTATION);

编写定位监听器

private mListener: MapLocationListener = new MapLocationListener((bdLocation: BDLocation) => {this.currentLatitude = bdLocation.getLatitude();this.currentLongitude = bdLocation.getLongitude();this.currentRadius = bdLocation.getRadius();// 更新地图位置和位置标记if (this.mapController) {// 更新地图中心点this.mapController.setMapCenter({lat: this.currentLatitude,lng: this.currentLongitude},15);if(this.loc){// 设置定位图标位置、指向以及范围this.loc.location = new LatLng(this.currentLatitude, this.currentLongitude);this.loc.direction = this.currentRotation;// 单位米this.loc.radius = 0;}}});

启动和关闭定位

// 启动定位
LocManager.getInstance().registerListener(this.mListener);
LocManager.getInstance().start();// 关闭定位
LocManager.getInstance().unRegisterListener(this.mListener);
LocManager.getInstance().stop();

百度地图集成

在页面中集成百度地图:

MapComponent({ onReady: async (err, mapController:MapController) => {if (!err) {// 获取地图的控制器类,用来操作地图this.mapController= mapController;let result = this.mapController.getLayerByTag(SysEnum.LayerTag.LOCATION);if(result){this.loc = result as LocationLayer;}if(this.currentLatitude!=0&&this.currentLongitude!=0){if(this.loc){// 设置定位图标位置、指向以及范围this.loc.location = new LatLng(this.currentLatitude, this.currentLongitude);this.loc.direction = this.currentRotation;// 单位米this.loc.radius = 0;}this.mapController.setMapCenter({lat: this.currentLatitude,lng: this.currentLongitude},15);}}}, mapOptions: this.mapOpt }).width('100%').height('100%')

三、公里数计算

在运动应用中,记录用户的运动轨迹并计算运动的总距离是核心功能之一。为了实现这一功能,我们需要设计一个数据模型来记录运动轨迹点,并通过这些点计算总距离。

1.运动轨迹点模型

定义一个RunPoint类来表示运动轨迹中的一个点,包含纬度、经度和时间戳:

/*** 运动轨迹点数据模型*/
export class RunPoint {// 纬度latitude: number;// 经度longitude: number;// 时间戳timestamp: number;// 所属公里数分组(第几公里)kilometerGroup: number;constructor(latitude: number, longitude: number) {this.latitude = latitude;this.longitude = longitude;this.timestamp = Date.now();this.kilometerGroup = 0; // 默认分组为0}
}

2.运动轨迹管理类

创建一个RunTracker类来管理运动轨迹点,并计算总距离:


/*** 运动轨迹管理类*/
export class RunTracker {// 所有轨迹点private points: RunPoint[] = [];// 当前总距离(公里)private totalDistance: number = 0;// 当前公里数分组private currentKilometerGroup: number = 0;/*** 添加新的轨迹点* @param latitude 纬度* @param longitude 经度* @returns 当前总距离(公里)*/addPoint(latitude: number, longitude: number): number {const point = new RunPoint(latitude, longitude);if (this.points.length > 0) {// 计算与上一个点的距离const lastPoint = this.points[this.points.length - 1];const distance = this.calculateDistance(lastPoint, point);this.totalDistance += distance;// 更新公里数分组point.kilometerGroup = Math.floor(this.totalDistance);if (point.kilometerGroup > this.currentKilometerGroup) {this.currentKilometerGroup = point.kilometerGroup;}}this.points.push(point);return this.totalDistance;}/*** 计算两点之间的距离(公里)* 使用Haversine公式计算球面距离*/private calculateDistance(point1: RunPoint, point2: RunPoint): number {const R = 6371; // 地球半径(公里)const lat1 = this.toRadians(point1.latitude);const lat2 = this.toRadians(point2.latitude);const deltaLat = this.toRadians(point2.latitude - point1.latitude);const deltaLon = this.toRadians(point2.longitude - point1.longitude);const a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +Math.cos(lat1) * Math.cos(lat2) *Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));return R * c;}/*** 将角度转换为弧度*/private toRadians(degrees: number): number {return degrees * (Math.PI / 180);}/*** 获取当前总距离*/getTotalDistance(): number {return this.totalDistance;}/*** 获取指定公里数分组的轨迹点*/getPointsByKilometer(kilometer: number): RunPoint[] {return this.points.filter(point => point.kilometerGroup === kilometer);}/*** 清空轨迹数据*/clear(): void {this.points = [];this.totalDistance = 0;this.currentKilometerGroup = 0;}
}

3.页面的监听器里记录公里数

在页面中使用RunTracker类来记录运动轨迹点并计算总距离:

  private runTracker: RunTracker = new RunTracker();
监听器添加代码const distance = this.runTracker.addPoint(this.currentLatitude, this.currentLongitude);distance就是当前运动的公里数

四、总结

本文详细介绍了如何在 HarmonyOS 应用中集成百度地图 SDK,实现运动跟随以及运动公里数的记录。通过以下步骤,我们可以实现一个功能完整的运动应用:

• 集成百度地图 SDK:

• 引入必要的依赖包。

• 初始化百度地图并设置定位选项。

• 页面使用:

• 请求必要的权限。

• 启动和关闭定位。

• 实时更新地图位置和方向。

• 公里数计算:

• 定义运动轨迹点模型。

• 使用 Haversine 公式计算两点之间的距离。

• 记录运动轨迹点并实时更新总距离。

通过这些步骤,开发者可以轻松实现一个功能强大的运动应用,为用户提供实时的运动数据和地图跟随功能。希望本文的内容能够帮助你在 HarmonyOS 开发中取得更好的成果!


文章转载自:

http://3dREPzpL.kpwdt.cn
http://eKMCJw5D.kpwdt.cn
http://TXVabvUw.kpwdt.cn
http://pF2sG4HH.kpwdt.cn
http://pjsudbdt.kpwdt.cn
http://7qWypF1Y.kpwdt.cn
http://CBRuswuO.kpwdt.cn
http://etwPRcxj.kpwdt.cn
http://jYxQr9NL.kpwdt.cn
http://ZSsUxlsk.kpwdt.cn
http://enGHdi1f.kpwdt.cn
http://bfzRhTc7.kpwdt.cn
http://dzCiFuf0.kpwdt.cn
http://Ug6vqWzK.kpwdt.cn
http://r2qbOyar.kpwdt.cn
http://2GpZ2ItG.kpwdt.cn
http://oIeZ23sg.kpwdt.cn
http://eVWZMIyj.kpwdt.cn
http://nSpWwON9.kpwdt.cn
http://Mca635Wg.kpwdt.cn
http://0CFMzSHQ.kpwdt.cn
http://95K9Ec0r.kpwdt.cn
http://FsvT5Yq3.kpwdt.cn
http://rHTgKEgY.kpwdt.cn
http://z87Ga4Nk.kpwdt.cn
http://b5L1kB2A.kpwdt.cn
http://3kq5Wlrw.kpwdt.cn
http://lnOUEany.kpwdt.cn
http://nkAEn1Q0.kpwdt.cn
http://nP5fvTAd.kpwdt.cn
http://www.dtcms.com/wzjs/651183.html

相关文章:

  • 云南住房和城乡建设部网站ps切片工具做网站
  • 自己做彩票网站简单吗麓谷做网站的公司
  • 免费推广自己的网站前端开发入门薪水
  • 做外贸soho网站的公司个人备案网站可以做新闻站吗
  • 深圳官方网站建设网络工程师考试报名官网
  • 3合1网站建设哪家好万网域名安全锿
  • 陕西建设执业中心网站办事大厅免费推广软件排行榜
  • 做阿里巴巴网站运营一站式外贸综合服务平台
  • 权威的徐州网站建设网站开发需求网
  • 企业的网站建设费用佛山市seo网站设计哪家好
  • 个人作品展示 网站wordpress评论翻页
  • 南通模板建站定制哪个网站可以做视频软件
  • 网站美工主要工作是什么纵横网站
  • 建站公司网站用什么好响应式网站开发价格
  • 哪个网站做律师推广公关公司组织架构图
  • 宁波其它区低价企业网站搭建哪家好编程如何自学
  • h5网站建设 网站定制开发做网站推广有用吗
  • 装修公司做宣传在哪个网站企业建站系统cms
  • 北京网站制作公司报价网站建设服务器软件
  • 东莞化工网站建设做外贸怎么能上国外网站
  • 重庆网站排名推广手机网站做分享到朋友圈
  • 网站手机站怎么做的WordPress影视采集
  • 微官网 手机网站我想带货怎么找货源
  • 丹东做网站的网站竞价词怎么做
  • 网站备案验证码错误温州营销网站制作报价
  • 南阳河南网站建设价格门户网站的营销特点
  • 澄海建网站软件工程师报名官网
  • 做网站海报用什么app免费网站重生九零做商女
  • 易名中国域名门户网站住房和城乡建设部网站造价师
  • 苏州网站建设 苏州网络推广专家建立网站培训讲义