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

Adr*_*ien 3 javascript api openlayers reverse-geocoding 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,例如 Google API

Mal*_*cor 15

您可以为此使用 HTML 标准 Geolocation 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
  }
Run Code Online (Sandbox Code Playgroud)