constgetLocation= () => {
AMap.plugin('AMap.Geolocation', function () {
vargeolocation=newAMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000 })
geolocation.getCityInfo(function (status, result) {
if (status=='complete') {
console.log(result, 'Ip获取的')
} else {
onError(result)
}
})
geolocation.getCurrentPosition(function (status, result) {
if (status=='complete') {
console.log(result)
} else {
onError(result)
}
})
})
functiononError (_data) {
console.log('🚀 ~ file: AboutView.vue ~ line 72 ~ onError ~ _data', _data)
getLngLatLocation()
}
}
constgetLngLatLocation= () => {
AMap.plugin('AMap.CitySearch', function () {
varcitySearch=newAMap.CitySearch()
citySearch.getLocalCity(function (status, result) {
if (status==='complete'&&result.info==='OK') {
console.log('通过ip获取当前城市:', result)
self.cityName=result.cityself.cityCode=result.adcodevarlnglat=result.rectangle.split(';')[0].split(',')
self.longitude=lnglat[0]
self.latitude=lnglat[1]
AMap.plugin('AMap.Geocoder', function () {
vargeocoder=newAMap.Geocoder({
city: result.adcode })
varlnglat=result.rectangle.split(';')[0].split(',')
geocoder.getAddress(lnglat, function (status, data) {
if (status==='complete'&&data.info==='OK') {
console.log(data, '地理位置')
}
})
})
} else {
}
})
})
}
getLocation()