-
-
[原创]【FAQ】HarmonyOS SDK 闭源开放能力 —Map Kit(5)
-
发表于: 2025-3-4 10:41 1706
-
1.问题描述:
提供两套标准方案,可根据体验需求选择:
1.地图Picker(地点详情)
用户体验:①展示地图 ②标记地点 ③用户选择已安装地图应用
接入文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/map-location-details-V5
2.导航意图面板
用户体验:①用户选择已安装地图应用
接入文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/start-intent-panel-V5
解决方案:
检查一下是否存在图片风控:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/push-image-control-V5
2.问题描述:
地图组件里marker的位置如何能固定在屏幕指定位置,比如我想让marker固定显示在 屏幕宽度/2,屏幕高度/4的位置上,该如何设置
解决方案:
这边可以尝试下使用stack布局,在地图组件上叠一个marker样式。
尝试下如下demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | import { map , mapCommon, MapComponent } from '@kit.MapKit' ; import { AsyncCallback } from '@kit.BasicServicesKit' ; / / 1 、导入文件 import display from '@ohos.display' @Entry @Component struct Index3 { private mapOptions?: mapCommon.MapOptions; private callback?: AsyncCallback< map .mapcomponentcontroller>; private mapController?: map .MapComponentController; @State screenWidth: number = 0 @State screenHeight: number = 0 aboutToAppear(): void { / / 初始化图标 / / 地图初始化参数,设置地图中心点坐标及层级 let target: mapCommon.LatLng = { latitude: 39.9181 , longitude: 116.3970193 }; let cameraPosition: mapCommon.CameraPosition = { target: target, zoom: 15 }; this.mapOptions = { position: cameraPosition }; / / 地图初始化的回调 this.callback = async (err, mapController) = > { if (!err) { this.mapController = mapController; } } display.getAllDisplays((err, data) = > { let screenWidth: number = data[ 0 ].width let screenHeight: number = data[ 0 ].height console.log( 'width = ' + screenWidth + 'height = ' + screenHeight) console.log( 'width + height = ' + JSON.stringify(data)) }) } build() { Stack() { MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback }).width( '100%' ).height( '100%' ) marker().position({ x: "44%" , y: "25%" }) } } } @Component struct marker { build() { Column({ space: 5 }) { Image($r( 'app.media.xxx' )).width( 50 ) Text( "xxxx" ) } .width( 50 ).height( 50 ) } } |
3.问题描述:
使用华为地图进行地图开发,定位图标展示不满足需求;
期望是连续定位、地图中心点不跟随定位蓝点移动、定位蓝点的方向依据设备方向旋转;
目前最合适的是用defaule模式,但是default模式定位蓝点的方向无法旋转,期望能够支持跟随设备旋转。
解决方案:
经过确认,目前的规格就是,在连续定位时,进行随设备旋转和跟随移动。
设计时分析了常见的竞品,也都没有提供目前需求的这种方式。
如果不是连续定位的话,可以不跟随移动;
后续应该不会增加类似的功能,麻烦伙伴自行选择要用哪种模式。
4.问题描述:
如何打开三方地图应用候选列表?
解决方案:
可以使用Ability Kit提供的拉起导航类应用 能力:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/start-navigation-apps-V5
5.问题描述:
使用MapComponent组件,在mapCallback回调中添加marker,大头针太大,无法设置大小。
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | import { map , mapCommon, MapComponent } from '@kit.MapKit' ; import { AsyncCallback } from '@kit.BasicServicesKit' ; import { image } from '@kit.ImageKit' ; @Entry @Component struct AddMarker { private mapOptions?: mapCommon.MapOptions; private mapController?: map .MapComponentController; private callback?: AsyncCallback< map .mapcomponentcontroller>; @State imagePixelMap: PixelMap | undefined = undefined; getMarkerPixelMap() { getContext(this).resourceManager.getMediaContent($r( "app.media.startIcon" )).then((data) = > { let arrayBuffer = data. buffer . slice (data.byteOffset, data.byteLength + data.byteOffset) let imageSource: image.ImageSource = image.createImageSource(arrayBuffer); imageSource.getImageInfo((err, value) = > { / / 获取图片资源的尺寸 console.log( 'testTag' ,`图片的尺寸为:width:${value.size.width}height:${value.size.height}`) if (err) { return ;} / / 转PixelMap,也可以在这个里面设置宽和高,比如下面是在原有的宽高基础上放大 5 倍 let opts: image.DecodingOptions = { editable: true, desiredSize: { height: value.size.height * 5 , width: value.size.width * 5 } }; imageSource.createPixelMap(opts, (err, pixelMap) = > { console.log( 'testTag' , `createPixelMap`) this.imagePixelMap = pixelMap this.addMarker() }) }) }) } addMarker() { / / Marker初始化参数 let markerOptions: mapCommon.MarkerOptions = { position: { latitude: 31.984410259206815 , longitude: 118.76625379397866 }, rotation: 0 , visible: true, zIndex: 0 , alpha: 1 , anchorU: 0.5 , anchorV: 1 , clickable: true, draggable: true, flat: false, icon:this.imagePixelMap }; console.log( 'testTag' , `addMarker`) this.mapController?.addMarker(markerOptions); } aboutToAppear(): void { / / 地图初始化参数 this.mapOptions = { position: { target: { latitude: 31.984410259206815 , longitude: 118.76625379397866 }, zoom: 15 } }; this.callback = async (err, mapController) = > { if (!err) { console.log( 'testTag' , `callback`) this.mapController = mapController; this.getMarkerPixelMap() } }; } build() { Stack() { Column() { MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback }); }.width( '100%' ) }.height( '100%' ) } } |
6.问题描述:
javaScript引入华为地图API文件,其中key要经过URL转码。是如何转码的?
解决方案:
文档上说明使用API密钥时需要调用URLEncoder.encode("Your apiKey", "UTF-8")方法对API密钥进行encodeURI编码。请参考文档:https://developer.huawei.com/consumer/cn/doc/HMSCore-Guides/javascript-api-preparations-0000001078448006#section175508543353
如何保护API密钥?:https://developer.huawei.com/consumer/cn/doc/HMSCore-Guides/faq-0000001050166999#section78166185011</map.mapcomponentcontroller></map.mapcomponentcontroller>
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课