Skip to content

Commit

Permalink
refactor(h5 map): 优化是否高德地图判断
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenyuWang committed Aug 2, 2022
1 parent 5de8794 commit 0fc59ce
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 51 deletions.
2 changes: 2 additions & 0 deletions src/platforms/h5/helpers/location.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 14 additions & 17 deletions src/platforms/h5/view/components/map/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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 }, {})
Expand Down Expand Up @@ -370,7 +367,7 @@ export default {
}
})
})
if (ISAMAP) {
if (IS_AMAP) {
this.isBoundsReady = true
this.$emit('boundsready')
}
Expand Down Expand Up @@ -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)
})
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -564,7 +561,7 @@ export default {
strokeDashStyle: 'solid'
}
if (ISAMAP) {
if (IS_AMAP) {
circleOptions.strokeColor = option.color
circleOptions.fillColor = option.fillColor || '#000'
} else {
Expand All @@ -584,7 +581,7 @@ export default {
}
const circle = new maps.Circle(circleOptions)
if (ISAMAP) {
if (IS_AMAP) {
map.add(circle)
}
circles.push(circle)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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])
Expand All @@ -759,7 +756,7 @@ export default {
}
})
if (ISAMAP) {
if (IS_AMAP) {
this.isBoundsReady = true
this.$emit('boundsready')
}
Expand Down
21 changes: 7 additions & 14 deletions src/platforms/h5/view/components/map/map-marker.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
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: {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 || {}
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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', {}, {
Expand Down Expand Up @@ -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) {
Expand All @@ -369,7 +362,7 @@ export default {
this._marker = null
},
removeMarkerCallout (callout) {
if (ISAMAP) {
if (IS_AMAP) {
callout.removeAMapText()
} else {
callout.setMap(null)
Expand Down
9 changes: 3 additions & 6 deletions src/platforms/h5/view/components/map/map-polygon.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
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]
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 4 additions & 9 deletions src/platforms/h5/view/components/map/maps/callout.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
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 () {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -118,7 +113,7 @@ export function createCallout (maps) {
} else {
this.alwaysVisible = false
}
if (ISAMAP) {
if (IS_AMAP) {
if (this.visible) {
this.createAMapText()
}
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/platforms/h5/view/components/map/maps/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
MapType,
getMapInfo
getMapInfo,
IS_AMAP
} from '../../../../helpers/location'
import { createCallout } from './callout'

Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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}`
Expand Down

0 comments on commit 0fc59ce

Please sign in to comment.