Sample code for 30+ languages & platforms
Dart

MaxMind IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the MaxMind GeoIP2 Precision Web Service.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final http = CkHttp();

  http.login = 'MAXMIND_ACCOUNT_ID';
  http.password = 'MAXMIND_LICENSE_KEY';
  http.accept = 'application/json';

  // Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
  final String jsonStr;
  try {
    jsonStr = http.quickGetStr('https://geoip.maxmind.com/geoip/v2.1/country/149.250.207.170');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final json = CkJsonObject();
  json.emitCompact = false;
  json.load(jsonStr);

  print(json.emit());

  // Sample output:
  // Use this online tool to generate parsing code from sample JSON: 
  // Generate Parsing Code from JSON

  // {
  //   "continent": {
  //     "code": "EU",
  //     "geoname_id": 6255148,
  //     "names": {
  //       "ja": "?????",
  //       "pt-BR": "Europa",
  //       "ru": "??????",
  //       "zh-CN": "??",
  //       "de": "Europa",
  //       "en": "Europe",
  //       "es": "Europa",
  //       "fr": "Europe"
  //     }
  //   },
  //   "country": {
  //     "is_in_european_union": true,
  //     "iso_code": "DE",
  //     "geoname_id": 2921044,
  //     "names": {
  //       "fr": "Allemagne",
  //       "ja": "????????",
  //       "pt-BR": "Alemanha",
  //       "ru": "????????",
  //       "zh-CN": "??",
  //       "de": "Deutschland",
  //       "en": "Germany",
  //       "es": "Alemania"
  //     }
  //   },
  //   "maxmind": {
  //     "queries_remaining": 49999
  //   },
  //   "registered_country": {
  //     "is_in_european_union": true,
  //     "iso_code": "DE",
  //     "geoname_id": 2921044,
  //     "names": {
  //       "es": "Alemania",
  //       "fr": "Allemagne",
  //       "ja": "????????",
  //       "pt-BR": "Alemanha",
  //       "ru": "????????",
  //       "zh-CN": "??",
  //       "de": "Deutschland",
  //       "en": "Germany"
  //     }
  //   },
  //   "traits": {
  //     "ip_address": "149.250.207.170"
  //   }
  // }
  // 
  // 

  final continentCode = json.stringOf('continent.code');
  final continentGeonameId = json.intOf('continent.geoname_id');
  final continentNamesJa = json.stringOf('continent.names.ja');
  final continentNamesPtBR = json.stringOf('continent.names.pt-BR');
  final continentNamesRu = json.stringOf('continent.names.ru');
  final continentNamesZhCN = json.stringOf('continent.names.zh-CN');
  final continentNamesDe = json.stringOf('continent.names.de');
  final continentNamesEn = json.stringOf('continent.names.en');
  final continentNamesEs = json.stringOf('continent.names.es');
  final continentNamesFr = json.stringOf('continent.names.fr');
  final countryIsoCode = json.stringOf('country.iso_code');
  final countryGeonameId = json.intOf('country.geoname_id');
  final countryNamesFr = json.stringOf('country.names.fr');
  final countryNamesJa = json.stringOf('country.names.ja');
  final countryNamesPtBR = json.stringOf('country.names.pt-BR');
  final countryNamesRu = json.stringOf('country.names.ru');
  final countryNamesZhCN = json.stringOf('country.names.zh-CN');
  final countryNamesDe = json.stringOf('country.names.de');
  final countryNamesEn = json.stringOf('country.names.en');
  final countryNamesEs = json.stringOf('country.names.es');
  final maxmindQueriesRemaining = json.intOf('maxmind.queries_remaining');
  final registeredCountryIsoCode = json.stringOf('registered_country.iso_code');
  final registeredCountryGeonameId = json.intOf('registered_country.geoname_id');
  final registeredCountryNamesEs = json.stringOf('registered_country.names.es');
  final registeredCountryNamesFr = json.stringOf('registered_country.names.fr');
  final registeredCountryNamesJa = json.stringOf('registered_country.names.ja');
  final registeredCountryNamesPtBR = json.stringOf('registered_country.names.pt-BR');
  final registeredCountryNamesRu = json.stringOf('registered_country.names.ru');
  final registeredCountryNamesZhCN = json.stringOf('registered_country.names.zh-CN');
  final registeredCountryNamesDe = json.stringOf('registered_country.names.de');
  final registeredCountryNamesEn = json.stringOf('registered_country.names.en');
  final traitsIpAddress = json.stringOf('traits.ip_address');
}