• Your shopping cart is empty!

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.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 IPv4 IPv6
IP2Location DB1 Sample BIN File Download (831.73 kB) Download (879.82 kB)
IP2Location DB2 Sample BIN File Download (33.66 MB) Download (34.47 MB)
IP2Location DB3 Sample BIN File Download (20.38 MB) Download (20.40 MB)
IP2Location DB4 Sample BIN File Download (51.90 MB) Download (51.92 MB)
IP2Location DB5 Sample BIN File Download (30.93 MB) Download (30.95 MB)
IP2Location DB6 Sample BIN File Download (61.77 MB) Download (61.82 MB)
IP2Location DB7 Sample BIN File Download (58.45 MB) Download (58.49 MB)
IP2Location DB8 Sample BIN File Download (68.49 MB) Download (68.59 MB)
IP2Location DB9 Sample BIN File Download (35.62 MB) Download (35.64 MB)
IP2Location DB10 Sample BIN File Download (74.20 MB) Download (74.24 MB)
IP2Location DB11 Sample BIN File Download (36.34 MB) Download (36.36 MB)
IP2Location DB12 Sample BIN File Download (74.80 MB) Download (74.86 MB)
IP2Location DB13 Sample BIN File Download (34.72 MB) Download (34.74 MB)
IP2Location DB14 Sample BIN File Download (76.13 MB) Download (76.18 MB)
IP2Location DB15 Sample BIN File Download (39.44 MB) Download (39.51 MB)
IP2Location DB16 Sample BIN File Download (80.75 MB) Download (80.83 MB)
IP2Location DB17 Sample BIN File Download (41.91 MB) Download (41.95 MB)
IP2Location DB18 Sample BIN File Download (88.04 MB) Download (88.05 MB)
IP2Location DB19 Sample BIN File Download (71.54 MB) Download (71.60 MB)
IP2Location DB20 Sample BIN File Download (90.46 MB) Download (90.51 MB)
IP2Location DB21 Sample BIN File Download (42.90 MB) Download (42.92 MB)
IP2Location DB22 Sample BIN File Download (94.52 MB) Download (94.45 MB)
IP2Location DB23 Sample BIN File Download (71.95 MB) Download (72.06 MB)
IP2Location DB24 Sample BIN File Download (95.61 MB) Download (95.80 MB)
IP2Location DB25 Sample BIN File Download (98.13 MB) Download (98.23 MB)