由于刚开始只找到了通过js注册的方式调用AMap.Geocoder
,所以是以html的形式获取的,如方法一所示。后来在开发过程中找到了调用接口,于是有了方法二。
方法一
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>批量地理编码</title>
</head>
<body>
<input type="file" id="file" />
<script src="https://webapi.amap.com/maps?v=1.4.15&key=ead4b4ffc3093ac65bf76055625e47a6&plugin=AMap.Geocoder"></script>
<script>
const geocoder = new AMap.Geocoder({
city: '0755'
})
const jsons = []
const input = document.querySelector('#file')
input.addEventListener(
'change',
function () {
if (this.files.length > 0) {
const file = this.files[0]
const reader = new FileReader()
reader.onload = function () {
const result = JSON.parse(this.result)
for (const addr of result) {
getLnglat(addr)
}
}
reader.readAsText(file)
}
},
false
)
const getLnglat = addr => {
geocoder.getLocation(addr, (status, result) => {
if (status === 'complete' && result.geocodes.length) {
const { lng, lat } = result.geocodes[0].location
const newAddr = addr.replace(/^(广东省?)?(深圳市?)?/, '')
jsons.push({
name: newAddr,
lat,
lng
})
if (!this.debounce) {
this.debounce = debounce(() => {
console.log(JSON.stringify(jsons))}, 1000)
}
this.debounce()
} else {
console.error('根据地址查询位置失败')
}
})
}
// 防抖
const debounce = (func, delay) => {
let timer = null
return function (...args) {
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
func.apply(this, args)
}, delay)
}
}
</script>
</body>
</html>
方法二:直接通过浏览器直接拿到数据
地理编码网址
地名转经纬度
const fs = require('fs')
const https = require('https')
const content = JSON.parse(fs.readFileSync('./街道办.json', { encoding: 'utf-8' }))
const url =
'https://restapi.amap.com/v3/place/text?s=rsv3&city=440307&children=&key=xxxxxxxxxxxxx&offset=1&page=1&extensions=all&city=440307&language=zh_cn&platform=JS&logversion=2.0&appname=https%3A%2F%2Flbs.amap.com%2Ftools%2Fpicker&csid=BBC4BCC2-5C64-49EC-A870-8065EB87A48D&sdkversion=1.4.17&keywords='
const result = []
const fail = []
for (const item of content) {
https
.get(`${url}${item.name}`, res => {
res.on('data', chunk => {
const mapData = JSON.parse(chunk.toString())
if (mapData.pois && mapData.pois.length > 0) {
const { location, name } = mapData.pois[0]
if (location) {
const [lng, lat] = location.split(',')
result.push({
name: item.name,
pois: name,
address: item.address,
tel: item.tel,
location: {
lng,
lat
}
})
if (!this.debounce) {
this.debounce = debounce(() => fs.writeFileSync('./aaa.json', JSON.stringify(result, null, 2)), 1000)
}
this.debounce()
} else {
fail.push({
...item
})
if (!this.failDebounce) {
this.failDebounce = debounce(() => fs.writeFileSync('./fail.json', JSON.stringify(fail, null, 2)), 1000)
}
this.failDebounce()
}
} else {
fail.push({
...item
})
if (!this.failDebounce) {
this.failDebounce = debounce(() => fs.writeFileSync('./fail.json', JSON.stringify(fail, null, 2)), 1000)
}
this.failDebounce()
}
})
})
.on('error', error => {
console.log(`request-error, ${error}`)
})
}
const debounce = (func, delay) => {
let timer = null
return function (...args) {
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
func.apply(this, args)
}, delay)
}
}
网友评论