useMap
地图
何时使用
当需要操作地图相关
API
警告
在React中, mapContext不能作为方法可用的标准. 由于context创建的异步性. 请手动获取或者增加delay再调用方法
const [
mapContext,
{
get,
open,
include,
moveTo,
translate,
toggleMarkers,
},
] = useMap(mapId, component?);
参数说明
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
mapId | 对应Map 组件 id | string | - |
component | 若为自定义组件对应组件实例 | TaroGeneral.IAnyObject | - |
返回值说明
返回值 | 说明 | 类型 |
---|---|---|
mapContext | 地图实例 | MapContext \| undefined |
get | 获取当前地图中心的经纬度、范围视野、缩放级别、旋转角、倾斜角 | PromiseOptionalAction<string, GetSuccessCallbackResult>; |
open | 拉起地图 APP 选择导航 | PromiseAction<ExcludeOption<MapContext.OpenMapAppOption>> |
include | 缩放视野展示所有经纬度 | PromiseParamsAction<(points: MapContext.MapPosition[], padding?: number[]) => void> |
moveTo | 将地图中心移置当前定位点 | PromiseOptionalAction<ExcludeOption<MapContext.MoveToLocationOption>> |
translate | 平移 marker,带动画 | PromiseAction<ExcludeOption<MapContext.TranslateMarkerOption>> |
toggleMarkers | 展示/移除 marker | PromiseParamsAction<(markers: Marker \| number[], clear?: boolean) => void> |
GetSuccessCallbackResult
export type GetSuccessCallbackResult = {
center: UnionResult<MapContext.GetCenterLocationSuccessCallbackResult>;
region: UnionResult<MapContext.GetRegionSuccessCallbackResult>;
rotate: UnionResult<MapContext.GetRotateSuccessCallbackResult>;
scale: UnionResult<MapContext.GetScaleSuccessCallbackResult>;
skew: UnionResult<MapContext.GetSkewSuccessCallbackResult>;
};
代码演示
- React
- Vue
media/useMap/index
import React from 'react';
import { Map } from '@tarojs/components';
import { useState } from '@taro-hooks/core';
import { useModal, useMap } from 'taro-hooks';
import DemoContent from '@src/components/DemoContent';
import { Button, Cell } from '@taroify/core';
import Icon from '../../../assets/tabbar/NO.0001.png';
import './index.less';
export default () => {
const mapId = 'demo-map-id';
const points = [
{ latitude: 30.653186, longitude: 104.083735 },
{ latitude: 30.655546, longitude: 104.0778 },
];
const markers = [
{
id: 0,
title: '太古里marker',
iconPath: Icon,
width: 50,
height: 50,
...points[1],
},
];
const translateInfo = {
autoRotate: true,
moveWithRotate: true,
markerId: 0,
rotate: 0,
destination: points[0],
};
const [mapContext, { get, include, moveTo, translate, toggleMarkers }] =
useMap(mapId);
const [mapInfo, setMapInfo] = useState({});
const handleGetInfo = async () => {
try {
const result = await get();
console.log('result', result);
setMapInfo(result);
toggleMarkers(markers, true);
} catch (e) {
show({ content: '获取中心位置失败' });
}
};
const handleInclude = async () => {
try {
await include(points);
show({ content: '设置经纬成功' });
} catch (e) {
show({ content: '设置经纬失败' });
}
};
const handleTranslate = async () => {
try {
// may clear by user
await toggleMarkers(markers, true);
moveTo(points[0]);
await translate(translateInfo);
show({ content: '平移marker成功' });
} catch (e) {
show({ content: '平移marker失败' });
}
};
const show = useModal({
title: 'useMap',
showCancel: false,
mask: true,
});
return (
<DemoContent>
<Map
id={mapId}
className="gap"
showLocation
showCompass
showScale
markers={markers}
latitude={mapInfo?.center?.latitude}
longitude={mapInfo?.center?.longitude}
/>
<Button
block
color="primary"
className="gap"
onClick={() => handleGetInfo()}
shape="square"
>
获取信息
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => handleInclude()}
shape="square"
>
展示指定经纬
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => moveTo()}
shape="square"
>
移至当前定位
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => handleTranslate()}
shape="square"
>
平移marker
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => toggleMarkers([0])}
shape="square"
>
移除所有marker
</Button>
<Cell.Group title="地图信息">
{Object.entries(mapInfo ?? {}).map(([key, value]) => (
<Cell key={key} title={key} brief={JSON.stringify(value)}></Cell>
))}
</Cell.Group>
</DemoContent>
);
};
media/useMap/index
<template>
<demo-content>
<map
class="gap"
:id="mapId"
:show-location="true"
:show-scale="true"
:show-compass="true"
:latitude="mapInfo?.center?.latitude"
:longitude="mapInfo?.center?.longitude"
:markers="markers"
></map>
<nut-button
shape="square"
type="primary"
class="gap"
block
@click="handleInclude()"
>展示指定经纬</nut-button
>
<nut-button
shape="square"
type="primary"
class="gap"
block
@click="moveTo()"
>移至当前定位</nut-button
>
<nut-button
shape="square"
type="primary"
class="gap"
block
@click="handleTranslate()"
>平移marker</nut-button
>
<nut-button
shape="square"
type="primary"
class="gap"
block
@click="toggleMarkers([0])"
>移除所有marker</nut-button
>
<nut-cell-group title="地图信息">
<nut-cell
v-for="(value, key) in mapInfo"
:key="key"
:title="key"
:sub-title="$filters.stringify(value)"
></nut-cell>
</nut-cell-group>
</demo-content>
</template>
<script setup lang="ts">
import { useState, useEffect } from '@taro-hooks/core';
import { escapeState } from '@taro-hooks/shared';
import { useModal, useMap } from 'taro-hooks';
const mapId = 'demo-map-id';
const points = [
{ latitude: 30.653186, longitude: 104.083735 },
{ latitude: 30.655546, longitude: 104.0778 },
];
const markers = [
{
id: 0,
title: '太古里marker',
iconPath: require('../../../assets/tabbar/NO.0001.png'),
width: 50,
height: 50,
...points[1],
},
];
const translateInfo = {
autoRotate: true,
moveWithRotate: true,
markerId: 0,
rotate: 0,
destination: points[0],
};
const [mapContext, { get, include, moveTo, translate, toggleMarkers }] =
useMap(mapId);
const [mapInfo, setMapInfo] = useState({});
const handleGetInfo = async () => {
try {
const result = await get();
setMapInfo(result);
} catch (e) {
show({ content: '获取中心位置失败' });
}
};
const handleInclude = async () => {
try {
await include(points);
show({ content: '设置经纬成功' });
} catch (e) {
show({ content: '设置经纬失败' });
}
};
const handleTranslate = async () => {
try {
// may clear by user
await toggleMarkers(markers, true);
moveTo(points[0]);
await translate(translateInfo);
show({ content: '平移marker成功' });
} catch (e) {
show({ content: '平移marker失败' });
}
};
useEffect(() => {
if (escapeState(mapContext)) {
handleGetInfo();
toggleMarkers(markers, true);
}
}, [mapContext]);
const show = useModal({
title: 'useMap',
showCancel: false,
mask: true,
});
</script>
<style>
#demo-map-id {
position: relative;
left: auto;
top: auto;
width: 100%;
height: 200px;
}
</style>
Hook 支持度
微信小程序 | H5 | ReactNative |
---|---|---|
✔️ |