IP2Location IP Geolocation Scala Library

This IP Geolocation library is the Scala programming language library to support all IP2Location™ database products. It has been optimized for speed and memory utilization. Developers can use the module to query all IP2Location™ binary databases for applications written using the Scala programming language.

Sample Codes
object IP2LocationTest {
  def main(args: Array[String]): Unit = {
    try {
      val ip = "8.8.8.8"
      
      // querying with the BIN file
      val loc = new IP2Location
      val binfile = "/usr/data/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-DISTRICT-ASN.BIN"
      val useMMF = true
      loc.Open(binfile, useMMF)
      val rec = loc.IPQuery(ip)
      if ("OK" == rec.getStatus) System.out.println(rec)
      else if ("EMPTY_IP_ADDRESS" == rec.getStatus) System.out.println("IP address cannot be blank.")
      else if ("INVALID_IP_ADDRESS" == rec.getStatus) System.out.println("Invalid IP address.")
      else if ("MISSING_FILE" == rec.getStatus) System.out.println("Invalid database path.")
      else if ("IPV6_NOT_SUPPORTED" == rec.getStatus) System.out.println("This BIN does not contain IPv6 data.")
      else System.out.println("Unknown error." + rec.getStatus)
      loc.Close()
      
      // querying with the web service
      val ws = new IP2LocationWebService
      val strAPIKey = "XXXXXXXXXX" // replace this with your IP2Location Web Service API key
      val strPackage = "WS25"
      val addOn = Array("continent", "country", "region", "city", "geotargeting", "country_groupings", "time_zone_info")
      val strLang = "es"
      val boolSSL = true
      ws.Open(strAPIKey, strPackage, boolSSL)
      var myresult = ws.IPQuery(ip, addOn, strLang)
      
      if (myresult.get("response") == null) { // standard results
        System.out.println("country_code: " + (if (myresult.get("country_code") != null) myresult.get("country_code").getAsString
        else ""))
        System.out.println("country_name: " + (if (myresult.get("country_name") != null) myresult.get("country_name").getAsString
        else ""))
        System.out.println("region_name: " + (if (myresult.get("region_name") != null) myresult.get("region_name").getAsString
        else ""))
        System.out.println("city_name: " + (if (myresult.get("city_name") != null) myresult.get("city_name").getAsString
        else ""))
        System.out.println("latitude: " + (if (myresult.get("latitude") != null) myresult.get("latitude").getAsString
        else ""))
        System.out.println("longitude: " + (if (myresult.get("longitude") != null) myresult.get("longitude").getAsString
        else ""))
        System.out.println("zip_code: " + (if (myresult.get("zip_code") != null) myresult.get("zip_code").getAsString
        else ""))
        System.out.println("time_zone: " + (if (myresult.get("time_zone") != null) myresult.get("time_zone").getAsString
        else ""))
        System.out.println("isp: " + (if (myresult.get("isp") != null) myresult.get("isp").getAsString
        else ""))
        System.out.println("domain: " + (if (myresult.get("domain") != null) myresult.get("domain").getAsString
        else ""))
        System.out.println("net_speed: " + (if (myresult.get("net_speed") != null) myresult.get("net_speed").getAsString
        else ""))
        System.out.println("idd_code: " + (if (myresult.get("idd_code") != null) myresult.get("idd_code").getAsString
        else ""))
        System.out.println("area_code: " + (if (myresult.get("area_code") != null) myresult.get("area_code").getAsString
        else ""))
        System.out.println("weather_station_code: " + (if (myresult.get("weather_station_code") != null) myresult.get("weather_station_code").getAsString
        else ""))
        System.out.println("weather_station_name: " + (if (myresult.get("weather_station_name") != null) myresult.get("weather_station_name").getAsString
        else ""))
        System.out.println("mcc: " + (if (myresult.get("mcc") != null) myresult.get("mcc").getAsString
        else ""))
        System.out.println("mnc: " + (if (myresult.get("mnc") != null) myresult.get("mnc").getAsString
        else ""))
        System.out.println("mobile_brand: " + (if (myresult.get("mobile_brand") != null) myresult.get("mobile_brand").getAsString
        else ""))
        System.out.println("elevation: " + (if (myresult.get("elevation") != null) myresult.get("elevation").getAsString
        else ""))
        System.out.println("usage_type: " + (if (myresult.get("usage_type") != null) myresult.get("usage_type").getAsString
        else ""))
        System.out.println("address_type: " + (if (myresult.get("address_type") != null) myresult.get("address_type").getAsString
        else ""))
        System.out.println("category: " + (if (myresult.get("category") != null) myresult.get("category").getAsString
        else ""))
        System.out.println("category_name: " + (if (myresult.get("category_name") != null) myresult.get("category_name").getAsString
        else ""))
        System.out.println("credits_consumed: " + (if (myresult.get("credits_consumed") != null) myresult.get("credits_consumed").getAsString
        else ""))
        // continent addon
        if (myresult.get("continent") != null) {
          val continentObj = myresult.getAsJsonObject("continent")
          System.out.println("continent => name: " + continentObj.get("name").getAsString)
          System.out.println("continent => code: " + continentObj.get("code").getAsString)
          val myarr = continentObj.getAsJsonArray("hemisphere")
          System.out.println("continent => hemisphere: " + myarr.toString)
          System.out.println("continent => translations: " + continentObj.getAsJsonObject("translations").get(strLang).getAsString)
        }
        // country addon
        if (myresult.get("country") != null) {
          val countryObj = myresult.getAsJsonObject("country")
          System.out.println("country => name: " + countryObj.get("name").getAsString)
          System.out.println("country => alpha3_code: " + countryObj.get("alpha3_code").getAsString)
          System.out.println("country => numeric_code: " + countryObj.get("numeric_code").getAsString)
          System.out.println("country => demonym: " + countryObj.get("demonym").getAsString)
          System.out.println("country => flag: " + countryObj.get("flag").getAsString)
          System.out.println("country => capital: " + countryObj.get("capital").getAsString)
          System.out.println("country => total_area: " + countryObj.get("total_area").getAsString)
          System.out.println("country => population: " + countryObj.get("population").getAsString)
          System.out.println("country => idd_code: " + countryObj.get("idd_code").getAsString)
          System.out.println("country => tld: " + countryObj.get("tld").getAsString)
          System.out.println("country => translations: " + countryObj.getAsJsonObject("translations").get(strLang).getAsString)
          val currencyObj = countryObj.getAsJsonObject("currency")
          System.out.println("country => currency => code: " + currencyObj.get("code").getAsString)
          System.out.println("country => currency => name: " + currencyObj.get("name").getAsString)
          System.out.println("country => currency => symbol: " + currencyObj.get("symbol").getAsString)
          val languageObj = countryObj.getAsJsonObject("language")
          System.out.println("country => language => code: " + languageObj.get("code").getAsString)
          System.out.println("country => language => name: " + languageObj.get("name").getAsString)
        }
        // region addon
        if (myresult.get("region") != null) {
          val regionObj = myresult.getAsJsonObject("region")
          System.out.println("region => name: " + regionObj.get("name").getAsString)
          System.out.println("region => code: " + regionObj.get("code").getAsString)
          System.out.println("region => translations: " + regionObj.getAsJsonObject("translations").get(strLang).getAsString)
        }
        // city addon
        if (myresult.get("city") != null) {
          val cityObj = myresult.getAsJsonObject("city")
          System.out.println("city => name: " + cityObj.get("name").getAsString)
          System.out.println("city => translations: " + cityObj.getAsJsonArray("translations").toString)
        }
        // geotargeting addon
        if (myresult.get("geotargeting") != null) {
          val geoObj = myresult.getAsJsonObject("geotargeting")
          System.out.println("geotargeting => metro: " + geoObj.get("metro").getAsString)
        }
        // country_groupings addon
        if (myresult.get("country_groupings") != null) {
          val myarr = myresult.getAsJsonArray("country_groupings")
          if (myarr.size > 0) for (x <- 0 until myarr.size) {
            System.out.println("country_groupings => #" + x + " => acronym: " + myarr.get(x).getAsJsonObject.get("acronym").getAsString)
            System.out.println("country_groupings => #" + x + " => name: " + myarr.get(x).getAsJsonObject.get("name").getAsString)
          }
        }
        // time_zone_info addon
        if (myresult.get("time_zone_info") != null) {
          val tzObj = myresult.getAsJsonObject("time_zone_info")
          System.out.println("time_zone_info => olson: " + tzObj.get("olson").getAsString)
          System.out.println("time_zone_info => current_time: " + tzObj.get("current_time").getAsString)
          System.out.println("time_zone_info => gmt_offset: " + tzObj.get("gmt_offset").getAsString)
          System.out.println("time_zone_info => is_dst: " + tzObj.get("is_dst").getAsString)
          System.out.println("time_zone_info => sunrise: " + tzObj.get("sunrise").getAsString)
          System.out.println("time_zone_info => sunset: " + tzObj.get("sunset").getAsString)
        }
      }
      else System.out.println("Error: " + myresult.get("response").getAsString)
      myresult = ws.GetCredit
      if (myresult.get("response") != null) System.out.println("Credit balance: " + myresult.get("response").getAsString)
    } catch {
      case e: Exception =>
        System.out.println(e)
        e.printStackTrace(System.out)
    }
  }
}
Sample IP2Location Databases (BIN)
Sample Packages - BIN File IPv4 IPv6
IP2Location DB1 Download (973.14 kB) Download (1.02 MB)
IP2Location DB2 Download (31.70 MB) Download (32.47 MB)
IP2Location DB3 Download (21.45 MB) Download (21.54 MB)
IP2Location DB4 Download (51.00 MB) Download (51.12 MB)
IP2Location DB5 Download (32.52 MB) Download (32.66 MB)
IP2Location DB6 Download (61.55 MB) Download (61.73 MB)
IP2Location DB7 Download (57.13 MB) Download (57.24 MB)
IP2Location DB8 Download (67.93 MB) Download (68.07 MB)
IP2Location DB9 Download (37.52 MB) Download (37.68 MB)
IP2Location DB10 Download (73.73 MB) Download (73.94 MB)
IP2Location DB11 Download (38.27 MB) Download (38.42 MB)
IP2Location DB12 Download (74.35 MB) Download (74.47 MB)
IP2Location DB13 Download (36.57 MB) Download (36.67 MB)
IP2Location DB14 Download (75.76 MB) Download (75.95 MB)
IP2Location DB15 Download (41.53 MB) Download (41.74 MB)
IP2Location DB16 Download (80.51 MB) Download (80.76 MB)
IP2Location DB17 Download (44.26 MB) Download (44.43 MB)
IP2Location DB18 Download (88.33 MB) Download (88.67 MB)
IP2Location DB19 Download (70.97 MB) Download (70.96 MB)
IP2Location DB20 Download (90.93 MB) Download (91.18 MB)
IP2Location DB21 Download (45.13 MB) Download (45.42 MB)
IP2Location DB22 Download (95.10 MB) Download (95.46 MB)
IP2Location DB23 Download (71.48 MB) Download (71.65 MB)
IP2Location DB24 Download (96.55 MB) Download (96.71 MB)
IP2Location DB25 Download (98.44 MB) Download (98.82 MB)
IP2Location DB26 Download (105.48 MB) Download (105.74 MB)
Pop Up Box

Discover Your User Locations

Retrieve geolocation data for FREE now!