How to Convert IP Addresses to Latitude/Longitude Using Baidu API, Logstash, and MaxMind

This guide explains how to transform user IP addresses into geographic coordinates by leveraging Baidu's IP APIs, Logstash's built‑in geoip filter, and MaxMind's GeoIP Java library, including code samples, response formats, and practical usage considerations.

ITPUB
ITPUB
ITPUB
How to Convert IP Addresses to Latitude/Longitude Using Baidu API, Logstash, and MaxMind

1. Baidu IP Geolocation APIs

Two Baidu services are available: a standard IP service and a high‑accuracy service. Both return JSON and require an API key (AK) and a coordinate system parameter.

Standard IP service : https://api.map.baidu.com/location/ip?ak=YOUR_AK&coor=bd09ll High‑accuracy service :

https://api.map.baidu.com/highacciploc/v1?qcip=IP_ADDRESS&qterm=pc&ak=YOUR_AK&coord=bd09ll

A typical response from the standard service includes address hierarchy and a point object with longitude ( x) and latitude ( y).

{
  "address": "CN|吉林|长春|None|CERNET|1|None",
  "content": {
    "address": "吉林省长春市",
    "address_detail": {
      "city": "长春市",
      "city_code": 53,
      "district": "",
      "province": "吉林省",
      "street": "",
      "street_number": ""
    },
    "point": {
      "x": "125.31364243",
      "y": "43.89833761"
    }
  },
  "status": 0
}

The high‑accuracy API adds a location object containing lat, lng, radius and confidence fields.

2. Automatic conversion with Logstash

Logstash includes a geoip filter that looks up IP addresses against an internal MaxMind database. The following minimal pipeline reads a JSON log file, parses the IP field, and outputs enriched events.

input{
  file{
    path => "D:/access.json"
    start_position => "beginning"
  }
}
filter{
  json{ source => "message" }
  date{
    match => ["time","yyyy-MM-dd HH:mm:ss"]
    timezone => "Asia/Shanghai"
  }
  geoip{
    source => "ip"
    target => "geoip"
  }
}
output{
  stdout{ codec => rubydebug }
}

The resulting event contains a geoip field with latitude, longitude and other location metadata.

3. Using MaxMind GeoIP library in Java

Download the free GeoLiteCity-2013-01-18.dat database and the geoip-api-1.3.1.jar library. Place the .dat file locally and add the JAR to the classpath. Example code to resolve an IP address:

import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import java.io.IOException;

public class TestMain {
    public static void main(String[] args) {
        try {
            LookupService lookup = new LookupService(
                "D:/lib/geoip/GeoLiteCity-2013-01-18.dat",
                LookupService.GEOIP_MEMORY_CACHE);
            Location loc = lookup.getLocation("144.0.9.29");
            System.out.println(
                "countryCode: " + loc.countryCode + "
" +
                "countryName: " + loc.countryName + "
" +
                "region: " + loc.region + "
" +
                "city: " + loc.city + "
" +
                "latitude: " + loc.latitude + "
" +
                "longitude: " + loc.longitude);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Sample output:

countryCode: CN
countryName: China
region: 25
city: Jinan
latitude: 36.668304
longitude: 116.99719

4. Practical considerations

Baidu APIs require registration and enforce rate limits, making them unsuitable for large‑scale batch geocoding.

Logstash’s built‑in MaxMind database is based on an older free data file; newer or more accurate data may require a paid subscription.

Commercial MaxMind services provide higher accuracy and support but involve licensing costs.

5. References

MaxMind GeoIP demo: https://www.maxmind.com/en/geoip-demo

Baidu IP API documentation: http://lbsyun.baidu.com/index.php?title=webapi/ip-api

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaLogstashIP geolocationBaidu APIGeoIPMaxMind
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.