Taro and Map

怼烎@ 2022-12-19 06:03 525阅读 0赞

我太累了,懒得加注释了

页面上一共三个按钮,实现了三个功能:
【1.打开微信手机地图选位置】
watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzQyMzM4OTYy_size_1_color_FFFFFF_t_70_pic_left
【2.展示一个点】
watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzQyMzM4OTYy_size_16_color_FFFFFF_t_70_pic_left
【3.展示很多点Markers】
在这里插入图片描述

  1. import React, { useState, useEffect } from "react";
  2. import { View, Button, Text, Map } from "@tarojs/components";
  3. import { AtModal, AtModalHeader, AtModalContent, AtModalAction } from "taro-ui";
  4. import Taro from "@tarojs/taro";
  5. import QQMapWX from "../utils/mapsdk/qqmap-wx-jssdk";
  6. export default function({ isOpened, changeOpen }) {
  7. const [adressInfo, setArdessInfo] = useState({ });
  8. const [markers, setMarkers] = useState([]);
  9. function showMap() {
  10. Taro.chooseLocation({
  11. latitude: adressInfo["latitude"] || undefined,
  12. longitude: adressInfo["longitude"] || undefined,
  13. success(res) {
  14. setArdessInfo(res);
  15. }
  16. });
  17. }
  18. function showMap2() {
  19. if (adressInfo["latitude"] && adressInfo["longitude"]) {
  20. Taro.openLocation({
  21. latitude: adressInfo["latitude"] || undefined,
  22. longitude: adressInfo["longitude"] || undefined,
  23. address: adressInfo["address"] || "",
  24. name: adressInfo["name"] || "",
  25. scale: 18 //缩放比例,范围5~18
  26. // complete 接口调用结束的回调函数(调用成功、失败都会执行)
  27. // fail 接口调用失败的回调函数
  28. // success 接口调用成功的回调函数
  29. });
  30. }
  31. }
  32. function showMap3() {
  33. new QQMapWX({
  34. key: "AXNBZ-HDOLW-4ZCRO-OHASD-SKPVV-DYBO4"
  35. }).search({
  36. keyword: "酒店",
  37. success: function(res) {
  38. let mks = res.data.map(element => {
  39. return {
  40. title: element.title,
  41. id: element.id,
  42. latitude: element.location.lat,
  43. longitude: element.location.lng,
  44. iconPath: "https://csdnimg.cn/medal/qixiebiaobing4@240.png", //图片路径
  45. width: 20,
  46. height: 20
  47. };
  48. });
  49. setMarkers(mks);
  50. }
  51. });
  52. }
  53. function onTap(e) {
  54. console.log("点击地图时触发", e);
  55. }
  56. function onMarkerTap(e) {
  57. console.log(
  58. "点击标记点时触发",
  59. e,
  60. "可以拿到坐标,然后问他到这里去,再掉,打开的接口,完美"
  61. );
  62. }
  63. function onLabelTap() {
  64. console.log("点击label时触发");
  65. }
  66. function onControlTap() {
  67. console.log("点击控件时触发");
  68. }
  69. function onCalloutTap() {
  70. console.log("点击标记点对应的气泡时触发");
  71. }
  72. function onUpdated() {
  73. console.log("在地图渲染更新完成时触发");
  74. }
  75. function onRegionChange() {
  76. console.log("视野发生变化时触发");
  77. }
  78. function onPoiTap() {
  79. console.log("点击地图poi点时触发");
  80. }
  81. return (
  82. <View>
  83. <AtModal
  84. isOpened={ isOpened}
  85. onClose={ () => {
  86. changeOpen();
  87. }}
  88. >
  89. <AtModalHeader>地图</AtModalHeader>
  90. <AtModalContent>
  91. 拿到地图信息了吗:
  92. <View>名称:{ adressInfo["name"]}</View>
  93. <View>地址:{ adressInfo["address"]}</View>
  94. <View>
  95. 经纬度:{ adressInfo["latitude"]}、{ adressInfo["longitude"]}
  96. </View>
  97. <Button onClick={ () => showMap()}>选择位置</Button>
  98. <Button onClick={ () => showMap2()}>打开地图</Button>
  99. <Button onClick={ () => showMap3()}>展开好多点</Button>
  100. <Map
  101. subkey="AXNBZ-HDOLW-4ZCRO-OHASD-SKPVV-DYBO4"
  102. latitude={ 31.222349}
  103. longitude={ 121.457743}
  104. include-points={ markers}
  105. markers={ markers}
  106. scale={ 18}
  107. show-location
  108. onTap={ onTap}
  109. onMarkerTap={ onMarkerTap}
  110. onLabelTap={ onLabelTap}
  111. onControlTap={ onControlTap}
  112. onCalloutTap={ onCalloutTap}
  113. onUpdated={ onUpdated}
  114. onRegionChange={ onRegionChange}
  115. onPoiTap={ onPoiTap}
  116. />
  117. { /* 基础展示 ↓ */}
  118. { /* <Map subkey="AXNBZ-HDOLW-4ZCRO-OHASD-SKPVV-DYBO4" latitude={31.222349} longitude={121.457743} include-points={markers} markers={markers} scale={18} show-location /> */}
  119. </AtModalContent>
  120. <AtModalAction>
  121. { /* <Button>取消</Button> <Button>确定</Button> */}
  122. </AtModalAction>
  123. </AtModal>
  124. </View>
  125. );
  126. }

发表评论

表情:
评论列表 (有 0 条评论,525人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Taro and Map

    我太累了,懒得加注释了 页面上一共三个按钮,实现了三个功能: 【1.打开微信手机地图选位置】 ![watermark_type_ZmFuZ3poZW5naG