高德地圖 定位 根據經緯度獲取信息 搜索



// 地圖生命周期 const amapEvents = { created: () => { if (!map) { setMap((window as any).AMap); } }, }; // 輸入時搜索提示信息 const changeFunc = (val) => { setSouVl(val); let auto; let geocoder; // 搜索范圍 map.plugin('AMap.Autocomplete', () => { auto = new map.Autocomplete({ input: 'tipinput', city: '全國' }); }); map.plugin(['AMap.Geocoder'], function () { geocoder = new map.Geocoder({ radius: 1000, extensions: 'all', city: '全國', }); }); map.plugin('AMap.PlaceSearch', () => { const place = new map.PlaceSearch({ pageSize: 50, pageIndex: 1, autoFitView: true, // 是否自動調整地圖視野使繪制的 Marker點都處於視口的可見范圍 }); map.event.addListener(auto, 'select', (e) => { // 判斷是否有坐標信息 if (e?.poi?.location) { console.log({ lng: e?.poi?.location?.lng, lat: e?.poi?.location?.lat }); } else { geoCode(e?.poi?.name); // 進行名稱搜索 } place.search(e?.poi?.name); geocoder.getAddress(e?.poi?.location, function (status, result) { if (status === 'complete' && result.regeocode) { const address = result?.regeocode?.formattedAddress; console.log('詳情地址',address); console.log(e?.poi?.name); } }); }); }); }; // 點擊搜索 const SearchFunc = (val) => { geoCode(val); }; // 市縣級轉換成經緯度 function geoCode(address) { const geocoder = new map.Geocoder({ radius: 1000, // 以已知坐標為中心點,radius為半徑,返回范圍內興趣點和道路信息 extensions: 'all', // 返回地址描述以及附近興趣點和道路信息,默認"base" city: '全國', }); geocoder.getLocation(address, function (status, result) { if (status === 'complete' && result.geocodes.length) { const lnglat = result?.geocodes[0].location; console.log({ lng: lnglat?.lng, lat: lnglat?.lat }); console.log(result?.geocodes[0]?.formattedAddress); console.log(result?.geocodes[0]?.formattedAddress); } }); } // 根據經緯度獲得詳細地址 function getPositionByLonLats(lng, lat) { // console.log("經度:"+lng+"緯度"+lat); const lnglatXY = [lng, lat]; // 地圖上所標點的坐標 map.service(['AMap.Geocoder'], function () { // 回調函數 const geocoder = new map.Geocoder({ city: '全國', }); geocoder.getAddress(lnglatXY, function (status, result) { if (status === 'complete' && result.info === 'OK') { const address = result.regeocode.formattedAddress; console.log(address); } }); }); }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM