vue3高德地图使用,地址搜索,地址逆解析

在vue3项目里使用高德地图

高德地图文档
先在项目的index.html页面里添加一些东西
在这里插入图片描述

<script type="text/javascript">
      window._AMapSecurityConfig = {
        securityJsCode: "xxxxxxxxxxxxx", //高德安全码
      };
    </script>
<script src="https://webapi.amap.com/maps?v=2.0&key=高德地图key&plugin=AMap.Geolocation"></script>

在这里插入图片描述

使用页面,初始化创建显示地图

//注意在页面添加该元素
<div id="container"></div>

 //初始化地图
 //初始化地图
 let autoComplete;
 declare let AMap: any; //ts检测忽略AMap,ts检测会报错
 let map,marker;
 const addMap = (longitude:string|number|undefined = 116.40389, latitude:string|number|undefined = 39.91497) =>{
    map = new AMap.Map('container', {
         //设置地图容器id
         viewMode: '2D', //是否为3D地图模式
         zoom: 14, //初始化地图级别
         center: [longitude,latitude], //初始化地图中心点位置
         resizeEnable: true
     });
     //地图标注点位置
     marker = new AMap.Marker({
         position: [longitude,latitude],
     });
     map.add(marker); //添加到地图
     map.on('click',mapClick);//添加地图点击事件
     AMap.plugin("AMap.AutoComplete", function () {
         //实例化AutoComplete
         autoComplete = new AMap.AutoComplete();
     });
 }


//地图搜索
//地图搜索
//地图搜索,keyword为搜索的内容,string类型
 autoComplete.search(keyword, function (status, result) {
        console.log(status,result.tips);
 });


let location = new AMap.LngLat(lng, lat);//将经纬度转换成高德location对象


//改变地图中心点和标注点
//改变地图中心点和标注点
//location是一个对象,地图搜索获取到的数据里就有location对象,或者通过上面那方法也可以将经纬度转成location对象
map.setCenter(location)//修改标注点位置,也可以不传这对象,直接传经纬度 [经度,纬度]
marker.setPosition(location)//修改地图中心点位置,也可以不传这对象,直接传经纬度 [经度,纬度]


//地址逆解析
//地址逆解析
//地址逆解析获取地名,我直接用了axios来请求,也可以用jsonp
//地址逆解析是web服务api的key,跟前面index.html(js Api)里的key不同(可能相同也行吧,我没试)
axios.get('https://restapi.amap.com/v3/geocode/regeo?key=地图key&location='+lng+','+lat).then(res =>{
    if(res.data.regeocode){
        console.log(res)
    }
})

Vue3使用高德地图输入地址进行标记的基本步骤通常包括以下几个阶段: 1. 注册高德开放平台账号并获取API Key。 2.Vue3项目中安装高德地图的npm包,例如使用`npm install @amap/amap-jsapi-loader`。 3. 在组件中引入并使用`@amap/amap-jsapi-loader`进行地图实例的加载。 4. 使用高德地图提供的服务,如地址解析服务(GeocodingService),将用户输入的地址转换为地图上的经纬度坐标。 5. 将获取到的经纬度坐标作为参数,创建标记(Marker)并添加到地图上显示。 以下是一个简单的示例代码,展示了如何在Vue3实现输入地址后标记地图: ```javascript <template> <div id="mapContainer" style="width: 100%; height: 500px;"></div> <input v-model="address" placeholder="输入地址" /> <button @click="addMarker">标记</button> </template> <script setup> import { ref, onMounted } from &#39;vue&#39;; import AMapLoader from &#39;@amap/amap-jsapi-loader&#39;; const address = ref(&#39;&#39;); const map = ref(null); onMounted(async () => { await AMapLoader.load({ key: &#39;你的API Key&#39;, // 替换为你的API Key version: &#39;2.0&#39;, // 高德地图JS API的版本号 plugins: [&#39;AMap.Geocoder&#39;], // 需要使用的插件列表 }); map.value = new AMap.Map(&#39;mapContainer&#39;, { zoom: 10, }); }); const addMarker = async () => { if (!map.value) { return; } const geocoder = new AMap.Geocoder(); geocoder.getAddress(address.value, (status, result) => { if (status === &#39;complete&#39; && result.info === &#39;OK&#39;) { const marker = new AMap.Marker({ map: map.value, position: result.regeocode.formattedAddress, // 使用地址解析的结果 }); map.value.setFitView(); // 地图自适应显示所有标记 } else { alert(&#39;地址解析失败,请检查输入的地址是否正确!&#39;); } }); }; </script> <style> #mapContainer { width: 100%; height: 500px; } </style> ``` 在上述代码中,首先在页面上提供了一个输入框用于用户输入地址,并且有一个按钮用于触发地址标记的动作。在`mounted`生命周期钩子中加载高德地图JSAPI,并创建一个地图实例。`addMarker`方法则利用高德地图的地理编码服务将用户输入的地址转换为经纬度,并创建一个标记添加到地图上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值