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

@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吗?能讲解一下大概思路和给些具体例子代码吗?
对了,给您看一下我做的效果图,因为是模拟器模拟定位,所以路线比较直
在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的跑步路线记录功能。