Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve geographical location from users using Angular [duplicate]

I'm programming in Angular with Openlayers library. I want to use this API : https://adresse.data.gouv.fr/api (the page is in french so I will explain the purpose)

The goal of this API is on the one hand to search some adresses on a map while building GeoJSON files and on the other hand to use a reverse geocoding. This is why I need geographical location from the user.

For example this request : http 'https://api-adresse.data.gouv.fr/search/?q=8 bd du port' will return all the streets in the world answering to the name "8 bd du port"

So I want to use the reverse geocoding and create a request like this : http 'https://api-adresse.data.gouv.fr/reverse/?lon=user_lon&lat=user_lat'

It is the best way to proceed ? I don't want to use an another API like Google one

like image 563
Adrien Avatar asked May 09 '26 22:05

Adrien


1 Answers

You can use the HTML standard Geolocation api for this.

  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
  }
like image 175
Malcor Avatar answered May 11 '26 10:05

Malcor