vue 获取用户位置 高德_vue引入高德地图获取经纬度地址

这篇博客介绍了如何在Vue项目中集成高德地图,通过在`index.html`引入API key,配置webpack的externals,然后在Vue组件内导入并使用AMap。在地图点击事件中,利用AMap.Geolocation获取用户地理位置,并通过AMap.Geocoder将经纬度转换为详细地址,实现了地图上的点击定位和地址显示功能。
摘要由CSDN通过智能技术生成

1、在index.html引入高德地图

//key找个适合例如:160cab8ad6c50752175d76e61ef92c50

2、在webpack.base.conf.js 配置引入

externals: {

'AMap': 'AMap',

}

3、在vue文件中使用

import AMap from 'AMap'

export default {

name: "company_manage",

data () {

return {

ruleForm: {

name: '',

phone: '',

addr: '',

long: '',

lat: '',

start_work_time: '',

end_work_time: '',

},

}

},

mounted:function () {

this.init()

},

methods: {

init() {

var map = new AMap.Map('my_container',{

resizeEnable: true,

zoom: 18,

})

AMap.plugin('AMap.Geolocation',function(){ //异步加载插件

var geolocation = new AMap.Geolocation()

map.addControl(geolocation)

})

var geocoder,marker;

function regeocoder(lnglatXY,that) {

AMap.plugin('AMap.Geocoder',function(){

var geocoder = new AMap.Geocoder({

radius: 1000,

extensions: "all"

});

geocoder.getAddress(lnglatXY, function(status, result) {

if (status === 'complete' && result.info === 'OK') {

var address = result.regeocode.formattedAddress;

that.ruleForm.addr = address  //兑换地址

}

});

if(!marker){

marker = new AMap.Marker();

map.add(marker);

}

marker.setPosition(lnglatXY);

})

}

var that = this

map.on('click', function(e) {

var lnglatXY = [e.lnglat.getLng(),e.lnglat.getLat()];

regeocoder(lnglatXY,that)

that.ruleForm.long = e.lnglat.getLng()

that.ruleForm.lat = e.lnglat.getLat()

});

},

},

}

#my_container{

margin-left: 140px;

height: 400px;

width: calc(100% - 140px);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值