请问在HarmonyOS鸿蒙Next下怎么开发类似于Keep运动健身app上的记录跑步路线的功能

请问在HarmonyOS鸿蒙Next下怎么开发类似于Keep运动健身app上的记录跑步路线的功能 请问在鸿蒙Next下怎么开发类似于Keep运动健身app上的记录跑步路线的功能? 可以的话, 提供一下开发思路或者例子代码,谢谢帮助!

如下图所示:

![](

8 回复
  1. 先尝试在界面上加载出地图,这里有一些坑要踩,主要是证书签名问题

  2. 加载出地图之后,尝试在地图上定位自己的位置

  3. 在地图的回调方法中,有监听位置变化的回调,将返回的坐标存入数组,也可以使用定时器循环获取当前位置

  4. 有了运动轨迹的坐标数组,尝试使用addPolyline方法在地图上画线,这里要注意坐标系转换的问题

更详细的教程可以查看我录制的视频:

https://mp.weixin.qq.com/s/ii-UIBuVIiwoowhLnzX9Ng

祝您成功。

更多关于请问在HarmonyOS鸿蒙Next下怎么开发类似于Keep运动健身app上的记录跑步路线的功能的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


大佬,你这个录制的视频能在其他地方或者浏览器看吗?手机微信客户端看,屏幕太小,不方便学习!

按视频步骤操作,并更新到最新的DevEco Studio后,就能加载地图出来了。

地图页面的代码如下:

import {HMRouter, HMRouterMgr} from "@hadss/hmrouter"
import CommonConstants from '../common/Constants';
import { MapComponent, mapCommon, map } from '@kit.MapKit'
import { geoLocationManager } from '@kit.LocationKit'
import {MapUtil} from '../util/MapUtil'
import { AsyncCallback } from '@kit.BasicServicesKit';

@HMRouter({
  pageUrl: 'RecordMapPage',
})
@Entry
@Component
export struct RecordMapPage {
  @StorageLink('longitude') longitude: number = CommonConstants.LONGITUDE;
  @StorageLink('latitude') latitude: number = CommonConstants.LATITUDE;
  mapOption?: mapCommon.MapOptions = {
    position: {
      target: {
        latitude: this.latitude,
        longitude: this.longitude
      },
      zoom: CommonConstants.ZOOM
    },
    mapType: mapCommon.MapType.STANDARD
  };
  private callback?: AsyncCallback<map.MapComponentController>;
  private mapController?: map.MapComponentController;
  private marker?: map.Marker;
  private mapPolyline?: map.MapPolyline;
  private myPosition?: mapCommon.LatLng;

  aboutToAppear(): void {
    this.callback = async (err, mapController) => {
      if (!err) {
        this.mapController = mapController;
        this.mapController?.setMyLocationEnabled(true);
        this.mapController?.setMyLocationControlsEnabled(true);
        let requestInfo: geoLocationManager.CurrentLocationRequest = {
          'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX,
          'scenario': geoLocationManager.LocationRequestScenario.UNSET,
          'maxAccuracy': 0
        };
        let locationChange = async (): Promise<void> => {
        };
        geoLocationManager.on('locationChange', requestInfo, locationChange);
        geoLocationManager.getCurrentLocation(requestInfo).then(async (result) => {
          let mapPosition: mapCommon.LatLng =
            await map.convertCoordinate(mapCommon.CoordinateType.WGS84, mapCommon.CoordinateType.GCJ02, result);
          AppStorage.setOrCreate('longitude', mapPosition.longitude);
          AppStorage.setOrCreate('latitude', mapPosition.latitude);
          let cameraPosition: mapCommon.CameraPosition = {
            target: mapPosition,
            zoom: 15,
            tilt: 0,
            bearing: 0
          };
          let cameraUpdate = map.newCameraPosition(cameraPosition);
          mapController?.animateCamera(cameraUpdate, 1000);
        })

        this.mapController.on('mapClick', async (position) => {
          this.mapController?.clear();
          this.marker?.remove();
          let requestInfo: geoLocationManager.CurrentLocationRequest = {
            'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX,
            'scenario': geoLocationManager.LocationRequestScenario.UNSET,
            'maxAccuracy': 0
          };
          let locationChange = async (location: geoLocationManager.Location): Promise<void> => {
            let wgs84Position: mapCommon.LatLng = {
              latitude: location.latitude,
              longitude: location.longitude
            };
            let gcj02Posion: mapCommon.LatLng =
              await map.convertCoordinate(mapCommon.CoordinateType.WGS84, mapCommon.CoordinateType.GCJ02,
                wgs84Position);
            this.myPosition = gcj02Posion
          };
          geoLocationManager.on('locationChange', requestInfo, locationChange);

          this.marker = await MapUtil.addMarker(position, this.mapController);
          const walkingRoutes = await MapUtil.walkingRoutes(position, this.myPosition);
          await MapUtil.paintRoute(walkingRoutes!, this.mapPolyline, this.mapController);
        });
      }
    };
  }

  onPageHide(): void {
    this.mapController?.clear();
  }

  build() {
    Column(){
      MapComponent({ mapOptions: this.mapOption, mapCallback: this.callback })
        .width('100%')
        .height('100%')
    }
  }
}

Log报错如下,好像看不出什么原因?

差不多的样子,怎么开发和用到什么kit吗?能讲解一下大概思路和给些具体例子代码吗?

image.png

对了,给您看一下我做的效果图,因为是模拟器模拟定位,所以路线比较直

在HarmonyOS鸿蒙Next下开发记录跑步路线功能,可以使用鸿蒙的地理位置服务(Location Kit)和地图服务(Map Kit)。首先,通过Location Kit获取设备的实时位置信息,包括经纬度、速度、方向等数据。使用LocationManager类初始化定位服务,并设置LocationListener监听位置变化。

其次,使用Map Kit在地图上绘制跑步路线。通过Map对象初始化地图,并使用Polyline类将获取到的经纬度点连接成线,实时更新跑步路径。可以将Polyline添加到Map对象中,动态显示跑步轨迹。

为了优化性能,可以在位置更新时设置适当的间隔时间,减少不必要的计算和绘制操作。同时,确保应用在后台运行时仍能持续获取位置信息,需申请必要的权限,如ACCESS_FINE_LOCATION和ACCESS_BACKGROUND_LOCATION。

最后,可以将记录的路线数据保存到本地或云端,以便用户查看历史记录。使用鸿蒙的数据管理服务(Data Ability)或分布式数据服务(Distributed Data Kit)实现数据的存储和同步。

通过上述步骤,可以在HarmonyOS鸿蒙Next下实现类似于Keep的跑步路线记录功能。

回到顶部