IpForensics
HomeGoto Dashboard
  • 📖Introduction
  • Protocols
    • HTTP Json Protocol
    • Response headers
    • Error Codes
  • Features
    • Pay-As-You-Go
    • API Security
    • HTTPS
  • Api Response Data
    • 📶Mobile Carrier
    • 🏥ASN
    • 📊General
    • ⏲️Timezone
    • 💵Currency
    • 👨‍💻Threat Detector
    • 🌐Company
    • 🌏Geolocation
    • 🌍Advance Geolocation
    • 💻Device Information
    • ✈️IATA/ICAO
  • API
    • 🚀Intro
    • 📊IP & Threat Intelligence API
      • Single Lookup
      • Origin Lookup
    • 💹Exchange Rate API
      • Currency Endpoint
      • Live Rate Endpoint
      • Historical Rate Endpoint
      • Currency Conversion Endpoint
      • Currency Swap Endpoint
  • Code Implantation
    • Block Automated Visits From Hosting Providers
    • Block VPN/Proxy and Tor users
    • Redirect by Country and Location Offers
    • Content Personalisation
    • Get the location from an IP Address in Javascript
    • How to get a client's IP address using JavaScript
    • HTML5 Geolocation with IpForensics fallback
    • Blocking Users by Country
    • Detecting Users Currency or Currency Personalisation
    • Preventing Free Trial Abuse
    • Device-Based Ads and App Binary
    • Detect Users Device/Computer and Operating System
    • Detect visitors from EU countries
    • How to detect and prevent credit card fraud
    • How to convert amounts to website visitor currency
  • MISC
    • Changelog
    • FAQs
    • Troubleshooting
    • API Status
    • Rate Limits
Powered by GitBook
On this page

Was this helpful?

  1. Code Implantation

HTML5 Geolocation with IpForensics fallback

An example of how you would use the ipdata API as a fallback for when HTML5 Geolocation fails or if the user blocks geolocation requests from your website

PreviousHow to get a client's IP address using JavaScriptNextBlocking Users by Country

Last updated 2 years ago

Was this helpful?

This example is adapted from the Wikipedia article on . Note that it handles every error by making a call to the IpForensics API to get the lat/long.

Also note that instead of getting the lat/long (that is when you fall back to our service) and then using another service to geocode the lat/long to a place i.e. city or country. You could simply get the country, city, region, postal code and numerous other attributes from the this.responseText response object in the example below.

const geoFindMe = () => {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(success, error, geoOptions);
    } else {
        console.log("Geolocation services are not supported by your web browser.");
    }
}

const success = (position) => {
    const latitude = position.coords.latitude;
    const longitude = position.coords.longitude;
    const altitude = position.coords.altitude;
    const accuracy = position.coords.accuracy;
    console.log(`lat: ${latitude} long: ${longitude}`);
}

const error = (error) => {
    var request = new XMLHttpRequest();
    request.open('GET', 'https://ipforensics.net/api/v1/origin?apikey=APIKEY');
    request.setRequestHeader('Accept', 'application/json');
    request.onreadystatechange = function () {
      if (this.readyState === 4) {
        data = JSON.parse(this.responseText)
        console.log(`lat: ${data.meta_data.geolocation.latitude} long: ${data.meta_data.geolocation.}`);
      }
    };
    request.send();
}

const geoOptions = {
    enableHighAccuracy: true,
    maximumAge: 30000,
    timeout: 27000
};

// call the function
geoFindMe()

Always reference the for complete response result

W3C Geolocation
API endpoint