From 5623740d45d147b0b95fdd33ce8e96552f2f334c Mon Sep 17 00:00:00 2001 From: handongxun Date: Thu, 9 Jan 2020 18:35:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20V3=E7=89=88=E6=9C=AC=20vue=20=20?= =?UTF-8?q?=E9=83=A8=E5=88=86=20API=20=E6=97=A0=E6=95=88=E5=8F=8A=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=B1=9E=E6=80=A7=E4=B8=8D=E8=83=BD=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=9A=84=20Bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/api/context/create-map-context.js | 40 +++++++----- .../app-plus/view/components/map/index.vue | 65 +++++++++++++++---- 2 files changed, 74 insertions(+), 31 deletions(-) diff --git a/src/core/service/api/context/create-map-context.js b/src/core/service/api/context/create-map-context.js index e50a2a454..9396dc9bd 100644 --- a/src/core/service/api/context/create-map-context.js +++ b/src/core/service/api/context/create-map-context.js @@ -1,42 +1,48 @@ import { invokeMethod, getCurrentPageVm -} from '../../platform' +} from '../../platform' + +import { + callback +} from 'uni-shared' function operateMapPlayer (mapId, pageVm, type, data) { invokeMethod('operateMapPlayer', mapId, pageVm, type, data) } +UniServiceJSBridge.subscribe('onMapMethodCallback', ({ + callbackId, + data +}) => { + callback.invoke(callbackId, data) +}) + +const methods = ['getCenterLocation', 'translateMarker', 'getScale', 'getRegion'] + export class MapContext { constructor (id, pageVm) { this.id = id this.pageVm = pageVm } - getCenterLocation (args) { - operateMapPlayer(this.id, this.pageVm, 'getCenterLocation', args) - } - moveToLocation () { operateMapPlayer(this.id, this.pageVm, 'moveToLocation') } - translateMarker (args) { - operateMapPlayer(this.id, this.pageVm, 'translateMarker', args) - } - includePoints (args) { operateMapPlayer(this.id, this.pageVm, 'includePoints', args) } +} - getRegion (args) { - operateMapPlayer(this.id, this.pageVm, 'getRegion', args) - } - - getScale (args) { - operateMapPlayer(this.id, this.pageVm, 'getScale', args) - } -} +methods.forEach(function (method) { + MapContext.prototype[method] = callback.warp(function (options, callbackId) { + operateMapPlayer(this.id, this.pageVm, method, { + options, + callbackId + }) + }) +}) export function createMapContext (id, context) { if (context) { diff --git a/src/platforms/app-plus/view/components/map/index.vue b/src/platforms/app-plus/view/components/map/index.vue index 45d546b40..3fa1268a9 100644 --- a/src/platforms/app-plus/view/components/map/index.vue +++ b/src/platforms/app-plus/view/components/map/index.vue @@ -146,7 +146,9 @@ export default { const list = this.controls.map((control) => { let position = { position: 'absolute' }; ['top', 'left', 'width', 'height'].forEach(key => { - position[key] = control.position[key] + 'px' + if (control.position[key]) { + position[key] = control.position[key] + 'px' + } }) return { id: control.id, @@ -160,6 +162,25 @@ export default { watch: { hidden (val) { this.map && this.map[val ? 'hide' : 'show']() + }, + latitude (val) { + this.map && this.map.setStyles({ + center: new plus.maps.Point(this.longitude, this.latitude) + }) + }, + longitude (val) { + this.map && this.map.setStyles({ + center: new plus.maps.Point(this.longitude, this.latitude) + }) + }, + markers (val) { + this.map && this._addMarkers(val) + }, + polyline (val) { + this.map && this._addMapLines(val) + }, + circles (val) { + this.map && this._addMapCircles(val) } }, mounted () { @@ -175,17 +196,6 @@ export default { if (this.hidden) { map.hide() } - this.$watch('attrs', () => { - if (this.map) { - this.map.setStyles(this.attrs) - // TODO 临时处理更新 longitude, latitude 无效问题 - this.map.setStyles({ - center: new plus.maps.Point(this.longitude, this.latitude) - }) - } - }, { - deep: true - }) this.$watch('position', () => { this.map && this.map.setStyles(this.position) }, { @@ -214,13 +224,40 @@ export default { } this.map && this[type](data) }, - getRegion () { + moveToLocation (data) { + this.map.setCenter(new plus.maps.Point(this.longitude, this.latitude)) + }, + getCenterLocation ({ callbackId }) { + const center = this.map.getCenter() + this._publishHandler(callbackId, { + longitude: center.longitude, + latitude: center.latitude, + errMsg: 'getCenterLocation:ok' + }) }, - getScale () { + getRegion ({ callbackId }) { + const rect = this.map.getBounds() + this._publishHandler(callbackId, { + southwest: rect.southwest, + northeast: rect.northeast || rect.northease, // 5plus API 名字写错了 + errMsg: 'getRegion:ok' + }) + }, + getScale ({ callbackId }) { + this._publishHandler(callbackId, { + scale: this.map.getZoom(), + errMsg: 'getScale:ok' + }) }, controlclick (e) { this.$trigger('controltap', {}, { id: e.id }) }, + _publishHandler (callbackId, data) { + UniViewJSBridge.publishHandler('onMapMethodCallback', { + callbackId, + data + }, this.$page.id) + }, _addMarker (nativeMap, marker) { const { id, -- GitLab