高德地图 定位 根据经纬度获取信息 搜索



// 地图生命周期 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