提交 ec5088f7 编写于 作者: Q qiang

fix(H5): map polyline/circles color (fixed #3433)

上级 c8694250
import { inject, onUnmounted, watch } from 'vue' import { inject, onUnmounted, watch } from 'vue'
import { defineSystemComponent, useCustomEvent } from '@dcloudio/uni-components' import { defineSystemComponent, useCustomEvent } from '@dcloudio/uni-components'
import { Maps, Map, Circle } from './maps' import { Maps, Map, Circle, CircleOptions } from './maps'
import { hexToRgba } from '../../../helpers/hexToRgba'
const props = { const props = {
latitude: { type: [Number, String], require: true }, latitude: { type: [Number, String], require: true },
longitude: { type: [Number, String], require: true }, longitude: { type: [Number, String], require: true },
color: { type: String, default: '' }, color: { type: String, default: '#000000' },
fillColor: { type: String, default: '' }, fillColor: { type: String, default: '#00000000' },
radius: { type: [Number, String], require: true }, radius: { type: [Number, String], require: true },
strokeWidth: { type: [Number, String], default: '' }, strokeWidth: { type: [Number, String], default: '' },
level: { type: String, default: '' }, level: { type: String, default: '' },
...@@ -39,30 +40,26 @@ export default /*#__PURE__*/ defineSystemComponent({ ...@@ -39,30 +40,26 @@ export default /*#__PURE__*/ defineSystemComponent({
} }
function addCircle(option: Props) { function addCircle(option: Props) {
const center = new maps.LatLng(option.latitude, option.longitude) const center = new maps.LatLng(option.latitude, option.longitude)
function getColor(color: string) { const circleOptions: CircleOptions = {
const c = color && color.match(/#[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?/)
if ('Color' in maps) {
if (c && c.length) {
return maps.Color.fromHex(
c[0],
Number('0x' + c[1] || 255) / 255
).toRGBA()
} else {
return undefined
}
}
return color
}
circle = new maps.Circle({
map: map as any, map: map as any,
center: center as any, center: center as any,
clickable: false, clickable: false,
radius: option.radius, radius: option.radius,
strokeWeight: Number(option.strokeWidth) || 1, strokeWeight: Number(option.strokeWidth) || 1,
fillColor: getColor(option.fillColor) || getColor('#00000001'),
strokeColor: getColor(option.color) || '#000000',
strokeDashStyle: 'solid', strokeDashStyle: 'solid',
}) }
const { r: fr, g: fg, b: fb, a: fa } = hexToRgba(option.fillColor)
const { r: sr, g: sg, b: sb, a: sa } = hexToRgba(option.color)
if ('Color' in maps) {
circleOptions.fillColor = new maps.Color(fr, fg, fb, fa) as any
circleOptions.strokeColor = new maps.Color(sr, sg, sb, sa) as any
} else {
circleOptions.fillColor = `rgb(${fr}, ${fg}, ${fb})`
circleOptions.fillOpacity = fa
circleOptions.strokeColor = `rgb(${sr}, ${sg}, ${sb})`
circleOptions.strokeOpacity = sa
}
circle = new maps.Circle(circleOptions)
} }
addCircle(props as Props) addCircle(props as Props)
watch(props, updateCircle) watch(props, updateCircle)
......
import { inject, PropType, onUnmounted, watch } from 'vue' import { inject, PropType, onUnmounted, watch } from 'vue'
import { defineSystemComponent, useCustomEvent } from '@dcloudio/uni-components' import { defineSystemComponent, useCustomEvent } from '@dcloudio/uni-components'
import { Maps, Map, LatLng, Polyline } from './maps' import { Maps, Map, LatLng, Polyline, PolylineOptions } from './maps'
import { hexToRgba } from '../../../helpers/hexToRgba'
interface Point { interface Point {
latitude: number latitude: number
...@@ -60,25 +61,43 @@ export default /*#__PURE__*/ defineSystemComponent({ ...@@ -60,25 +61,43 @@ export default /*#__PURE__*/ defineSystemComponent({
path.push(new maps.LatLng(point.latitude, point.longitude)) path.push(new maps.LatLng(point.latitude, point.longitude))
}) })
const strokeWeight = Number(option.width) || 1 const strokeWeight = Number(option.width) || 1
polyline = new maps.Polyline({ const { r: sr, g: sg, b: sb, a: sa } = hexToRgba(option.color)
const { r: br, g: bg, b: bb, a: ba } = hexToRgba(option.borderColor)
const polylineOptions: PolylineOptions = {
map: map as any, map: map as any,
clickable: false, clickable: false,
path: path as any, path: path as any,
strokeWeight, strokeWeight,
strokeColor: option.color || undefined, strokeColor: option.color || undefined,
strokeDashStyle: option.dottedLine ? 'dash' : 'solid', strokeDashStyle: option.dottedLine ? 'dash' : 'solid',
}) }
const borderWidth = Number(option.borderWidth) || 0 const borderWidth = Number(option.borderWidth) || 0
const polylineBorderOptions: PolylineOptions = {
map: map as any,
clickable: false,
path: path as any,
strokeWeight: strokeWeight + borderWidth * 2,
strokeColor: option.borderColor || undefined,
strokeDashStyle: option.dottedLine ? 'dash' : 'solid',
}
if ('Color' in maps) {
polylineOptions.strokeColor = new maps.Color(sr, sg, sb, sa) as any
polylineBorderOptions.strokeColor = new maps.Color(
br,
bg,
bb,
ba
) as any
} else {
polylineOptions.strokeColor = `rgb(${sr}, ${sg}, ${sb})`
polylineOptions.strokeOpacity = sa
polylineBorderOptions.strokeColor = `rgb(${br}, ${bg}, ${bb})`
polylineBorderOptions.strokeOpacity = ba
}
if (borderWidth) { if (borderWidth) {
polylineBorder = new maps.Polyline({ polylineBorder = new maps.Polyline(polylineBorderOptions)
map: map as any,
clickable: false,
path: path as any,
strokeWeight: strokeWeight + borderWidth * 2,
strokeColor: option.borderColor || undefined,
strokeDashStyle: option.dottedLine ? 'dash' : 'solid',
})
} }
polyline = new maps.Polyline(polylineOptions)
} }
addPolyline(props as Props) addPolyline(props as Props)
watch(props, updatePolyline) watch(props, updatePolyline)
......
...@@ -3,10 +3,12 @@ export type GoogleMaps = typeof google.maps ...@@ -3,10 +3,12 @@ export type GoogleMaps = typeof google.maps
export type OverlayView = google.maps.OverlayView export type OverlayView = google.maps.OverlayView
export type LatLng = google.maps.LatLng export type LatLng = google.maps.LatLng
export type Polyline = google.maps.Polyline export type Polyline = google.maps.Polyline
export type PolylineOptions = google.maps.PolylineOptions
export type Map = google.maps.Map export type Map = google.maps.Map
export type Marker = google.maps.Marker export type Marker = google.maps.Marker
export type Label = google.maps.MarkerLabel export type Label = google.maps.MarkerLabel
export type Circle = google.maps.Circle export type Circle = google.maps.Circle
export type Icon = google.maps.Icon export type Icon = google.maps.Icon
export type Polygon = google.maps.Polygon export type Polygon = google.maps.Polygon
export type Polygon = google.maps.PolygonOptions export type PolygonOptions = google.maps.PolygonOptions
export type CircleOptions = google.maps.CircleOptions
...@@ -2,16 +2,22 @@ import { ...@@ -2,16 +2,22 @@ import {
Map as GMap, Map as GMap,
LatLng as GLatLng, LatLng as GLatLng,
Polyline as GPolyline, Polyline as GPolyline,
PolylineOptions as GPolylineOptions,
Circle as GCircle, Circle as GCircle,
CircleOptions as GCircleOptions,
} from './google/types' } from './google/types'
import { import {
Map as QMap, Map as QMap,
LatLng as QLatLng, LatLng as QLatLng,
Polyline as QPolyline, Polyline as QPolyline,
PolylineOptions as QPolylineOptions,
Circle as QCircle, Circle as QCircle,
CircleOptions as QCircleOptions,
} from './qq/types' } from './qq/types'
export type Map = GMap | QMap export type Map = GMap | QMap
export type LatLng = GLatLng | QLatLng export type LatLng = GLatLng | QLatLng
export type Polyline = GPolyline | QPolyline export type Polyline = GPolyline | QPolyline
export type PolylineOptions = GPolylineOptions & QPolylineOptions
export type Circle = GCircle | QCircle export type Circle = GCircle | QCircle
export type CircleOptions = GCircleOptions & QCircleOptions
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册