diff --git a/src/core/service/api/context/create-map-context.js b/src/core/service/api/context/create-map-context.js index e50a2a454ee09d9aace3b1bdcee4b0fadb2df368..9396dc9bd1f060cd9b6fbf653ff57eeef9ea70c5 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 45d546b40fa0c3823785f3154dcfccfb24803dad..3fa1268a965b1fccc5ef5aba17769b738e4a3435 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,