tdt-map-vue 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +151 -36
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -115,6 +115,157 @@ export default {
|
|
|
115
115
|
- `destroyMap()`:销毁地图
|
|
116
116
|
- `resizeMap()`:重新计算地图尺寸
|
|
117
117
|
|
|
118
|
+
### 方法使用示例
|
|
119
|
+
|
|
120
|
+
下面给出几个最常用的方法调用示例,方便父组件通过 `ref` 直接控制地图。
|
|
121
|
+
|
|
122
|
+
#### 1. 新增标注 `addMarkers(markers)`
|
|
123
|
+
|
|
124
|
+
`markers` 参数是一个数组,每一项的格式如下:
|
|
125
|
+
|
|
126
|
+
```js
|
|
127
|
+
[
|
|
128
|
+
{
|
|
129
|
+
id: "marker-1",
|
|
130
|
+
lng: 116.397428,
|
|
131
|
+
lat: 39.90923,
|
|
132
|
+
option: {
|
|
133
|
+
content: "<div>北京</div>",
|
|
134
|
+
iconInfo: {
|
|
135
|
+
icon: "https://example.com/icon.png",
|
|
136
|
+
offsetX: 32,
|
|
137
|
+
offsetY: 32,
|
|
138
|
+
},
|
|
139
|
+
textInfo: {
|
|
140
|
+
text: "北京",
|
|
141
|
+
offsetX: 0,
|
|
142
|
+
offsetY: -30,
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Vue 3 调用示例:
|
|
150
|
+
|
|
151
|
+
```vue
|
|
152
|
+
<template>
|
|
153
|
+
<div>
|
|
154
|
+
<TiandiMap ref="mapRef" tk="your-tianditu-api-key" />
|
|
155
|
+
<button @click="addMarker">新增标注</button>
|
|
156
|
+
</div>
|
|
157
|
+
</template>
|
|
158
|
+
|
|
159
|
+
<script setup>
|
|
160
|
+
import { ref } from "vue";
|
|
161
|
+
import TiandiMap from "tdt-map-vue";
|
|
162
|
+
|
|
163
|
+
const mapRef = ref(null);
|
|
164
|
+
|
|
165
|
+
const addMarker = () => {
|
|
166
|
+
mapRef.value?.addMarkers([
|
|
167
|
+
{
|
|
168
|
+
id: "marker-1",
|
|
169
|
+
lng: 116.397428,
|
|
170
|
+
lat: 39.90923,
|
|
171
|
+
option: {
|
|
172
|
+
content: "<div>北京</div>",
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
]);
|
|
176
|
+
};
|
|
177
|
+
</script>
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
#### 2 设置中心点 `setCenterAndZoom(center, zoom)`
|
|
182
|
+
|
|
183
|
+
- `center`:数组,格式为 `[lng, lat]`
|
|
184
|
+
- `zoom`:缩放级别,类型为 `number`
|
|
185
|
+
|
|
186
|
+
```vue
|
|
187
|
+
<template>
|
|
188
|
+
<div>
|
|
189
|
+
<TiandiMap ref="mapRef" tk="your-tianditu-api-key" />
|
|
190
|
+
<button @click="goToCity">定位到城市</button>
|
|
191
|
+
</div>
|
|
192
|
+
</template>
|
|
193
|
+
|
|
194
|
+
<script setup>
|
|
195
|
+
import { ref } from "vue";
|
|
196
|
+
import TiandiMap from "tdt-map-vue";
|
|
197
|
+
|
|
198
|
+
const mapRef = ref(null);
|
|
199
|
+
|
|
200
|
+
const goToCity = () => {
|
|
201
|
+
mapRef.value?.setCenterAndZoom([117.017362, 25.075884], 12);
|
|
202
|
+
};
|
|
203
|
+
</script>
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
#### 3 获取拾取坐标 `getMapLngLat()`
|
|
207
|
+
|
|
208
|
+
`getMapLngLat()` 没有入参,返回值为当前拾取到的坐标对象,格式如下:
|
|
209
|
+
|
|
210
|
+
```js
|
|
211
|
+
{
|
|
212
|
+
lng: 117.017362,
|
|
213
|
+
lat: 25.075884
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
```vue
|
|
218
|
+
<template>
|
|
219
|
+
<div>
|
|
220
|
+
<TiandiMap ref="mapRef" tk="your-tianditu-api-key" />
|
|
221
|
+
<button @click="getPickedPoint">获取拾取坐标</button>
|
|
222
|
+
</div>
|
|
223
|
+
</template>
|
|
224
|
+
|
|
225
|
+
<script setup>
|
|
226
|
+
import { ref } from "vue";
|
|
227
|
+
import TiandiMap from "tdt-map-vue";
|
|
228
|
+
|
|
229
|
+
const mapRef = ref(null);
|
|
230
|
+
|
|
231
|
+
const getPickedPoint = () => {
|
|
232
|
+
const point = mapRef.value?.getMapLngLat();
|
|
233
|
+
console.log("当前拾取坐标:", point);
|
|
234
|
+
};
|
|
235
|
+
</script>
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
#### 4 隐藏标注 `hideMarkers(markers)`
|
|
239
|
+
|
|
240
|
+
`markers` 参数是一个数组,通常只需要传入要隐藏标注的 `id`。
|
|
241
|
+
|
|
242
|
+
```js
|
|
243
|
+
[
|
|
244
|
+
{ id: "marker-1" },
|
|
245
|
+
{ id: "marker-2" },
|
|
246
|
+
]
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
```vue
|
|
250
|
+
<template>
|
|
251
|
+
<div>
|
|
252
|
+
<TiandiMap ref="mapRef" tk="your-tianditu-api-key" />
|
|
253
|
+
<button @click="hideMarker">隐藏标注</button>
|
|
254
|
+
</div>
|
|
255
|
+
</template>
|
|
256
|
+
|
|
257
|
+
<script setup>
|
|
258
|
+
import { ref } from "vue";
|
|
259
|
+
import TiandiMap from "tdt-map-vue";
|
|
260
|
+
|
|
261
|
+
const mapRef = ref(null);
|
|
262
|
+
|
|
263
|
+
const hideMarker = () => {
|
|
264
|
+
mapRef.value?.hideMarkers([{ id: "marker-1" }]);
|
|
265
|
+
};
|
|
266
|
+
</script>
|
|
267
|
+
```
|
|
268
|
+
|
|
118
269
|
### Vue 3 示例
|
|
119
270
|
|
|
120
271
|
```vue
|
|
@@ -231,42 +382,6 @@ export default {
|
|
|
231
382
|
|
|
232
383
|
`map` 是天地图的地图实例,可以直接拿来调用原生 API。
|
|
233
384
|
|
|
234
|
-
### 坐标拾取说明
|
|
235
|
-
|
|
236
|
-
组件内部已经集成了坐标拾取能力:
|
|
237
|
-
|
|
238
|
-
- 地图初始化时会创建 `CoordinatePickup`
|
|
239
|
-
- 拾取到的坐标会写入组件内部状态
|
|
240
|
-
- 父组件可以通过 `ref` 调用 `getMapLngLat()` 获取当前拾取结果
|
|
241
|
-
|
|
242
|
-
如果你需要在业务里做“点击地图获取坐标”的功能,可以在父组件中通过 `ref` 读取当前坐标,并结合自己的表单或标注逻辑进行处理。
|
|
243
|
-
|
|
244
|
-
## 标注数据格式
|
|
245
|
-
|
|
246
|
-
标注数据建议统一使用数组结构传入,每一项都可以包含坐标、唯一标识和扩展配置。`option` 中支持图标、文字标签和弹窗内容,因此既能展示基础点位,也能满足更复杂的业务展示需求。
|
|
247
|
-
|
|
248
|
-
```js
|
|
249
|
-
[
|
|
250
|
-
{
|
|
251
|
-
id: "marker-1",
|
|
252
|
-
lng: 116.397428,
|
|
253
|
-
lat: 39.90923,
|
|
254
|
-
option: {
|
|
255
|
-
iconInfo: {
|
|
256
|
-
icon: "https://example.com/icon.png",
|
|
257
|
-
offsetX: 32,
|
|
258
|
-
offsetY: 32,
|
|
259
|
-
},
|
|
260
|
-
content: "<div>北京市中心</div>",
|
|
261
|
-
textInfo: {
|
|
262
|
-
text: "北京",
|
|
263
|
-
offsetX: 0,
|
|
264
|
-
offsetY: -30,
|
|
265
|
-
},
|
|
266
|
-
},
|
|
267
|
-
},
|
|
268
|
-
];
|
|
269
|
-
```
|
|
270
385
|
|
|
271
386
|
## 使用建议
|
|
272
387
|
|