Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web): 修复 高德地图在web平台下translateMarker无法使用的bug (question/196586) #5097

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions packages/uni-h5/src/view/components/map/MapMarker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ export default /*#__PURE__*/ defineSystemComponent({
}
}
onMapReady((map, maps, trigger) => {
if (getIsAMap()) {
AMap.plugin('AMap.MoveAnimation', function () {
marker = new AMap.Marker()
})
}
function updateMarker(option: Props) {
const title = option.title
let position: any
Expand Down Expand Up @@ -478,22 +483,34 @@ export default /*#__PURE__*/ defineSystemComponent({
rotation = marker.getRotation()
}
const a = marker.getPosition()
const b = new (maps as QQMaps | GoogleMaps).LatLng(
destination.latitude,
destination.longitude
)
const distance =
(
maps as QQMaps | GoogleMaps
).geometry.spherical.computeDistanceBetween(a as any, b as any) /
1000
let b: any
let distance = 0
let MapsEvent: any
if (getIsAMap()) {
b = new (maps as AMap.NameSpace).LngLat(
destination.longitude,
destination.latitude
)
distance = b.distanceTo(a) / 1000
MapsEvent = (maps as AMap.NameSpace).Event
} else {
b = new (maps as QQMaps | GoogleMaps).LatLng(
destination.latitude,
destination.longitude
)
distance =
(
maps as QQMaps | GoogleMaps
).geometry.spherical.computeDistanceBetween(
a as any,
b as any
) / 1000
MapsEvent = (maps as QQMaps | GoogleMaps).event
}
const time =
(typeof duration === 'number' ? duration : 1000) /
(1000 * 60 * 60)
const speed = distance / time
const MapsEvent =
(maps as QQMaps | GoogleMaps).event ||
(maps as AMap.NameSpace).Event
const movingEvent = MapsEvent.addListener(
marker,
'moving',
Expand All @@ -510,8 +527,10 @@ export default /*#__PURE__*/ defineSystemComponent({
}
)
const event = MapsEvent.addListener(marker, 'moveend', () => {
event.remove()
movingEvent.remove()
if (!getIsAMap()) {
event.remove()
movingEvent.remove()
}
marker.lastPosition = a as QLatLng
marker.setPosition(b as any)
const label = marker.label
Expand All @@ -528,7 +547,7 @@ export default /*#__PURE__*/ defineSystemComponent({
}
})
let lastRtate = 0
if (autoRotate) {
if (autoRotate && !getIsAMap()) {
if (marker.lastPosition) {
lastRtate = (
maps as QQMaps | GoogleMaps
Expand All @@ -547,7 +566,17 @@ export default /*#__PURE__*/ defineSystemComponent({
marker.setRotation(rotation + rotate)
}
if ('moveTo' in marker) {
marker.moveTo(b as QLatLng, speed)
if (getIsAMap()) {
marker.moveTo(
b as QLatLng,
{
duration: duration,
autoRotation: autoRotate,
} as any
)
} else {
marker.moveTo(b as QLatLng, speed)
}
} else {
marker.setPosition(b as GLatLng)
MapsEvent.trigger(marker, 'moveend', {})
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-h5/src/view/components/map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,10 @@ function useMap(
if (context) {
try {
context.translate(data)
callOptions(data, `${type}:ok`)
} catch (error: any) {
callOptions(data, `${type}:fail ${error.message}`)
}
callOptions(data, `${type}:ok`)
} else {
callOptions(data, `${type}:fail not found`)
}
Expand Down
Loading