map.uvue 14.7 KB
Newer Older
Anne_LXM's avatar
Anne_LXM 已提交
1
<template>
2
  <view class="content">
3 4 5
    <map class="map" id="map1" ref="map1" :longitude="location.longitude" :latitude="location.latitude" :scale="scale"
      :markers="markers" :include-points="includePoints" :polyline="polyline" :polygons="polygons" :circles="circles"
      :controls="controls" :show-location="showLocation" :enable-3D="enable3D" :rotate="rotate" :skew="skew"
6 7
      :show-compass="showCompass" :enable-overlooking="enableOverlooking" :enable-zoom="enableZoom"
      :enable-scroll="enableScroll" :enable-rotate="enableRotate" :enable-satellite="enableSatellite"
8 9
      :enable-traffic="enableTraffic" @markertap="onmarkertap" @callouttap="oncallouttap" @controltap="oncontroltap"
      @regionchange="onregionchange" @tap="maptap" @updated="onupdated" @poitap="onpoitap"></map>
10
    <scroll-view class="scrollview" scroll-y="true">
11
      <view class="tips">注意:需要正确配置地图服务商的Key才能正常显示地图组件</view>
Anne_LXM's avatar
Anne_LXM 已提交
12 13 14
      <view class="uni-title">
        <text class="uni-title-text">属性示例</text>
      </View>
15 16
      <input-data defaultValue="13" title="scale: 缩放级别,取值范围为5-18" type="number"
        @confirm="confirm_scale_input"></input-data>
Anne_LXM's avatar
Anne_LXM 已提交
17
      <boolean-data :defaultValue="showLocation" title="开启显示带有方向的当前定位点" @change="change_show_location"></boolean-data>
18

Anne_LXM's avatar
Anne_LXM 已提交
19 20
      <button class="button" @click="addControls">控件</button>
      <button class="button" @click="addMarkers">标记点</button>
Anne_LXM's avatar
Anne_LXM 已提交
21
      <button class="button" @click="addMarkersLabel">为标记点旁边增加标签</button>
Anne_LXM's avatar
Anne_LXM 已提交
22 23 24 25 26 27 28 29 30
      <button class="button" @click="addPolyline">路线</button>
      <button class="button" @click="addPolygons">多边形</button>
      <button class="button" @click="addCircles">圆</button>
      <button class="button" @click="includePoint">缩放视野以包含所有给定的坐标点</button>
      <view class="uni-title">
        <text class="uni-title-text">方法示例</text>
      </View>
      <button class="button" @click="handleGetCenterLocation">获取当前地图中心的经纬度</button>
      <button class="button" @click="handleGetRegion">获取当前地图的视野范围</button>
Anne_LXM's avatar
Anne_LXM 已提交
31 32 33 34
      <button class="button" @click="handleTranslateMarker">平移marker</button>
      <button class="button" @click="handleMoveToLocation">将地图中心移动到当前定位点</button>
      <button class="button" @click="handleGetScale">获取当前地图的缩放级别</button>

35 36
    </scroll-view>
  </view>
Anne_LXM's avatar
Anne_LXM 已提交
37
</template>
38

Anne_LXM's avatar
Anne_LXM 已提交
39
<script setup lang="uts">
40
  type Anchor = {
41 42
    x: number,
    y: number
43 44 45
  }

  type Callout = {
46 47 48 49 50 51 52 53 54
    content: string,
    color: string,
    fontSize: number,
    borderRadius: number,
    borderWidth: number,
    borderColor: string,
    bgColor: string,
    padding: number,
    display: string
55 56
  }

Anne_LXM's avatar
Anne_LXM 已提交
57
  type Label = {
58 59 60 61 62
    content: string,
    color: string,
    fontSize: number,
    x: number,
    y: number,
Anne_LXM's avatar
Anne_LXM 已提交
63
    borderColor: string
64
    borderWidth: number,
Anne_LXM's avatar
Anne_LXM 已提交
65
    borderRadius: number,
66 67
    bgColor: string,
    padding: number
Anne_LXM's avatar
Anne_LXM 已提交
68 69
  }

70
  type Markers = {
71 72 73 74 75 76 77 78 79 80 81 82
    id: number,
    latitude: number,
    longitude: number,
    title?: string,
    iconPath: string,
    zIndex?: string,
    rotate?: number,
    width?: number,
    height?: number,
    label?: Label,
    anchor?: Anchor,
    callout?: Callout
83 84 85
  }

  type Points = {
86 87
    latitude: number,
    longitude: number
88 89 90
  }

  type Polyline = {
91 92 93 94 95 96 97
    points: Points[],
    color: string,
    width: number,
    dottedLine: boolean,
    arrowLine: boolean,
    borderColor: string,
    borderWidth: number
98 99 100
  }

  type Polygons = {
101 102 103 104 105
    points: Points[];
    fillColor: string;
    strokeWidth: number;
    strokeColor: string;
    zIndex: number;
106 107 108
  }

  type Circles = {
109 110 111 112 113 114
    latitude: number;
    longitude: number;
    radius: number;
    strokeWidth: number;
    color: string;
    fillColor: string;
115 116
  }

Anne_LXM's avatar
Anne_LXM 已提交
117
  type PositionType = {
118 119 120 121
    left: number,
    top: number,
    width: number,
    height: number
Anne_LXM's avatar
Anne_LXM 已提交
122 123 124
  }

  type ControlsType = {
125 126 127 128
    id?: number;
    position: PositionType;
    iconPath: string;
    clickable?: boolean;
Anne_LXM's avatar
Anne_LXM 已提交
129 130
  }

Anne_LXM's avatar
Anne_LXM 已提交
131
  type TypeJestResult = {
132 133
    translateMarkerMsg: string,
    animationEnd: boolean,
Anne_LXM's avatar
Anne_LXM 已提交
134 135 136
    centerPoints: Points,
    southwest: Points,
    northeast: Points,
137 138
    moveToLocationMsg: string,
    scale: number
Anne_LXM's avatar
Anne_LXM 已提交
139 140
  }

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  const testMarkers = [{
    id: 0,
    latitude: 39.989631,
    longitude: 116.481018,
    title: '方恒国际 阜通东大街6号',
    zIndex: '1',
    iconPath: '../../../static/location.png',
    rotate: 0,
    width: 20,
    height: 20,
    anchor: {
      x: 0.5,
      y: 1
    },
    callout: {
      content: '方恒国际 阜通东大街6号',
      color: '#00BFFF',
      fontSize: 10,
      borderRadius: 4,
      borderWidth: 1,
      borderColor: '#333300',
      bgColor: '#CCFF99',
Anne_LXM's avatar
Anne_LXM 已提交
163
      padding: 5,
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      display: 'ALWAYS'
    }
  },
  {
    id: 1,
    latitude: 39.9086920000,
    longitude: 116.3974770000,
    title: '天安门',
    zIndex: '1',
    iconPath: '../../../static/location.png',
    width: 40,
    height: 40,
    anchor: {
      x: 0.5,
      y: 1
    },
    callout: {
      content: '首都北京\n天安门',
      color: '#00BFFF',
      fontSize: 12,
184
      borderRadius: 10,
Anne_LXM's avatar
Anne_LXM 已提交
185
      borderWidth: 2,
186 187
      borderColor: '#333300',
      bgColor: '#CCFF11',
Anne_LXM's avatar
Anne_LXM 已提交
188
      padding: 5,
189 190 191 192 193
      display: 'ALWAYS'
    }
  }
  ];

Anne_LXM's avatar
Anne_LXM 已提交
194

195 196 197 198 199 200 201 202 203 204
  const testPolyline = [{
    points: [{
      latitude: 39.925539,
      longitude: 116.279037
    },
    {
      latitude: 39.925539,
      longitude: 116.520285
    }],
    color: '#FFCCFF',
205
    width: 7,
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    dottedLine: true,
    arrowLine: true,
    borderColor: '#000000',
    borderWidth: 2
  },
  {
    points: [{
      latitude: 39.938698,
      longitude: 116.275177
    },
    {
      latitude: 39.966069,
      longitude: 116.289253
    },
    {
      latitude: 39.944226,
      longitude: 116.306076
    },
    {
      latitude: 39.966069,
      longitude: 116.322899
    },
    {
      latitude: 39.938698,
      longitude: 116.336975
    }],
    color: '#CCFFFF',
    width: 5,
Anne_LXM's avatar
Anne_LXM 已提交
234
    dottedLine: false,
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    arrowLine: true,
    borderColor: '#CC0000',
    borderWidth: 3
  }
  ];

  const testPolygons = [{
    points: [{
      latitude: 39.781892,
      longitude: 116.293413
    },
    {
      latitude: 39.787600,
      longitude: 116.391842
    },
    {
      latitude: 39.733187,
      longitude: 116.417932
    },
    {
      latitude: 39.704653,
      longitude: 116.338255
    }],
    fillColor: '#FFCCFF',
    strokeWidth: 3,
    strokeColor: '#CC99CC',
    zIndex: 11
  },
  {
    points: [{
      latitude: 39.887600,
      longitude: 116.518932
    },
    {
      latitude: 39.781892,
      longitude: 116.518932
    },
    {
      latitude: 39.781892,
      longitude: 116.428932
    },
    {
      latitude: 39.887600,
      longitude: 116.428932
    }
    ],
    fillColor: '#CCFFFF',
    strokeWidth: 5,
    strokeColor: '#CC0000',
    zIndex: 3
  }
  ];

  const testCircles = [{
    latitude: 39.996441,
    longitude: 116.411146,
    radius: 15000,
    strokeWidth: 5,
    color: '#CCFFFF',
    fillColor: '#CC0000'
  },
  {
    latitude: 40.096441,
    longitude: 116.511146,
    radius: 12000,
    strokeWidth: 3,
    color: '#CCFFFF',
    fillColor: '#FFCCFF'
  },
  {
    latitude: 39.896441,
    longitude: 116.311146,
    radius: 9000,
    strokeWidth: 1,
    color: '#CCFFFF',
    fillColor: '#CC0000'
  }
  ];

  const testIncludePoints = [{
    latitude: 39.989631,
    longitude: 116.481018,
  },
  {
    latitude: 39.9086920000,
    longitude: 116.3974770000,
  }
  ];


Anne_LXM's avatar
Anne_LXM 已提交
325
  const map = ref(null as MapContext | null);
Anne_LXM's avatar
Anne_LXM 已提交
326 327 328
  const location = ref({ longitude: 116.39742, latitude: 39.909 });
  const rotate = ref(0);
  const skew = ref(0);
Anne_LXM's avatar
Anne_LXM 已提交
329
  // 自动化测试
Anne_LXM's avatar
Anne_LXM 已提交
330
  const autoTest = ref(false);
Anne_LXM's avatar
Anne_LXM 已提交
331 332 333 334 335 336
  const updateAutoTest = (value: boolean) => {
    autoTest.value = value
  }


  const jestResult = reactive({
337 338
    translateMarkerMsg: "",
    animationEnd: false,
Anne_LXM's avatar
Anne_LXM 已提交
339
    centerPoints: {
340 341
      latitude: 0,
      longitude: 0
Anne_LXM's avatar
Anne_LXM 已提交
342 343
    },
    southwest: {
344 345
      latitude: 0,
      longitude: 0
Anne_LXM's avatar
Anne_LXM 已提交
346 347
    },
    northeast: {
348 349
      latitude: 0,
      longitude: 0
Anne_LXM's avatar
Anne_LXM 已提交
350
    },
351 352
    moveToLocationMsg: "",
    scale: 0,
Anne_LXM's avatar
Anne_LXM 已提交
353 354
  } as TypeJestResult);

355

Anne_LXM's avatar
Anne_LXM 已提交
356 357 358 359
  onReady(() => {
    map.value = uni.createMapContext("map1", getCurrentInstance()!.proxy!)
  });

Anne_LXM's avatar
Anne_LXM 已提交
360 361 362 363 364
  const scale = ref(13);
  const confirm_scale_input = (value: number) => {
    scale.value = value
  };

365
  const controls = ref([] as ControlsType[]);
Anne_LXM's avatar
Anne_LXM 已提交
366
  const addControls = () => {
Anne_LXM's avatar
Anne_LXM 已提交
367
    controls.value.push({
Anne_LXM's avatar
Anne_LXM 已提交
368 369 370 371 372 373
      id: 1,
      position: {
        left: 5,
        top: 180,
        width: 30,
        height: 30
374
      },
Anne_LXM's avatar
Anne_LXM 已提交
375 376
      iconPath: '../../../static/uni.png',
      clickable: true
Anne_LXM's avatar
Anne_LXM 已提交
377
    })
378
  }
Anne_LXM's avatar
Anne_LXM 已提交
379

Anne_LXM's avatar
Anne_LXM 已提交
380
  const showLocation = ref(false);
381
  const change_show_location = (checked: boolean) => {
Anne_LXM's avatar
Anne_LXM 已提交
382 383 384
    showLocation.value = checked
  }

Anne_LXM's avatar
Anne_LXM 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398
  const includePoints = ref([] as Points[]);
  const includePoint = () => {
    includePoints.value = testIncludePoints;
  };


  const markers = reactive([] as Markers[]);
  const addMarkers = () => {
    markers.push(...testMarkers);
  };

  const addMarkersLabel = () => {
    markers.forEach((marker, index) => {
      marker.label = {
399
        content: 'Hello Label' + (index + 1),
Anne_LXM's avatar
Anne_LXM 已提交
400 401 402 403
        color: '#aa00ff',
        fontSize: 12,
        x: 5,
        y: 0,
404 405
        borderColor: '#333300',
        borderWidth: 2,
Anne_LXM's avatar
Anne_LXM 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
        borderRadius: 20,
        bgColor: '#aaffff',
        padding: 10
      };
    });
  };


  const polyline = ref([] as Polyline[]);
  const addPolyline = () => {
    scale.value = 12;
    polyline.value = testPolyline;
  };


  const polygons = ref([] as Polygons[]);
  const addPolygons = () => {
    scale.value = 10;
    polygons.value = testPolygons;
  };


  const circles = ref([] as Circles[]);
  const addCircles = () => {
    scale.value = 10;
    circles.value = testCircles;
  };

  const showCompass = ref(true);
  const enable3D = ref(true);
  const enableOverlooking = ref(true);
  const enableZoom = ref(true);
  const enableScroll = ref(true);
  const enableRotate = ref(true);
  const enableSatellite = ref(false);
  const enableTraffic = ref(false);

443
  const enableThreeD = (e) => {
Anne_LXM's avatar
Anne_LXM 已提交
444 445
    enable3D.value = e.detail.value;
  }
446
  const changeShowCompass = (e) => {
Anne_LXM's avatar
Anne_LXM 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    showCompass.value = e.detail.value;
  }
  const changeEnableOverlooking = (e) => {
    enableOverlooking.value = e.detail.value;
  };

  const changeEnableZoom = (e) => {
    enableZoom.value = e.detail.value;
  };

  const changeEnableScroll = (e) => {
    enableScroll.value = e.detail.value;
  };

  const changeEnableRotate = (e) => {
    enableRotate.value = e.detail.value;
  };

  const changeEnableSatellite = (e) => {
    enableSatellite.value = e.detail.value;
  };

  const changeEnableTraffic = (e) => {
    enableTraffic.value = e.detail.value;
  };


  const handleGetCenterLocation = () => {
    if (map.value) {
      map.value.getCenterLocation({
        success: ret => {
Anne_LXM's avatar
Anne_LXM 已提交
478
          // console.log('getCenterLocation',ret);
Anne_LXM's avatar
Anne_LXM 已提交
479
          jestResult.centerPoints = ret;
480
          if (!autoTest.value) {
Anne_LXM's avatar
Anne_LXM 已提交
481 482 483 484
            uni.showModal({
              content: JSON.stringify(ret)
            });
          }
Anne_LXM's avatar
Anne_LXM 已提交
485 486 487 488 489 490 491 492 493
        }
      });
    }
  };

  const handleGetRegion = () => {
    if (map.value) {
      map.value.getRegion({
        success: ret => {
Anne_LXM's avatar
Anne_LXM 已提交
494
          // console.log('getRegion',JSON.stringify(ret));
Anne_LXM's avatar
Anne_LXM 已提交
495 496
          jestResult.southwest = ret.southwest;
          jestResult.northeast = ret.northeast
497
          if (!autoTest.value) {
Anne_LXM's avatar
Anne_LXM 已提交
498 499 500 501
            uni.showModal({
              content: JSON.stringify(ret)
            });
          }
Anne_LXM's avatar
Anne_LXM 已提交
502 503 504 505 506
        }
      });
    }
  };

Anne_LXM's avatar
Anne_LXM 已提交
507

Anne_LXM's avatar
Anne_LXM 已提交
508 509 510 511 512 513 514 515
  const handleTranslateMarker = () => {
    if (map.value) {
      map.value.translateMarker({
        markerId: 1,
        destination: {
          latitude: 39.989631,
          longitude: 116.481018
        },
516 517
        autoRotate: true,
        rotate: 10,
Anne_LXM's avatar
Anne_LXM 已提交
518 519
        duration: 2000,
        animationEnd: () => {
Anne_LXM's avatar
Anne_LXM 已提交
520 521
          // console.log('动画结束');
          jestResult.animationEnd = true;
Anne_LXM's avatar
Anne_LXM 已提交
522 523
        },
        success: ret => {
Anne_LXM's avatar
Anne_LXM 已提交
524 525
          // console.log('handleTranslateMarker',JSON.stringify(ret));
          jestResult.translateMarkerMsg = ret.errMsg;
Anne_LXM's avatar
Anne_LXM 已提交
526
        },
Anne_LXM's avatar
Anne_LXM 已提交
527 528
        fail: error => {
          console.log(error)
Anne_LXM's avatar
Anne_LXM 已提交
529 530 531 532 533
        }
      });
    }
  };

Anne_LXM's avatar
Anne_LXM 已提交
534 535 536 537 538

  const handleGetScale = () => {
    if (map.value) {
      map.value.getScale({
        success: res => {
539 540 541 542 543 544 545 546
          // console.log('getScale',res);
          scale.value = res.scale
          jestResult.scale = res.scale
          if (!autoTest.value) {
            uni.showModal({
              content: '当前地图的缩放级别为:' + res.scale
            });
          }
Anne_LXM's avatar
Anne_LXM 已提交
547 548
        },
        fail: error => {
549
          console.log(error)
Anne_LXM's avatar
Anne_LXM 已提交
550 551 552 553 554 555 556 557
        },
      });
    }
  };

  const handleMoveToLocation = () => {
    if (map.value) {
      map.value.moveToLocation({
558 559 560 561 562 563 564 565 566
        latitude: 39.909,
        longitude: 116.39742,
        success: res => {
          // console.log('moveToLocation',res);
          jestResult.moveToLocationMsg = res.errMsg;
          if (!autoTest.value) {
            uni.showModal({
              content: JSON.stringify(res)
            });
Anne_LXM's avatar
Anne_LXM 已提交
567
          }
568 569 570 571
        },
        fail: error => {
          console.log(error)
        }
Anne_LXM's avatar
Anne_LXM 已提交
572 573 574 575
      });
    }
  };

576
  const maptap = (e: UniEvent) => {
Anne_LXM's avatar
Anne_LXM 已提交
577
    // console.log('点击地图时触发',e)
Anne_LXM's avatar
Anne_LXM 已提交
578 579 580 581 582
    uni.showModal({
      content: JSON.stringify(e)
    });
  };

583
  const onmarkertap = (e: UniEvent) => {
Anne_LXM's avatar
Anne_LXM 已提交
584
    // console.log('点击标记点时触发',e)
Anne_LXM's avatar
Anne_LXM 已提交
585 586 587 588 589
    uni.showModal({
      content: JSON.stringify(e)
    });
  };

590
  const oncontroltap = (e: UniEvent) => {
Anne_LXM's avatar
Anne_LXM 已提交
591
    // console.log('点击控件时触发',e)
Anne_LXM's avatar
Anne_LXM 已提交
592 593 594 595 596
    uni.showModal({
      content: JSON.stringify(e)
    });
  };

597
  const oncallouttap = (e: UniEvent) => {
Anne_LXM's avatar
Anne_LXM 已提交
598
    // console.log('点击标记点对应的气泡时触发',e)
Anne_LXM's avatar
Anne_LXM 已提交
599 600 601 602 603
    uni.showModal({
      content: JSON.stringify(e)
    });
  };

604 605
  const onupdated = (e: UniEvent) => {
    console.log('在地图渲染更新完成时触发', e)
Anne_LXM's avatar
Anne_LXM 已提交
606 607
  };

608 609
  const onregionchange = (e: UniEvent) => {
    console.log('视野发生变化时触发', e)
Anne_LXM's avatar
Anne_LXM 已提交
610 611
  };

612
  const onpoitap = (e: UniEvent) => {
Anne_LXM's avatar
Anne_LXM 已提交
613
    // console.log('点击地图poi点时触发',e)
Anne_LXM's avatar
Anne_LXM 已提交
614 615 616 617 618
    uni.showModal({
      content: JSON.stringify(e)
    });
  };

Anne_LXM's avatar
Anne_LXM 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
  defineExpose({
    jestResult,
    autoTest,
    updateAutoTest,
    addControls,
    addMarkers,
    addMarkersLabel,
    addPolyline,
    addPolygons,
    addCircles,
    includePoint,
    handleGetCenterLocation,
    handleGetRegion,
    handleTranslateMarker,
    handleMoveToLocation,
    handleGetScale
  })
Anne_LXM's avatar
Anne_LXM 已提交
636
</script>
637

Anne_LXM's avatar
Anne_LXM 已提交
638
<style>
639 640 641 642 643 644
  .content {
    flex: 1;
  }

  .map {
    width: 100%;
Anne_LXM's avatar
Anne_LXM 已提交
645
    height: 300px;
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
    background-color: #f0f0f0;
  }

  .scrollview {
    flex: 1;
    padding: 10px;
  }

  .list-item {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    padding: 5px 0px;
  }

  .list-text {
    flex: 1;
  }

  .button {
    margin-top: 5px;
    margin-bottom: 5px;
  }
669 670 671 672 673 674

  .tips {
    font-size: 12px;
    margin-top: 15px;
    opacity: .8;
  }
Anne_LXM's avatar
Anne_LXM 已提交
675
</style>