vue中使用高德地图

1.安装

cnpm install vue-amap --save

2.main.js 中引入

import VueAMap from ‘vue-amap‘
Vue.use(VueAMap)
VueAMap.initAMapApiLoader({
  key: ‘3ebdb3c7a684a4e64f39ddd306056522‘,
  plugin: [‘AMap.Scale‘, ‘AMap.OverView‘, ‘AMap.ToolBar‘, ‘AMap.MapType‘, ‘AMap.PlaceSearch‘, ‘AMap.Geolocation‘, ‘AMap.Geocoder‘],
  v: ‘1.4.4‘,
  uiVersion: ‘1.0‘})
‘‘‘
key: 高德的key(自己申请)

plugin: 引用的插件(根据需要引入)

v: 高德SDK 版本

uiVersion:UI库 版本
‘‘‘

3.在map.vue中引用

<template>
    <div class="container">
        <div class="search-box">
            <input
                    v-model="searchKey"
                    type="search"
                    id="search">
            <button @click="searchByHand">搜索</button>
            <div class="tip-box" id="searchTip"></div>
        </div>
        <!--
          amap-manager: 地图管理对象
          vid:地图容器节点的ID
          zooms: 地图显示的缩放级别范围,在PC上,默认范围[3,18],取值范围[3-18];在移动设备上,默认范围[3-19],取值范围[3-19]
          center: 地图中心点坐标值
          plugin:地图使用的插件
          events: 事件
        -->
        <el-amap class="amap-box"
                 :amap-manager="amapManager"
                 :vid="‘amap-vue‘"
                 :zoom="zoom"
                 :plugin="plugin"
                 :center="center"
                 :events="events"
        >
            <!-- 标记 -->
            <el-amap-marker v-for="(marker, index) in markers" :position="marker" :key="index"></el-amap-marker>
        </el-amap>
    </div>

</template>

<style>

    .container {
        width: 700px;
        height: 500px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate3d(-50%, -50%, 0);
        border: 1px solid #999;
    }

    .search-box {
        position: absolute;
        z-index: 5;
        width: 70%;
        left: 13%;
        top: 10px;
        height: 30px;
    }

    .search-box input {
        float: left;
        width: 80%;
        height: 100%;
        border: 1px solid #30ccc1;
        padding: 0 8px;
        outline: none;
    }

    .search-box button {
        float: left;
        width: 20%;
        height: 100%;
        background: #30ccc1;
        border: 1px solid #30ccc1;
        color: #fff;
        outline: none;
    }

    .tip-box {
        width: 100%;
        max-height: 260px;
        position: absolute;
        top: 30px;
        overflow-y: auto;
        background-color: #fff;
    }
</style>

<script>

    import {AMapManager, lazyAMapApiLoaderInstance} from ‘vue-amap‘

    let amapManager = new AMapManager()
    export default {
        data() {
            let self = this
            return {
                address: null,
                searchKey: ‘‘,
                amapManager,
                markers: [],
                searchOption: {
                    city: ‘全国‘,
                    citylimit: true
                },
                center: [121.329402, 31.228667],
                zoom: 17,
                lng: 0,
                lat: 0,
                loaded: false,
                events: {
                    init() {
                        lazyAMapApiLoaderInstance.load().then(() => {
                            self.initSearch()
                        })
                    },
                    // 点击获取地址的数据
                    click(e) {
                        // console.log(e)
                        self.markers = []
                        let {lng, lat} = e.lnglat
                        self.lng = lng
                        self.lat = lat
                        self.center = [lng, lat]
                        self.markers.push([lng, lat])
                        // 这里通过高德 SDK 完成。
                        let geocoder = new AMap.Geocoder({
                            radius: 1000,
                            extensions: ‘all‘
                        })
                        geocoder.getAddress([lng, lat], function (status, result) {
                            if (status === ‘complete‘ && result.info === ‘OK‘) {
                                if (result && result.regeocode) {
                                    console.log(result.regeocode.formattedAddress)
                                    self.address = result.regeocode.formattedAddress
                                    self.searchKey = result.regeocode.formattedAddress
                                    self.$nextTick()
                                }
                            }
                        })
                    }
                },
                // 一些工具插件
                plugin: [
                    // {
                    //   pName: ‘Geocoder‘,
                    //   events: {
                    //     init (o) {
                    //       console.log(o.getAddress())
                    //     }
                    //   }
                    // },
                    {
                        // 定位
                        pName: ‘Geolocation‘,
                        events: {
                            init(o) {
                                // o是高德地图定位插件实例
                                o.getCurrentPosition((status, result) => {
                                    if (result && result.position) {
                                        // 设置经度
                                        self.lng = result.position.lng
                                        // 设置维度
                                        self.lat = result.position.lat
                                        // 设置坐标
                                        self.center = [self.lng, self.lat]
                                        self.markers.push([self.lng, self.lat])
                                        // load
                                        self.loaded = true
                                        // 页面渲染好后
                                        self.$nextTick()
                                    }
                                })
                            }
                        }
                    },
                    {
                        // 工具栏
                        pName: ‘ToolBar‘,
                        events: {
                            init(instance) {
                                console.log(instance);
                            }
                        }
                    },
                    {
                        // 鹰眼
                        pName: ‘OverView‘,
                        events: {
                            init(instance) {
                                console.log(instance);
                            }
                        }
                    },
                    {
                        // 地图类型
                        pName: ‘MapType‘,
                        defaultType: 0,
                        events: {
                            init(instance) {
                                console.log(instance);
                            }
                        }
                    },
                    {
                        // 搜索
                        pName: ‘PlaceSearch‘,
                        events: {
                            init(instance) {
                                console.log(instance)
                            }
                        }
                    }
                ]
            }
        },
        methods: {
            initSearch() {
                let vm = this
                let map = this.amapManager.getMap()
                AMapUI.loadUI([‘misc/PoiPicker‘], function (PoiPicker) {
                    var poiPicker = new PoiPicker({
                        input: ‘search‘,
                        placeSearchOptions: {
                            map: map,
                            pageSize: 10
                        },
                        suggestContainer: ‘searchTip‘,
                        searchResultsContainer: ‘searchTip‘
                    })
                    vm.poiPicker = poiPicker
                    // 监听poi选中信息
                    poiPicker.on(‘poiPicked‘, function (poiResult) {
                        // console.log(poiResult)
                        let source = poiResult.source
                        let poi = poiResult.item
                        if (source !== ‘search‘) {
                            poiPicker.searchByKeyword(poi.name)
                        } else {
                            poiPicker.clearSearchResults()
                            vm.markers = []
                            let lng = poi.location.lng
                            let lat = poi.location.lat
                            let address = poi.cityname + poi.adname + poi.name
                            vm.center = [lng, lat]
                            vm.markers.push([lng, lat])
                            vm.lng = lng
                            vm.lat = lat
                            vm.address = address
                            vm.searchKey = address
                        }
                    })
                })
            },
            searchByHand() {
                if (this.searchKey !== ‘‘) {
                    this.poiPicker.searchByKeyword(this.searchKey)
                }
            }
        }
    }

</script>

4.在.eslintrc.js 中引入 globals

globals: {
        ‘AMap‘: false,
        ‘AMapUI‘: false
    }

原文地址:https://www.cnblogs.com/zhouze/p/11431553.html

时间: 2024-08-02 08:34:54

vue中使用高德地图的相关文章

在vue中使用高德地图vue-amap

1.安装 vue-amap我安装指定版本0.5.10的版本 npm i --save [email protected] 2.main.js中的配置 key申请地址教程:https://lbs.amap.com/api/javascript-api/guide/abc/prepare // 高德离线地图 import VueAMap from 'vue-amap'; Vue.use(VueAMap); VueAMap.initAMapApiLoader({ // 高德key key: 'd6ea

vue 中使用高德地图, 地图选点

功能简述: 网页版高德地图开发,主要支持地图选点(没做导航),搜索选点.(功能类似微信与好友聊天中的选择位置,发送位置,如下图) 第一步:先成为高德地图的开发者(获取高德地图的key);获取key 第二步:引入高德地图js文件 <!-- 高德地图 --> <script src="https://webapi.amap.com/maps?v=1.4.14&key=你申请到高德地图开发者key&plugin=AMap.Driving"></s

在第三方应用中打开高德地图的特色功能页面

由于最近的项目中需要在第三方应用中直接打开高德地图的电子狗页面,在高德开发者论坛找了一遍无果之后,只能自己反编译高德地图来找了,现在记录下来方便大家有类似需求时可以直接找到,这样就不需要反编译了. 高德地图有一些非常方便的特色功能,我们可以直接在第三方应用中打开高德地图的这些特色功能页面方便大家使用,前提是手机上必须要安装高德地图. 首先反编译高德地图的APK得到AndroidMainfest.xml文件: <?xml version="1.0" encoding="u

vue-cli2.X中引入高德地图,将其设为全局对象

平时一般未用脚手架构建或用webpack构建的项目只要用script标签引入即可,在vue-cli 2.X构建的项目中,需要用如下方式引入高德地图: 首先在高德地图开放平台中注册开发者账号并获取key,传送门:https://lbs.amap.com/api/javascript-api/guide/abc/prepare 接下来在cli项目根目录下的index.html中的<header>标签中用script标签引入 <script type="text/javascript

Vue中使用百度地图——根据输入框输入的内容,获取详细地址

知识点:在Vue.js项目中调用百度地图API,实现input框,输入地址,在百度地图上定位到准确地址,获得到经纬度 参考博客:  百度地图的引用,初步了解参考博客:http://blog.csdn.net/docallen/article/details/70877925 详细功能修改参考博客: https://www.cnblogs.com/NearTheSea/p/6808093.html 效果图:在input输入框中,输入要查询的地址,搜索出相关的名字 选中详细地址,地图定位到详细地址

在flutter中使用高德地图

1. 配置Android原生文件,在如下文件中的application中添加如下内容        2. 配置高德地图的sha1 2.1在Android studio中打开原生Android窗口 2.2. 找到sha1:双击signingReport即可获取 2.3 原文地址:https://www.cnblogs.com/xhrr/p/11420148.html

js中实现高德地图坐标经纬度转百度地图坐标

1 function tobdMap(x, y) { 2 var x_pi = 3.14159265358979324 * 3000.0 / 180.0; 3 var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi); 4 var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi); 5 var bd_lon = z * Math.cos(theta) + 0.00

GMap.Net中使用高德地图

GMap.Net是.Net下一个地图控件,可以基于Http协议加载各个公司的地图,如Google,Bing,ArcGIS的,主要原理是通过解析各个公司的地图服务的URL,传入相应的参数得到对应的切片底图(Tiled map),如一个高德地图的GMapProvider的实现如下: using System; using GMap.NET; using GMap.NET.MapProviders; using GMap.NET.Projections; namespace GMapProviders

vue里使用高德地图

一.发现vue-amap 发现vue-amap插件,优点是文档详细,易于上手 二.使用vue-amap 1 准备  在使用这个插件之前,首先需要申请高德地图的key.这个到官网就可以申请.申请过程就不叙述了. 2 安装vue-amap npm包安装:npm install -s vue-amap CDN安装:引入组件 3 引入vue-amap(这里以npm安装方式为例) 4 使用vue-amap 使用高德地图api,根据上面的demo使用 原文地址:https://www.cnblogs.com