提交 0fc59ce1 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

refactor(h5 map): 优化是否高德地图判断

上级 5de87940
......@@ -36,3 +36,5 @@ export function getMapInfo () {
key: ''
}
}
export const IS_AMAP = getMapInfo().type === MapType.AMAP
......@@ -48,10 +48,7 @@ import {
import mapMarker from './map-marker'
import mapPolygon from './map-polygon'
import { ICON_PATH_ORIGIN, MapType, getMapInfo } from '../../../helpers/location'
const mapInfo = getMapInfo()
const ISAMAP = mapInfo.type === MapType.AMAP
import { ICON_PATH_ORIGIN, IS_AMAP } from '../../../helpers/location'
function getAMapPosition (maps, latitude, longitude) {
return new maps.LngLat(longitude, latitude)
......@@ -60,7 +57,7 @@ function getGoogleQQMapPosition (maps, latitude, longitude) {
return new maps.LatLng(latitude, longitude)
}
function getMapPosition (maps, latitude, longitude) {
return ISAMAP ? getAMapPosition(maps, latitude, longitude) : getGoogleQQMapPosition(maps, latitude, longitude)
return IS_AMAP ? getAMapPosition(maps, latitude, longitude) : getGoogleQQMapPosition(maps, latitude, longitude)
}
function getLat (latLng) {
......@@ -231,7 +228,7 @@ export default {
},
methods: {
handleAMapClick (e) {
if (ISAMAP) {
if (IS_AMAP) {
const { pageX, pageY } = e.changedTouches[0]
this.$trigger('click', { x: pageX, y: pageY }, {})
this.$trigger('tap', { x: pageX, y: pageY }, {})
......@@ -370,7 +367,7 @@ export default {
}
})
})
if (ISAMAP) {
if (IS_AMAP) {
this.isBoundsReady = true
this.$emit('boundsready')
}
......@@ -492,7 +489,7 @@ export default {
const path = []
option.points.forEach(point => {
const pointPosition = ISAMAP ? [point.longitude, point.latitude] : getGoogleQQMapPosition(maps, point.latitude, point.longitude)
const pointPosition = IS_AMAP ? [point.longitude, point.latitude] : getGoogleQQMapPosition(maps, point.latitude, point.longitude)
path.push(pointPosition)
})
......@@ -507,7 +504,7 @@ export default {
strokeDashStyle: option.dottedLine ? 'dash' : 'solid'
}
if (ISAMAP) {
if (IS_AMAP) {
polylineOptions.strokeColor = option.strokeColor
polylineOptions.strokeStyle = option.dottedLine ? 'dashed' : 'solid'
polylineOptions.isOutline = !!option.borderWidth
......@@ -535,7 +532,7 @@ export default {
polyline.push(new maps.Polyline(polylineBorderOptions))
}
const _polyline = new maps.Polyline(polylineOptions)
if (ISAMAP) {
if (IS_AMAP) {
map.add(_polyline)
}
polyline.push(_polyline)
......@@ -554,7 +551,7 @@ export default {
const circles = this.circlesSync
this.removeCircles()
this.circles.forEach(option => {
const center = ISAMAP ? [option.longitude, option.latitude] : getGoogleQQMapPosition(maps, option.latitude, option.longitude)
const center = IS_AMAP ? [option.longitude, option.latitude] : getGoogleQQMapPosition(maps, option.latitude, option.longitude)
const circleOptions = {
map,
center,
......@@ -564,7 +561,7 @@ export default {
strokeDashStyle: 'solid'
}
if (ISAMAP) {
if (IS_AMAP) {
circleOptions.strokeColor = option.color
circleOptions.fillColor = option.fillColor || '#000'
} else {
......@@ -584,7 +581,7 @@ export default {
}
const circle = new maps.Circle(circleOptions)
if (ISAMAP) {
if (IS_AMAP) {
map.add(circle)
}
circles.push(circle)
......@@ -636,7 +633,7 @@ export default {
}
$event.stopPropagation()
}
if (ISAMAP) {
if (IS_AMAP) {
this.$refs.mapContainer.appendChild(control)
} else {
map.controls[maps.ControlPosition.TOP_LEFT].push(control)
......@@ -665,7 +662,7 @@ export default {
return
}
const position = getMapPosition(maps, res.latitude, res.longitude)
if (ISAMAP) {
if (IS_AMAP) {
location = new maps.Marker({
position,
map,
......@@ -736,7 +733,7 @@ export default {
this.boundsReady(() => {
const map = this._map
if (ISAMAP) {
if (IS_AMAP) {
const _points = []
points.forEach(point => {
_points.push([point.longitude, point.latitude])
......@@ -759,7 +756,7 @@ export default {
}
})
if (ISAMAP) {
if (IS_AMAP) {
this.isBoundsReady = true
this.$emit('boundsready')
}
......
import {
MapType,
getMapInfo
} from '../../../helpers/location'
import { mapInfo, MapType, IS_AMAP } from '../../../helpers/location'
import getRealPath from 'uni-platform/helpers/get-real-path'
const mapInfo = getMapInfo()
const ISAMAP = mapInfo.type === MapType.AMAP
export default {
props: {
id: {
......@@ -105,7 +99,7 @@ export default {
maps.event.addListener(marker, 'click', (e) => {
const callout = marker.callout
if (callout && !callout.alwaysVisible) {
if (ISAMAP) {
if (IS_AMAP) {
callout.visible = !callout.visible
if (callout.visible) {
marker.callout.createAMapText()
......@@ -140,7 +134,7 @@ export default {
const maps = this._maps
const marker = this._marker
const title = option.title
const position = ISAMAP ? new maps.LngLat(option.longitude, option.latitude) : new maps.LatLng(option.latitude, option.longitude)
const position = IS_AMAP ? new maps.LngLat(option.longitude, option.latitude) : new maps.LatLng(option.latitude, option.longitude)
const img = new Image()
img.onload = () => {
const anchor = option.anchor || {}
......@@ -216,8 +210,7 @@ export default {
})
marker.label = label
} else if ('setLabel' in marker) {
// 高德
if (ISAMAP) {
if (IS_AMAP) {
const content =
`<div style="
margin-left:${labelStyle.marginLeft};
......@@ -280,7 +273,7 @@ export default {
if (callout) {
callout.setOption(calloutStyle)
} else {
if (ISAMAP) {
if (IS_AMAP) {
const callback = (self) => {
if (self.idString) {
self.$parent.$trigger('callouttap', {}, {
......@@ -342,7 +335,7 @@ export default {
let latitude
let longitude
if (ISAMAP) {
if (IS_AMAP) {
latitude = e.lnglat.lat
longitude = e.lnglat.lng
} else if (mapInfo.type === MapType.QQ) {
......@@ -369,7 +362,7 @@ export default {
this._marker = null
},
removeMarkerCallout (callout) {
if (ISAMAP) {
if (IS_AMAP) {
callout.removeAMapText()
} else {
callout.setMap(null)
......
import { hexToRgba } from 'uni-shared'
import { MapType, getMapInfo } from '../../../helpers/location'
const mapInfo = getMapInfo()
const ISAMAP = mapInfo.type === MapType.AMAP
import { IS_AMAP } from '../../../helpers/location'
export default {
props: {
// 边框虚线,腾讯地图支持,google 地图不支持,默认值为[0, 0] 为实线,非 [0, 0] 为虚线,H5 端无法像微信小程序一样控制虚线的间隔像素大小
// 边框虚线,腾讯地图支持,google 高德 地图不支持,默认值为[0, 0] 为实线,非 [0, 0] 为虚线,H5 端无法像微信小程序一样控制虚线的间隔像素大小
dashArray: {
type: Array,
default: () => [0, 0]
......@@ -69,7 +66,7 @@ export default {
const path = points.map(item => {
const { latitude, longitude } = item
return ISAMAP ? [longitude, latitude] : new _maps.LatLng(latitude, longitude)
return IS_AMAP ? [longitude, latitude] : new _maps.LatLng(latitude, longitude)
})
const { r: fcR, g: fcG, b: fcB, a: fcA } = hexToRgba(fillColor)
......
import {
MapType,
getMapInfo
} from '../../../../helpers/location'
const mapInfo = getMapInfo()
const ISAMAP = mapInfo.type === MapType.AMAP
import { IS_AMAP } from '../../../../helpers/location'
export function createCallout (maps) {
function onAdd () {
......@@ -75,7 +70,7 @@ export function createCallout (maps) {
constructor (option = {}, callback, parent) {
this.option = option || {}
this.visible = this.alwaysVisible = option.display === 'ALWAYS'
if (ISAMAP) {
if (IS_AMAP) {
this.callback = callback
this.parent = parent
if (this.visible) {
......@@ -118,7 +113,7 @@ export function createCallout (maps) {
} else {
this.alwaysVisible = false
}
if (ISAMAP) {
if (IS_AMAP) {
if (this.visible) {
this.createAMapText()
}
......@@ -167,7 +162,7 @@ export function createCallout (maps) {
destroy = onRemove
}
if (!ISAMAP) {
if (!IS_AMAP) {
const prototype = Callout.prototype
const overlay = new (maps.OverlayView || maps.Overlay)()
for (const key in overlay) {
......
import {
MapType,
getMapInfo
getMapInfo,
IS_AMAP
} from '../../../../helpers/location'
import { createCallout } from './callout'
......@@ -10,7 +11,6 @@ const GOOGLE_MAP_CALLBACKNAME = '__map_callback__'
export function loadMaps (libraries, callback) {
const mapInfo = getMapInfo()
const ISAMAP = mapInfo.type === MapType.AMAP
if (!mapInfo.key) {
console.error('Map key not configured.')
return
......@@ -22,7 +22,7 @@ export function loadMaps (libraries, callback) {
window[mapInfo.type] &&
window[mapInfo.type].maps
) {
maps = ISAMAP ? window[mapInfo.type] : window[mapInfo.type].maps
maps = IS_AMAP ? window[mapInfo.type] : window[mapInfo.type].maps
maps.Callout = maps.Callout || createCallout(maps)
callback(maps)
} else if (callbacks.length) {
......@@ -33,7 +33,7 @@ export function loadMaps (libraries, callback) {
const callbackName = GOOGLE_MAP_CALLBACKNAME + mapInfo.type
globalExt[callbackName] = function () {
delete globalExt[callbackName]
maps = ISAMAP ? window[mapInfo.type] : window[mapInfo.type].maps
maps = IS_AMAP ? window[mapInfo.type] : window[mapInfo.type].maps
maps.Callout = createCallout(maps)
callbacks.forEach((callback) => callback(maps))
callbacks.length = 0
......@@ -48,7 +48,7 @@ export function loadMaps (libraries, callback) {
src += `libraries=${libraries.join('%2C')}&`
}
if (ISAMAP) {
if (IS_AMAP) {
handleAMapSecurityPolicy(mapInfo)
}
script.src = `${src}key=${mapInfo.key}&callback=${callbackName}`
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册