uniapp 自定义map组件

该博客介绍了如何在uni-app中自定义地图组件,包括引用高德地图API,申请key,实现城市选择功能,并提及需根据项目需求调整graceui引用。尽管uniapp SDK已更新,但组件优化未完善,仅供学习参考。
摘要由CSDN通过智能技术生成

uniapp 自定义map组件
1、地图附近相关数据引用高德
2、key自己申请
3、附带城市选择
4、项目引入的是graceui,请自行修改
5、调用相关自行处理
注意:直接引用不可用,uniapp sdk升级后中心点和maekers相关只改为可用,没做优化 仅限参考、仅限参考、仅限参考
在这里插入图片描述

addressChoose.vue

<template>
    <aPage :footerHeight="0">
        <!-- 页面头部 -->
        <view slot="gHeader" class="grace-header-body" style="background-color: #fff;">
            <text class="grace-header-icons grace-icons icon-back grace-block" @tap="closeChoose"></text>
            <view class="grace-header-content"><view style="width:100%;text-align: center;">选择位置</view></view>
            <text class="mapSure" @tap="sureMap" style="margin-right: 20rpx;">完成</text>
        </view>
        <!-- 页面主体 -->
        <view slot="gBody" style="position: relative;" class="grace-bg-body">
            <!-- 页面主体 -->
            <view class="grace-body gracedy" style="position: relative; ">
                <view class="serchCont">
                    <view class="searchbox">
                        <view class="cityName" @tap="shouwCity(true)">
                            <text class="city">{
   {
    city }}</text>
                            <text class="icon-arrow-down grace-header-icons grace-icons grace-gray seliocn"></text>
                        </view>
                        <view class="shu">|</view>
                        <view class=""><graceSearch @inputting="onKeyInput" @clear="clearText" @confirm="onKeyInput" :kwd="serarchText" placeholder="搜索地点"></graceSearch></view>
                    </view>
                </view>
                <view class="" style="width: 100%; height: 600rpx;overflow: hidden;" v-show="!mapHiden" @touchmove.stop="">
                    <!-- #ifdef APP-PLUS -->
                    <map
                        id="map1"
                        ref="map1"
                        style="width: 100%; height: 600rpx;"
                        :latitude="latitude"
                        :longitude="longitude"
                        @regionchange="selectMap"
                        show-location
                        :scale="16"
                    ></map>
                    <!-- #endif -->
                    <!-- #ifdef H5 -->
                    <map
                        id="map1"
                        ref="map1"
                        style="width: 100%; height: 600rpx;"
                        :markers="covers"
                        :latitude="latitude"
                        :longitude="longitude"
                        @regionchange="selectMap"
                        show-location
                        :scale="15"
                    ></map>
                    <!-- #endif -->
                </view>
                <view class="">
                    <view class="tmapList" :class="mapHiden ? 'tmapList1' : ''" v-if="!mapHiden">
                        <view class="mapSing" v-for="(item, index) in mapResult" :key="index" @tap="selectAddress(item)">
                            <view class="mapleflt">
                                <view class="weiz">{
   {
    index == 0 && !mapHiden && !showads ? '我的位置' : item.name }}</view>
                                <view class="adress">{
   {
    typeof item.address == 'string' ? item.address : '' }}</view>
                            </view>
                            <view class="mapright"><image src="/static/img/map/currentPos.png" mode="" v-if="item.check"></image></view>
                        </view>
                    </view>
                    <view class="tmapList" :class="mapHiden ? 'tmapList1' : ''" v-else>
                        <view class="mapSing" v-for="(item, index) in keyResult" :key="index" @tap="keySelect(item)">
                            <view class="mapleflt">
                                <view class="weiz">{
   {
    item.name }}</view>
                                <view class="adress">{
   {
    typeof item.address == 'string' ? item.address : '' }}</view>
                            </view>
                            <view class="mapright"><image src="/static/img/map/currentPos.png" mode="" v-if="item.check"></image></view>
                        </view>
                    </view>
                </view>
            </view>

            <selectCity v-show="showModule" ref="selectCity" @chooseCity="chooseCity" style="position: fixed;top: 0;left: 0;z-index: 9999999;background-color: #fff;"></selectCity>
        </view>
    </aPage>
</template>

<script>
import selectCity from '@/components/selectCity.vue';
export default {
   
    data() {
   
        return {
   
          key: '',
          cities: [],
	      addressName: "",
	      weather: {
   
	        hasData: false,
	        data: [],
	      },
	      title: "map",
	      address: {
   },
	      latitude: 31.972952,
	      longitude: 118.78025,
	      searchNeary: 4000,
	      covers: [
	        {
   
	          width: 20,
	          height: 20,
	          latitude: 31.972952,
	          longitude: 118.78025,
	          iconPath: "/static/images/common/location.png",
	          address: "",
	          name: "",
	        },
	      ],
	      icon: "https://p-yy-yjy.oss-cn-shanghai.aliyuncs.com/static/images/20220106/1141641459414_.pic.jpg",
	      map: null,
	      mapCtx: null,
	      keyResult: [],
	      mapResult: [],
	      mapHiden: false,
	      inputClearValue: "",
	      showClearIcon: false,
	      serarchText: "",
	      keyUser: {
   },
	      shoeLoding: false,
	      showModule: false,
	      mapCtx: {
   },
	      city: "南京市",
	      showads: false,
	      tindex: 0,
	      searchKey: "",
	      selectKey: {
   },
	      haveData: false,
        };
    },
   components: {
   
    selectCity,
  },
  onReady() {
   
    console.log("onReady");
    // #ifdef APP-PLUS
    this.newMap();
    // #endif
  },
  onLoad: function (e) {
   
    this._select = e.s;
    // if (this._select) {
   
    //   this.obtainAddres(this._select);
    // }
    this.obtainAddres(this._select);

    this.haveData = true;
  },
  onShow() {
   },
  methods: {
   
    loadScrpit(address, bol) {
   
      console.log("addressChoose loadScrpit");
      let self = this;
      var map = new AMap.Map("map1", {
   
        enableHighAccuracy: true, //是否使用高精度定位,默认:true
        zoomToAccuracy: true,
        zoom: 15,
        scrollWheel: false,
        dragEnable: true, //是否可拖动
        resizeEnable: true,
        rotateEnable: true,
        buttonPosition: "RB",
        buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
        center:
          address && address.lng
            ? [address.lng, address.lat]
            : [118.78025, 31.972952], //初始化地图中心点
      });
      self.map = map;
      console.log("addressChoose map init ");
      if ("https:" == location.protocol) {
   
        map.plugin(["AMap.Geolocation"], function () {
   
          let geolocation = new AMap.Geolocation({
   
            enableHighAccuracy: true, //  是否使用高精度定位,默认:true
            timeout: 10000, //  超过10秒后停止定位,默认:无穷大
            maximumAge: 0, // 定位结果缓存0毫秒,默认:0
            convert: true, // 自动偏移坐标,偏移后的坐标为高德坐标,默认:true
            showButton: true, //  显示定位按钮,默认:true
            buttonPosition: "LB", // 定位按钮停靠位置,默认:'LB',左下角
            buttonOffset: new AMap.Pixel(10, 20), //  定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
            showMarker: true, //  定位成功后在定位到的位置显示点标记,默认:true
            showCircle: true, //  定位成功后用圆圈表示定位精度范围,默认:true
            panToLocation: true, //  定位成功后将定位到的位置作为地图中心点,默认:true
            zoomToAccuracy: true, //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
          });
          // console.log("addressChoose map Geolocation ");

          map.addControl(geolocation);
          // geolocation.getCurrentPosition()
          AMap.event.addListener(geolocation, "complete", (res) => {
   
            console.log(
              "addressChoose map Geolocation complete ",
              JSON.stringify(res)
            );
            if 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给钱,谢谢!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值