vue 使用高德地图点击标记点以及经纬度转地理位置

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

<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.13&key=your key"></script> 

2、在webpack.base.conf.js 代码最后面引入

externals: {
    'AMap': 'AMap',
  }

3、在vue文件中使用

<template>
<div id="my_container"></div>
</template>
<script>
    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()
                });
            },
        },
    }
</script>

<style scoped>
#my_container{
    margin-left: 110px;
    height: 300px;
    width: calc(100% - 110px);
}
</style>
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现在Vue使用高德地图,并根据经纬度标记地理位置,可以按照以下步骤进行操作。 首先,需要在项目中引入高德地图的JavaScript SDK。可以通过在index.html文件中引入以下代码: ```html <script src="//webapi.amap.com/maps?v=1.4.15&key=your-amap-key"></script> ``` 其中,替换"your-amap-key"为你自己的高德地图API密钥。 接下来,在Vue组件中创建一个地图容器,可以使用div元素来承载地图。在组件的`mounted`生命周期钩子中,初始化地图并设置中心和缩放级别: ```javascript mounted() { var map = new AMap.Map('map-container', { center: [经度, 纬度], zoom: 缩放级别 }); } ``` 其中,将`经度`和`纬度`替换为具体的地理位置经纬度坐标,并根据需要设置合适的`缩放级别`。 要在地图上标记一个地,可以使用`AMap.Marker`对象。通过创建一个新的`AMap.Marker`实例,并设置其位置和图标,即可在地图上显示标记: ```javascript var marker = new AMap.Marker({ position: [经度, 纬度], icon: 'http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png' // 自定义标记图标 }); marker.setMap(map); // 将标记添加到地图上 ``` 同样,将`经度`和`纬度`替换为具体的地理位置经纬度坐标。如果需要自定义标记的图标,可以通过设置`icon`属性,将图标的URL传递给它。 最后,在模板中添加一个专门承载地图的div元素: ```html <div id="map-container"></div> ``` 这样,就可以在Vue中实现高德地图使用,并根据经纬度在地图上标记地理位置了。记得替换相关的经纬度高德地图API密钥,以适应你的实际需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

haeasringnar

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

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

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

打赏作者

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

抵扣说明:

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

余额充值