From de8bd5b5b08ac70bef36b09ecbd7cccacb494630 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Fri, 30 Jul 2021 15:10:16 +0800 Subject: [PATCH] chore(App): Map --- .../src/service/context/createMapContext.ts | 8 +-- .../src/view/components/map/index.tsx | 51 +++++++++++-------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/packages/uni-api/src/service/context/createMapContext.ts b/packages/uni-api/src/service/context/createMapContext.ts index 19b4c0c37..bf95575fd 100644 --- a/packages/uni-api/src/service/context/createMapContext.ts +++ b/packages/uni-api/src/service/context/createMapContext.ts @@ -24,7 +24,7 @@ const operateMapWrap = ( id: string, pageId: number, type: string, - options?: Data + options?: any ) => { operateMap(id, pageId, type, options, (res) => { options && operateMapCallback(options, res) @@ -41,8 +41,9 @@ export class MapContext implements UniApp.MapContext { getCenterLocation(options: any) { operateMapWrap(this.id, this.pageId, 'getCenterLocation', options) } - moveToLocation() { - operateMapWrap(this.id, this.pageId, 'moveToLocation') + // @ts-expect-error + moveToLocation(options: any) { + operateMapWrap(this.id, this.pageId, 'moveToLocation', options) } getScale(options: any) { operateMapWrap(this.id, this.pageId, 'getScale', options) @@ -73,6 +74,7 @@ export class MapContext implements UniApp.MapContext { openMapApp() {} } +// @ts-expect-error export const createMapContext = defineSyncApi( API_CREATE_MAP_CONTEXT, (id, context) => { diff --git a/packages/uni-app-plus/src/view/components/map/index.tsx b/packages/uni-app-plus/src/view/components/map/index.tsx index 6fd65b354..3a20d5d46 100644 --- a/packages/uni-app-plus/src/view/components/map/index.tsx +++ b/packages/uni-app-plus/src/view/components/map/index.tsx @@ -18,6 +18,7 @@ import { import { useNativeAttrs, useNative } from '../../../helpers/useNative' import { getCurrentPageId } from '@dcloudio/uni-core' import { getRealPath } from '../../../platform/getRealPath' +import CoverImage from '../cover-image' interface Coordinate { latitude: number @@ -83,7 +84,6 @@ type Control = { } interface Map extends PlusMapsMap { __markers__: Markers - __markers_map__: Record __lines__: Lines __circles__: Circles } @@ -152,11 +152,24 @@ export default /*#__PURE__*/ defineBuiltInComponent({ map = extend( plus.maps.create( getCurrentPageId() + '-map-' + (props.id || Date.now()), - Object.assign({}, attrs.value, position) + Object.assign( + {}, + attrs.value, + position, + (() => { + if (props.latitude && props.longitude) { + return { + center: new plus.maps.Point!( + Number(props.longitude), + Number(props.latitude) + ), + } + } + })() + ) ), { __markers__: [], - __markers_map__: {}, __lines__: [], __circles__: [], } @@ -187,12 +200,9 @@ export default /*#__PURE__*/ defineBuiltInComponent({ (position) => map && map.setStyles(position), { deep: true } ) - watch( - () => hidden.value, - (val) => { - map && map[val ? 'hide' : 'show']() - } - ) + watch(hidden, (val) => { + map && map[val ? 'hide' : 'show']() + }) watch( () => props.scale, (val) => { @@ -212,19 +222,22 @@ export default /*#__PURE__*/ defineBuiltInComponent({ () => props.markers, (val) => { _addMarkers(val as Markers, true) - } + }, + { deep: true } ) watch( () => props.polyline, (val) => { _addMapLines(val as Lines) - } + }, + { deep: true } ) watch( () => props.circles, (val) => { _addMapCircles(val as Circles) - } + }, + { deep: true } ) }) @@ -240,6 +253,7 @@ export default /*#__PURE__*/ defineBuiltInComponent({ id: control.id, iconPath: getRealPath(control.iconPath), position: position, + clickable: control.clickable, } }) ) @@ -256,12 +270,14 @@ export default /*#__PURE__*/ defineBuiltInComponent({
{mapControls.value.map((control, index) => ( - + control.clickable && trigger('controltap', {} as Event, { controlId: control.id }) } /> @@ -278,10 +294,7 @@ function useMapMethods(props: Props, trigger: CustomEventTrigger) { let map: Map | null function moveToLocation( resolve: Callback, - { - longitude, - latitude, - }: { longitude?: Props['longitude']; latitude?: Props['latitude'] } = {} + { longitude, latitude }: Partial = {} ) { if (!map) return map.setCenter( @@ -310,7 +323,7 @@ function useMapMethods(props: Props, trigger: CustomEventTrigger) { const rect = map.getBounds() resolve({ southwest: rect.getSouthWest(), - northeast: rect.getNorthEast(), // 5plus API 名字写错了 + northeast: rect.getNorthEast(), errMsg: 'getRegion:ok', }) } @@ -373,7 +386,6 @@ function useMapMethods(props: Props, trigger: CustomEventTrigger) { // 此处5+文档中PlusMapsMarker对象只有方法,没有属性 // @ts-expect-error map.__markers__.push(nativeMarker) - map && (map.__markers_map__[id + ''] = nativeMarker) }) } function _clearMarkers() { @@ -383,7 +395,6 @@ function useMapMethods(props: Props, trigger: CustomEventTrigger) { map?.removeOverlay(marker as unknown as PlusMapsOverlay) }) map.__markers__ = [] - map.__markers_map__ = {} } function _addMarkers(markers: Markers, clear?: boolean) { if (clear) { -- GitLab