如何使用Angular从用户那里检索地理位置



我正在使用Openlayers库在Angular中编程。 我想使用此API:https://adresse.data.gouv.fr/api(该页面是法语的,所以我将解释目的(

此 API 的目标是一方面在构建 GeoJSON 文件时在地图上搜索一些地址,另一方面是使用反向地理编码。这就是为什么我需要用户的地理位置。

例如,此请求:http 'https://api-adresse.data.gouv.fr/search/?q=8 bd du port'将返回世界上所有响应名称"8 bd du port"的街道

所以我想使用反向地理编码并创建一个这样的请求:http 'https://api-adresse.data.gouv.fr/reverse/?lon=user_lon&lat=user_lat'

这是最好的方法吗?我不想使用另一个像谷歌这样的API

您可以使用HTML标准的地理位置api。

getLocation(): void{
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position)=>{
const longitude = position.coords.longitude;
const latitude = position.coords.latitude;
this.callApi(longitude, latitude);
});
} else {
console.log("No support for geolocation")
}
}
callApi(Longitude: number, Latitude: number){
const url = `https://api-adresse.data.gouv.fr/reverse/?lon=${Longitude}&lat=${Latitude}`
//Call API
}

最新更新