當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
高德地图 JS API - 根据地名实现标记定位
生活随笔
收集整理的這篇文章主要介紹了
高德地图 JS API - 根据地名实现标记定位
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
德地圖 JS API 使用前的準備工作請參考官方網站說明: https://lbs.amap.com/api/javascript-api/guide/abc/prepare
?
根據地名實現地圖標記定位,主要依賴高德地圖的:地理編碼與逆地理編碼。下面來看具體的實現代碼:
1. HTML (地圖容器)
<div class="map-container" id="container"></div>?
2. JAVASCRIPT
function markLocation(mapId, address) {AMap.plugin('AMap.Geocoder', function() {var geocoder = new AMap.Geocoder(); geocoder.getLocation(address, function(status, result) {if (status === 'complete' && result.info === 'OK') {// 經緯度 var lng = result.geocodes[0].location.lng;var lat = result.geocodes[0].location.lat;// 地圖實例var map = new AMap.Map(mapId, {resizeEnable: true, // 允許縮放center: [lng, lat], // 設置地圖的中心點zoom: 15 // 設置地圖的縮放級別,0 - 20});// 添加標記var marker = new AMap.Marker({map: map,position: new AMap.LngLat(lng, lat), // 經緯度 });} else {console.log('定位失敗!');}});}); }?
方法寫好了,調用很簡單了:
// 傳入地圖容器ID 和 地名 markLocation('container', '湖北省武漢市');?
3. 效果圖
轉載于:https://www.cnblogs.com/similar/p/9400601.html
總結
以上是生活随笔為你收集整理的高德地图 JS API - 根据地名实现标记定位的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 驱动程序实例(一):LED设备驱动程序(
- 下一篇: JavaScript 数字转汉字+ele