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.
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=bd09llA 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.997194. 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
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
