UNPKG

@uiw/react-amap-marker

Version:

点标记是用来标示某个位置点信息的一种地图要素,本章介绍如何在地图图面使用点标记。

355 lines (325 loc) 19.9 kB
<div markdown="1"> <sup>使用 <a href="https://wangchujiang.com/#/app" target="_blank">我的应用</a> 也是一种 <a href="https://wangchujiang.com/#/sponsor" target="_blank">支持</a> 我的方式:</sup> <br> <a target="_blank" href="https://apps.apple.com/app/Vidwall/6747587746" title="Vidwall for macOS"><img align="center" alt="Vidwall" height="52" width="52" src="https://github.com/user-attachments/assets/7b5df70a-ed91-4d4b-85be-f00e60a09ce9"></a> <a target="_blank" href="https://wangchujiang.com/mousio-hint/" title="Mousio Hint for macOS"><img align="center" alt="Mousio Hint" height="52" width="52" src="https://github.com/user-attachments/assets/3c0af128-0cef-44e5-a8db-4741dc5a6690"></a> <a target="_blank" href="https://apps.apple.com/app/6746747327" title="Mousio for macOS"><img align="center" alt="Mousio" height="52" width="52" src="https://github.com/user-attachments/assets/9edf61ff-5a6c-4676-9cc2-8fd3c1ad0dfb"></a> <a target="_blank" href="https://apps.apple.com/app/6745227444" title="Musicer for macOS"><img align="center" alt="Musicer" height="52" width="52" src="https://github.com/user-attachments/assets/b7abfba8-88ff-4c86-a125-43073d5aef22"></a> <a target="_blank" href="https://apps.apple.com/app/6743841447" title="Audioer for macOS"><img align="center" alt="Audioer" height="52" width="52" src="https://github.com/user-attachments/assets/7a836865-8c90-4119-87bc-19e06a76c957"></a> <a target="_blank" href="https://apps.apple.com/app/6744690194" title="FileSentinel for macOS"><img align="center" alt="FileSentinel" height="52" width="52" src="https://github.com/user-attachments/assets/28bce2cc-290e-45bf-9068-585ff6ecafe9"></a> <a target="_blank" href="https://apps.apple.com/app/6743495172" title="FocusCursor for macOS"><img align="center" alt="FocusCursor" height="52" width="52" src="https://github.com/user-attachments/assets/d543668a-737b-4853-a6bb-eaa269e69836"></a> <a target="_blank" href="https://apps.apple.com/app/6742680573" title="Videoer for macOS"><img align="center" alt="Videoer" height="52" width="52" src="https://github.com/user-attachments/assets/10ffb0f1-0625-40d6-93f1-2c2496592595"></a> <a target="_blank" href="https://apps.apple.com/app/6740425504" title="KeyClicker for macOS"><img align="center" alt="KeyClicker" height="52" width="52" src="https://github.com/user-attachments/assets/5a19fcb9-cb81-4855-b4ea-31c604d9612a"></a> <a target="_blank" href="https://apps.apple.com/app/6739052447" title="DayBar for macOS"><img align="center" alt="DayBar" height="52" width="52" src="https://github.com/user-attachments/assets/771b608d-594c-492d-8532-d9231e383f5b"></a> <a target="_blank" href="https://apps.apple.com/app/6739444407" title="Iconed for macOS"><img align="center" alt="Iconed" height="52" width="52" src="https://github.com/user-attachments/assets/8a35dc7b-4faf-4e2a-9311-f66d6844a896"></a> <a target="_blank" href="https://apps.apple.com/app/6737160756" title="RightMenu Master for macOS"><img align="center" alt="RightMenu Master" height="52" width="52" src="https://github.com/user-attachments/assets/39a76541-71bf-4de7-a01c-c62f0557dff5"></a> <a target="_blank" href="https://apps.apple.com/app/6723903021" title="Paste Quick for macOS"><img align="center" alt="Quick RSS" height="52" width="52" src="https://github.com/user-attachments/assets/bdaad5b7-9810-44ce-8f17-8410864465d2"></a> <a target="_blank" href="https://apps.apple.com/app/6670696072" title="Quick RSS for macOS/iOS"><img align="center" alt="Quick RSS" height="52" width="52" src="https://github.com/user-attachments/assets/374106b5-a448-4d1d-9ccb-b04b6bc681ed"></a> <a target="_blank" href="https://apps.apple.com/app/6670167443" title="Web Serve for macOS"><img align="center" alt="Web Serve" height="52" width="52" src="https://github.com/user-attachments/assets/e1d9f76f-0f3d-4ba5-8a15-253ee173bb1c"></a> <a target="_blank" href="https://apps.apple.com/app/6503953628" title="Copybook Generator for macOS/iOS"><img align="center" alt="Copybook Generator" height="52" width="52" src="https://github.com/jaywcjlove/jaywcjlove/assets/1680273/b90e42ff-158b-4534-82ca-5898fd0e8d73"></a> <a target="_blank" href="https://apps.apple.com/app/6471227008" title="DevTutor for macOS/iOS"><img align="center" alt="DevTutor for SwiftUI" height="52" width="52" src="https://github.com/jaywcjlove/jaywcjlove/assets/1680273/f15c154d-0192-48eb-8e0e-9e245ffd974a"></a> <a target="_blank" href="https://apps.apple.com/app/6479819388" title="RegexMate for macOS/iOS"><img align="center" alt="RegexMate" height="52" width="52" src="https://github.com/jaywcjlove/jaywcjlove/assets/1680273/aabe5aa9-9a96-4390-8bed-c3e4023d0dea"></a> <a target="_blank" href="https://apps.apple.com/app/6479194014" title="Time Passage for macOS/iOS"><img align="center" alt="Time Passage" height="52" width="52" src="https://github.com/jaywcjlove/time-passage/assets/1680273/6f30e429-e6f3-4dbe-9921-a5effe2a05e9"></a> <a target="_blank" href="https://apps.apple.com/app/6478772538" title="IconizeFolder for macOS"><img align="center" alt="Iconize Folder" height="52" width="52" src="https://github.com/jaywcjlove/jaywcjlove/assets/1680273/fa9d8b9c-1e51-4ded-877c-fa5b21c47220"></a> <a target="_blank" href="https://apps.apple.com/app/6478511402" title="Textsound Saver for macOS/iOS"><img align="center" alt="Textsound Saver" height="52" width="52" src="https://github.com/jaywcjlove/jaywcjlove/assets/1680273/0595e842-980b-4574-8891-a8ba853a08be"></a> <a target="_blank" href="https://apps.apple.com/app/6476924627" title="Create Custom Symbols for macOS"><img align="center" alt="Create Custom Symbols" height="52" width="52" src="https://github.com/jaywcjlove/jaywcjlove/assets/1680273/8cd022ce-a3f1-4e89-b7c6-6fbd0d4db77c"></a> <a target="_blank" href="https://apps.apple.com/app/6476452351" title="DevHub for macOS"><img align="center" alt="DevHub" height="52" width="52" src="https://github.com/user-attachments/assets/4a44a4fd-67ce-430b-af0a-72f18feaa47d"></a> <a target="_blank" href="https://apps.apple.com/app/6476400184" title="Resume Revise for macOS"><img align="center" alt="Resume Revise" height="52" width="52" src="https://github.com/jaywcjlove/jaywcjlove/assets/1680273/c9954a20-1905-48de-bdf8-d71837974aa2"></a> <a target="_blank" href="https://apps.apple.com/app/6472593276" title="Palette Genius for macOS"><img align="center" alt="Palette Genius" height="52" width="52" src="https://github.com/jaywcjlove/jaywcjlove/assets/1680273/27340413-d355-45b2-8f6f-6ac37682d957"></a> <a target="_blank" href="https://apps.apple.com/app/6470879005" title="Symbol Scribe for macOS"><img align="center" alt="Symbol Scribe" height="52" width="52" src="https://github.com/jaywcjlove/jaywcjlove/assets/1680273/c7249f05-fa70-4def-a1e9-571d5f171fc9"></a> </div> <hr> Marker 点标记组件 === [![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor) [![npm version](https://img.shields.io/npm/v/@uiw/react-amap-marker.svg)](https://www.npmjs.com/package/@uiw/react-amap-marker) [![Downloads](https://img.shields.io/npm/dm/@uiw/react-amap-marker.svg?style=flat)](https://www.npmjs.com/package/@uiw/react-amap-marker) 点标记是用来标示某个位置点信息的一种地图要素,本章介绍如何在地图图面使用点标记。 ```jsx import { Marker } from '@uiw/react-amap'; // 或者单独安装使用 import { Marker } from '@uiw/react-amap-marker'; ``` ### 基本用法 ```jsx mdx:preview import ReactDOM from 'react-dom'; import React, { useState, useRef } from 'react'; import { Map, APILoader, Marker } from '@uiw/react-amap'; const Example = () => { const [show, setShow] = useState(true); return ( <> <button onClick={() => setShow(!show)}> {show ? '隐藏' : '显示'} </button> <div style={{ width: '100%', height: '300px' }}> <Map zoom={4}> <Marker visible={show} title="北京市" position={new AMap.LngLat(116.405285,39.904989)} /> <Marker visible={show} title="天津市" position={new AMap.LngLat(117.190182,39.125596)} /> <Marker visible={show} title="河北省" position={new AMap.LngLat(114.502461,38.045474)} /> <Marker visible={show} title="山西省" position={new AMap.LngLat(112.549248,37.857014)} /> <Marker visible={show} title="内蒙古自治区" position={new AMap.LngLat(111.670801,40.818311)} /> <Marker visible={show} title="辽宁省" position={new AMap.LngLat(123.429096,41.796767)} /> <Marker visible={show} title="吉林省" position={new AMap.LngLat(125.3245,43.886841)} /> <Marker visible={show} title="黑龙江省" position={new AMap.LngLat(126.642464,45.756967)} /> <Marker visible={show} title="上海市" position={new AMap.LngLat(121.472644,31.231706)} /> <Marker visible={show} title="江苏省" position={new AMap.LngLat(118.767413,32.041544)} /> <Marker visible={show} title="浙江省" position={new AMap.LngLat(120.153576,30.287459)} /> <Marker visible={show} title="安徽省" position={new AMap.LngLat(117.283042,31.86119)} /> <Marker visible={show} title="福建省" position={new AMap.LngLat(119.306239,26.075302)} /> <Marker visible={show} title="江西省" position={new AMap.LngLat(115.892151,28.676493)} /> <Marker visible={show} title="山东省" position={new AMap.LngLat(117.000923,36.675807)} /> <Marker visible={show} title="河南省" position={new AMap.LngLat(113.665412,34.757975)} /> <Marker visible={show} title="湖北省" position={new AMap.LngLat(114.298572,30.584355)} /> <Marker visible={show} title="湖南省" position={new AMap.LngLat(112.982279,28.19409)} /> <Marker visible={show} title="广东省" position={new AMap.LngLat(113.280637,23.125178)} /> <Marker visible={show} title="广西壮族自治区" position={new AMap.LngLat(108.320004,22.82402)} /> <Marker visible={show} title="海南省" position={new AMap.LngLat(110.33119,20.031971)} /> <Marker visible={show} title="重庆市" position={new AMap.LngLat(106.504962,29.533155)} /> <Marker visible={show} title="四川省" position={new AMap.LngLat(104.065735,30.659462)} /> <Marker visible={show} title="贵州省" position={new AMap.LngLat(106.713478,26.578343)} /> <Marker visible={show} title="云南省" position={new AMap.LngLat(102.712251,25.040609)} /> <Marker visible={show} title="西藏自治区" position={new AMap.LngLat(91.132212,29.660361)} /> <Marker visible={show} title="陕西省" position={new AMap.LngLat(108.948024,34.263161)} /> <Marker visible={show} title="甘肃省" position={new AMap.LngLat(103.823557,36.058039)} /> <Marker visible={show} title="青海省" position={new AMap.LngLat(101.778916,36.623178)} /> <Marker visible={show} title="宁夏回族自治区" position={new AMap.LngLat(106.278179,38.46637)} /> <Marker visible={show} title="新疆维吾尔自治区" position={new AMap.LngLat(87.617733,43.792818)} /> <Marker visible={show} title="台湾省" position={new AMap.LngLat(121.509062,25.044332)} /> <Marker visible={show} title="香港特別行政區" position={new AMap.LngLat(114.173355,22.320048)} /> <Marker visible={show} title="澳門特別行政區" position={new AMap.LngLat(113.54909,22.198951)} /> </Map> </div> </> ); } const Mount = () => ( <APILoader akey="a7a90e05a37d3f6bf76d4a9032fc9129"> <Example /> </APILoader> ); export default Mount; ``` ### 点标注的文本标签 ```jsx mdx:preview import ReactDOM from 'react-dom'; import React, { useState } from 'react'; import { Map, APILoader, Marker } from '@uiw/react-amap'; const Example = () => { const [show, setShow] = useState(true); const [count, setCount] = useState(1); return ( <> <button onClick={() => setShow(!show)}> {show ? '隐藏' : '显示'} </button> <div style={{ width: '100%', height: '300px' }}> <Map zoom={4}> <Marker visible={show} title="北京市" // offset={new AMap.Pixel(-13, -30)} label={{ // 设置文本标注偏移量 // offset: new AMap.Pixel(20, 20), // 设置文本标注内容 content: "<div class='info'>我是 marker 的 label 标签</div>", // 设置文本标注方位 direction: 'right' }} position={new AMap.LngLat(117.283042,31.86119)} /> <Marker visible={show} title="北京市" label={{ // 设置文本标注内容 content: "<div class='info'>我是 marker 222的 label 标签</div>", // 设置文本标注方位 direction: 'right' }} draggable bubble={true} // content="<div>我是 marker 的 label 标签</div>" position={new AMap.LngLat(113.280637,23.125178)} > <div style={{ height: '32px', width: '23px', margin: '-31px 0 0 -10px', background: 'url(https://amap.com/assets/img/poi-marker.png) 437px 267px', backgroundSize: '437px 267px', backgroundPosition: '-9px -3px' }} /> <div> 我是 marker 的 {count} label 标签 <button onClick={() => setCount(count + 1)}> {count} 点击 + 1 </button> </div> </Marker> </Map> </div> </> ); } const Mount = () => ( <APILoader akey="a7a90e05a37d3f6bf76d4a9032fc9129"> <Example /> </APILoader> ); export default Mount; ``` ### 自适应显示多个点标记 ```jsx mdx:preview import ReactDOM from 'react-dom'; import React, { useState, useRef } from 'react'; import { Map, APILoader, Marker } from '@uiw/react-amap'; const Example = () => { const [show, setShow] = useState(true); const [map, setMap] = useState(); return ( <> <button onClick={() => setShow(!show)}> {show ? '隐藏' : '显示'} </button> <button onClick={() => { if (map) { map.setFitView(null, false, [150, 60, 100, 60]); } }}> 地图自适应显示到合适的范围 </button> <div style={{ width: '100%', height: '300px' }}> <Map zoom={13} center={[116.397428, 39.90923]} ref={(instance) => { if (instance && instance.map && !map) { setMap(instance.map); } }} > <Marker visible={show} icon={new AMap.Icon({ imageSize: new AMap.Size(25, 34), image: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-1.png' })} offset={new AMap.Pixel(-13, -30)} position={new AMap.LngLat(116.205467, 39.907761)} /> <Marker visible={show} icon={new AMap.Icon({ imageSize: new AMap.Size(25, 34), image: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-2.png' })} offset={new AMap.Pixel(-13, -30)} position={[116.368904, 39.913423]} /> <Marker visible={show} icon={new AMap.Icon({ imageSize: new AMap.Size(25, 34), image: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-3.png' })} offset={new AMap.Pixel(-13, -30)} position={[116.305467, 39.807761]} /> </Map> </div> </> ); } const Mount = () => ( <APILoader akey="a7a90e05a37d3f6bf76d4a9032fc9129"> <Example /> </APILoader> ); export default Mount; ``` ### 自定义标记 ```jsx mdx:preview import ReactDOM from 'react-dom'; import React, { useState, useRef } from 'react'; import { Map, APILoader, Marker } from '@uiw/react-amap'; const Example = () => { const [show, setShow] = useState(true); const [map, setMap] = useState(); return ( <> <button onClick={() => setShow(!show)}> {show ? '隐藏' : '显示'} </button> <button onClick={() => { if (map) { map.setFitView(null, false, [150, 60, 100, 60]); } }}> 地图自适应显示到合适的范围 </button> <div style={{ width: '100%', height: '300px' }}> <Map zoom={10} center={[116.397428, 39.90923]} ref={(instance) => { if (instance && instance.map && !map) { setMap(instance.map); } }} > <Marker visible={show} offset={new AMap.Pixel(-13, -30)} position={new AMap.LngLat(116.205467, 39.907761)} > <div style={{ background: 'red', color: '#fff' }}> 我是 Marker </div> </Marker> </Map> </div> </> ); } const Mount = () => ( <APILoader akey="a7a90e05a37d3f6bf76d4a9032fc9129"> <Example /> </APILoader> ); export default Mount; ``` ### Props [更多参数设置](https://github.com/uiwjs/react-amap/blob/268303d/src/types/overlay.d.ts#L8-L111) | 参数 | 说明 | 类型 | 默认值 | |--------- |-------- |--------- |-------- | | visible | 覆盖物是否可见。 | `boolean` | - | | map | 要显示该marker的地图对象 | `Map` | - | | position | 点标记在地图上显示的位置,默认为地图中心点 | `LngLat` | - | | offset | 点标记显示位置偏移量,默认值为 `Pixel(-10,-34)`。 | `Pixel` | - | | content | 点标记显示内容。可以是 `HTML` 要素字符串或者 `HTML DOM` 对象。`icon` 属性将被覆盖。 | `ReactNode` | - | | children | 点标记显示内容。`children`有效时,`icon` 属性将被覆盖。 | `ReactNode` | - | ### 事件 [事件类型文档](https://github.com/uiwjs/react-amap/blob/268303d/src/types/overlay.d.ts#L112-L182) | 参数 | 说明 | 类型 | | ---- | ---- | ---- | | onClick | 鼠标左键单击事件 | `(event: MapsEvent): void;` | | onDblClick | 鼠标左键双击事件 | `(event: MapsEvent): void;` | | onRightClick | 鼠标右键单击事件 | `(event: MapsEvent): void;` | | onMouseMove | 鼠标移动 | `(event: MapsEvent): void;` | | onMouseOver | 鼠标移近点标记时触发事件 | `(event: MapsEvent): void;` | | onMouseOut | 鼠标移出点标记时触发事件 | `(event: MapsEvent): void;` | | onMouseDown | 鼠标在点标记上按下时触发事件 | `(event: MapsEvent): void;` | | onMouseUp | 鼠标在点标记上按下后抬起时触发事件 | `(event: MapsEvent): void;` | | onDragStart | 开始拖拽点标记时触发事件 | `(event: MapsEvent): void;` | | onDragging | 鼠标拖拽移动点标记时触发事件 | `(event: MapsEvent): void;` | | onDragEnd | 点标记拖拽移动结束触发事件 | `(event: MapsEvent): void;` | | onMoving | 点标记在执行 | `(obj: { passedPath:Array<LngLat> }): void;` | | onMoveEnd | 点标记执行moveTo动画结束时触发事件,也可以由moveAlong方法触发 | `(): void;` | | onMoveAlong | 点标记执行moveAlong动画一次后触发事件 | `(): void;` | | onTouchStart | 触摸开始时触发事件,仅适用移动设备 | `(event: MapsEvent): void;` | | onTouchMove | 触摸移动进行中时触发事件,仅适用移动设备 | `(event: MapsEvent): void;` | | onTouchEnd | 触摸结束时触发事件,仅适用移动设备 | `(event: MapsEvent): void;` |