首页 > 其他分享 >移动端封装map 组件,调起高德地图

移动端封装map 组件,调起高德地图

时间:2024-05-24 16:10:19浏览次数:21  
标签:map console log point longitude maps latitude 调起 高德

封装的map.vue

<template>
    <view>
        <map id="popMap" ref="maps" :longitude="longitude" :latitude="latitude" scale="14" :markers="markers"
            @tap="onMapTap" @click="onMapClick" @regionchange="onRegionChange" show-location
            style="width: 750rpx; height: 100vh;"></map>
    </view>
</template>

<script>
    import amapPlugin from "@amap/amap-jsapi-loader";
    export default {
        data() {
            return {
                // 初始化地图的位置
                longitude: 118.020013,
                latitude: 37.461536,
                markers: [ // 设置标记点
                    {
                        latitude: 118.020013,
                        longitude: 37.461536,
                        title: 'New York City',
                        iconPath: '/static/mapLocation.png',
                    },
                ],
            };
        },
        onReady() {
            // 高德地图API初始化
            this.initAmap();
            this.addMapEvent()
        },
        mounted() {
            // this.addMapEvent()
        },
        methods: {
            initAmap() {
                console.log('111');
                // 高德地图API初始化代码,需要自行获取key
                amapPlugin.initAMapApi('13254965e7ab4c469911c1256a2b129e');
            },
            onRegionChange(e) {
                // 当地图区域发生变化时,更新位置
                return
                if (e.type === 'end') {
                    console.log(e);
                    this.longitude = e.detail.centerLocation.longitude;
                    this.latitude = e.detail.centerLocation.latitude;
                    this.markers[0].longitude = this.longitude
                    this.markers[0].latitude = this.latitude
                    // 使用高德地图API进行逆地理编码查询地址信息
                    this.reverseGeocode()
                    // uni.getLocation({
                    //     longitude: this.longitude,
                    //     latitude: this.latitude,
                    //     success: (res) => {
                    //         console.log(res, '成功');
                    //         this.reverseGeocode()
                    //         // 调用高德API进行逆地理编码
                    //         // amapPlugin.reverseGeocode({
                    //         //     location: `${this.longitude},${this.latitude}`,
                    //         //     success: (data) => {
                    //         //         // 成功获取地址信息
                    //         //         console.log(data.address);
                    //         //     },
                    //         //     fail: () => {
                    //         //         // 获取地址失败处理
                    //         //     }
                    //         // });
                    //     }
                    // });
                }
            },
            onMapTap(e) {
                console.log('地图被点击了', e)
                let that = this;
                console.log(this.$refs.maps, 'maps')
                // var maps = uni.createMapContext(e.target.id, this);
                // console.log(maps, 'maps');
                // console.log(maps.pageVm, '0000')

                // this.$refs.maps.onclick = function(point) {
                //     console.log(point, 'point')
                // }
            },
            onMapClick(e) {
                console.log(e, 'click点击');
            },
            reverseGeocode(e) {
                const {
                    longitude,
                    latitude
                } = this.markers[0];
                const key = '13254965e7ab4c469911c1256a2b129e'; // 替换为你的高德API密钥
                const url = `https://restapi.amap.com/v3/geocode/regeo?key=${key}&location=${longitude},${latitude}`;

                uni.request({
                    url,
                    success: ({
                        data
                    }) => {
                        if (data.status === '1') {
                            console.log(data, '反显成功');
                            // uni.showModal({
                            //     title: '地址信息',
                            //     content: data.regeocode.formatted_address,
                            //     showCancel: false
                            // });
                        } else {
                            uni.showToast({
                                title: '逆地理编码失败',
                                icon: 'none'
                            });
                        }
                    },
                    fail: () => {
                        uni.showToast({
                            title: '请求失败',
                            icon: 'none'
                        });
                    }
                });
            },
            addMapEvent() {
                console.log('点击时间', uni.createMapContext);
                let that = this;
                var maps = this.$refs.maps //uni.createMapContext("popMap", this).$getAppMap();
                console.log(maps, 'maps');
                maps.onclick = function(point) {
                    console.log(point, "point")
                    that.longitude = point.longitude
                    that.latitude = point.latitude
                    that.covers = [];
                    that.covers = [{
                        id: 2, //点击时候是第二次作为标记点定义。id为2,不能重复
                        latitude: point.latitude,
                        longitude: point.longitude,
                        iconPath: img,
                        fontSize: 80,
                        title: "所选位置"
                    }]
                }
            }
        }
        // //根据地址获取经纬度
        // geoApi = (address) => axios(
        //     'http://restapi.amap.com/v3/geocode/geo',
        //     'GET', {
        //         address,
        //         key
        //     }
        // )

    };
</script>
View Code

使用时,直接引入组件,该传参传参,该接收接收

标签:map,console,log,point,longitude,maps,latitude,调起,高德
From: https://www.cnblogs.com/qinyuanchun/p/18211150

相关文章

  • mapper,service,controller,entity之间的关系
    目录一、学到了二、错误与纠正一、学到了1.mapper(repository):数据访问层,负责与数据库进行交互,执行数据库的操作。定义了各种数据库操作方法的接口,并由ORM框架自动实现主要职能是执行数据库的增删改查,并将数据库操作结果返回给上层的service。2.service:业务逻辑层,处理逻......
  • 【Python】数据分析 Section 6.4: Heatmaps | from Coursera “Applied Data Science
    Heatmapsareawaytovisualizethree-dimensionsofdataandtotakeadvantageofspatialproximityofthosedimensions.InmakingrevisionstothiscourseIwasreallytemptedtogetridofthesectiononheatmaps,asI'veseenenoughbadheatmapst......
  • CF Round946 (Div. 3)C:map的map<pair<int,int>int>映射 + 性质
    BeautifulTriplePairs题目描述Polycarpwasgivenanarray$a$of$n$integers.Hereallylikestriplesofnumbers,soforeach$j$($1\lej\len-2$)hewrotedownatripleofelements$[a_j,a_{j+1},a_{j+2}]$.Polycarpconsidersapa......
  • Kubernetes-ConfigMap详解
    简介:一、ComfigMap的创建1.使用目录创建2.使用文件创建3.使用命令行创建二、Pod中使用ConfigMap1.使用ConfigMap代替环境变量2.使用ConfigMap设置命令行参数3.使用ConfigMap用做数据卷插件三、ConfigMap的热更新简介:ConfigMap功能在Kubernetes1.2版本中引入,许......
  • 在Flink中jackson-databind包下的ObjectMapper处理大写字段问题
    需要加上配置,不然解析会失败,产生一个空对象objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES,true);//忽略大小写代码:publicclassStreamingJob{publicstaticvoidmain(String[]args)throwsException{finalLoggerlogger......
  • 3/24MapReduce面试必看
    本质上是三个进程运行,一个maptask一个reducetask 一个MR程序写程序 添加依赖后,mapperreducer driveryarn集群的配置为了实现数据落盘和网络传输还要进行序列化和反序列化,本质就是将各个结构体里的基本数据类型一一传递 实现writable接口顺序要一致输入和输出基本......
  • HttpURLConnection 调用soap 并且使用Dom4j解析多层级XML为Map对象
    1.引入dom4j的maven依赖包<dependency><groupId>org.dom4j</groupId><artifactId>dom4j</artifactId><version>2.1.4</version></dependency>2.转map方法1importjava.io.BufferedReader;2importjava.io.InputStrea......
  • 关于vue-baidu-map的一些记录
    这一阶段主要的内容是负责编写和百度地图相关的信息。(写到我想吐)仿照导航的页面效果。1.使用说明这里使用的是vue-baidu-map相关组件,这里我就不去说明如何去安装他们了,我们直接向下看。vue-baidu-map的操作手册的网址:VueBaiduMap(dafrok.github.io)。当然我这里介绍的那些只......
  • kimi- MarkMap 生成思维导图
    1、Prompt:帮我分析《被人讨厌的勇气》这本书,从里面总结出核心内容,要求:1.提供5个主要观点2.每个观点至少有3个支撑观点说明3.按照以下格式,使用markdown的代码快格式输出:```#被人讨厌的勇气##<观点1>-<支撑观点1>-<支撑观点2>-<支撑观点3>##<观点2>-<支撑......
  • 高德地图安卓sdk,在uniapp中实现,地图上多个坐标点,点击坐标点,显示坐标信息
     <template><viewclass="content"><mapid="map":style="{width:'100%',height:'50vh'}":markers="markers":longitude="longitude":latitude=......