Calling IP2Location inside MATLAB

MATLAB is a programming platform designed specifically for engineers and scientists to analyze and design systems and products that transform our world. It also has the ability to interface with Java classes provided by external parties to perform custom tasks. Today we will look at how to call the IP2Location Java library inside of MATLAB.

Prerequisites

First of all, you will need to have MATLAB installed. You’ll also either need a trial or paid license to use it. Download the installer from the below and run it:

https://www.mathworks.com/products/matlab.html

After you’ve installed MATLAB, download the latest IP2Location JAR file from the below:

https://search.maven.org/artifact/com.ip2location/ip2location-java

The JAR file should be named like ip2location-java-x.y.z.jar so make sure you get the correct file.

Copy it to a folder e.g., C:\myfolder\ in our case.

Next, you’ll need the IP2Location BIN database file. Download the zipped file containing the BIN file from either of the links below.

For the commercial file, go to https://www.ip2location.com/database/ip2location.

For the free LITE file, go to https://lite.ip2location.com/.

Extract the BIN file into the above folder.

IP2Location LITE database

Let’s look at the codes now

Configure the IP2Location JAR class path

Open the MATLAB application and you’ll see something like below:

Matlab

Inside the Command Window, type the following code to set the IP2Location Java JAR path.

javaaddpath('C:\myfolder\ip2location-java-8.9.1.jar')

Note that the above is our JAR file version and folder, yours may differ so modify the command accordingly.

We are using the dynamic Java class path style for our demo but for production systems, you may want to use the static path style instead. With the static path, you don’t have to set the class path each time you use MATLAB.

However, we won’t cover the static path style. Please see the below for instructions on setting up the static path.

https://www.mathworks.com/help/matlab/matlab_external/static-path-of-java-class-path.html

Initialize the IP2Location Java component

Run the below command to create the IP2Location Java object.

ipl = javaObject('com.ip2location.IP2Location')

You’ll see something like the below which means your IP2Location Java object has been created.

Configure the IP2Location Java component

After creating the IP2Location Java object, you need to configure it with the IP2Location BIN file location to use in geolocation queries.

Run the below to configure the BIN file path.

ipl.Open('C:\myfolder\IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY.BIN')

Querying IP geolocation data

Let’s now query an IP address for the geolocation data. For our example, we’ll use 8.8.8.8 as our IP address.

res = ipl.IPQuery('8.8.8.8')

See the results in the image below.

IP lookup result

Conclusion

It is really that easy to incorporate geolocation functionality into your MATLAB codes. All you need is the IP2Location Java component along with the IP2Location BIN database file.

Was this article helpful?

Related Articles