Get User's Device Location

How to get location of a user's device from its ip address
  1. Overview
  2. Client-side requests
  3. Server-side requests
  4. Response
  5. Example

Overview

Woosmap Geolocation API can provide the location of your users thanks to IP address of their devices. You can query the API using a public or a private key according to your use case and calls origins.

Client-side requests

For client-side requests, use your public key (parameter key) related to your project.

$.ajax({
    url: 'https://api.woosmap.com/geolocation/position/',
    type: 'GET',
    dataType:'json',
    data: {
        key:[YOUR_PUBLIC_KEY]
    },
    success: function(geoloc) {
        console.log(geoloc);
    }
});

Server-side requests

For server-side requests, get the position of a specific ip address using a private_keyin your requests.

curl 'https://api.woosmap.com/geolocation/position/?private_key={private_key}&ip_address={ip_to_geolocalize}'

Response

The Response is a formated JSON containing the keys: country_code, country_name, continent, city, region_state, latitude, longitude, accuracy, viewport
latitude, longitude values are approximate latitude and longitude of the geographical area associated with the IP address.
accuracy value is the approximate accuracy radius, in kilometers, around the latitude and longitude associated with the IP address.

According to IP data relevance and accuracy some keys may eventually not be provided.

{
    "country_code": "FR",
    "country_name": "France",
    "continent": "Europe",
    "city": "My City",
    "region_state": "my-region",
    "latitude": 51.507527,
    "longitude": -0.127942,
    "accuracy": 20,
    "viewport": {
        "northeast": {
            "lat": 51.640551,
            "lng": 0.072636
        },
        "southwest": {
            "lat": 51.373665,
            "lng": -0.320715
        }
}

Example

Here is a sample of a Geolocation API call using Javascript. The fetched location is used to generate a Google static map.

Was this article helpful?
Have more questions? Submit a request