vue地圖插件-vue-map-點(diǎn)擊獲取經(jīng)緯度

1.去高德開(kāi)發(fā)平臺(tái)申請(qǐng)自己的appkey

2.下載vue-amp

npm install vue-amap --save-dev

3.接下來(lái)去項(xiàng)目的main.js復(fù)制如下代碼,把a(bǔ)ppkey換成自己的


Vue.use(aMap)

aMap.initAMapApiLoader({
  key: '3025364******22bdaf9b6',  // 你的key
  //用到的插件
  plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch','AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 
'AMap.MapType', 'AMap.Geolocation','AMap.Geocoder', 'AMap.AMapManager', 'AMap.Marker'],
  v: '1.4.4', //版本
  uiVersion: '1.0' //ui版本
})

4.創(chuàng)建一個(gè)全局amap.vue組件

<template>
    <div class="container">
        <div class="search-box">
            <input
                    v-model="searchKey"
                    type="search"
                    id="search">
            <el-button @click="searchByHand" size="mini" type="primary">搜索</el-button>
            <el-button @click="submitAddress" size="mini" type="success">確認(rèn)</el-button>
            <div class="tip-box" id="searchTip"></div>
        </div>
        <el-amap class="amap-box"
                 :amap-manager="amapManager"
                 :vid="'amap-vue'"
                 :zoom="zoom"
                 :plugin="plugin"
                 :center="center"
                 :events="events"
        >
            <!-- 標(biāo)記 -->
            <el-amap-marker v-for="(marker, index) in markers" :position="marker" :key="index"></el-amap-marker>
        </el-amap>
    </div>
</template>


<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: '全國(guó)',
                    citylimit: true
                },
                center: [121.329402, 31.228667],
                zoom: 17,
                lng: 0,
                lat: 0,
                loaded: false,
                markerEvent:{
                    click(e){
                        console.log('點(diǎn)擊',e);
                    }
                },
                events: {
                    init() {
                        lazyAMapApiLoaderInstance.load().then(() => {
                            self.initSearch()
                        })
                    },
                    // 點(diǎn)擊獲取地址的數(shù)據(jù)
                    // lng精度
                    // lat維度
                    click(e) {
                        self.markers = []
                        let {lng, lat} = e.lnglat
                        self.lng = lng
                        self.lat = lat
                        console.log(e)
                        self.center = [lng, lat]
                        self.markers.push([lng, lat])
                        // 這里通過(guò)高德 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: 'Geolocation',
                        events: {
                            init(o) {
                                // o是高德地圖定位插件實(shí)例
                                o.getCurrentPosition((status, result) => {
                                    if (result && result.position) {
                                        // 設(shè)置經(jīng)度
                                        self.lng = result.position.lng
                                        // 設(shè)置維度
                                        self.lat = result.position.lat
                                        // 設(shè)置坐標(biāo)
                                        self.center = [self.lng, self.lat]
                                        self.markers.push([self.lng, self.lat])
                                        // load
                                        self.loaded = true
                                        // 頁(yè)面渲染好后
                                        self.$nextTick()
                                    }
                                })
                            },
                            click(e){
                                console.log(e);
                            }
                        }
                    },
                    {
                        // 工具欄
                        pName: 'ToolBar',
                        events: {
                            init(instance) {
                                console.log(instance);
                            }
                        }
                    },
                    {
                        // 鷹眼
                        pName: 'OverView',
                        events: {
                            init(instance) {
                                console.log(instance);
                            }
                        }
                    },
                    {
                        // 地圖類(lèi)型
                        pName: 'MapType',
                        defaultType: 0,
                        events: {
                            init(instance) {
                                console.log(instance);
                            }
                        }
                    },
                    {
                        // 搜索
                        pName: 'PlaceSearch',
                        events: {
                            init(instance) {
                                console.log(instance)
                            }
                        }
                    }
                ]
            }
        },
        methods: {
            submitAddress(){
                console.log('經(jīng)緯度',this.center)
                console.log('地址',this.address)
            },
            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
                    // 監(jiān)聽(tīng)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]
                            console.log(vm.center)
                            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>



<style>
    .container {
        width:100%;
        height: 500px;
        position: relative;
        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: 59%;
        height: 100%;
        font-size:13px;
        border: 2px solid #30ccc1;
        padding: 0 8px;
        outline: none;
    }

    .search-box button {
        float: left;
        margin-left:5px;
        /* 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>

然后在需要用到的地方導(dǎo)入就可以了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容