Search code examples
geolocationmaxmind

Mapping IP to latitude and longitude by MaxMind database


I want to map a bunch of IP addresses to their latitude and longitude by MaxMind database. My code is in C, I do not know how to use this database.


Solution

  • I assume you want something along these lines:

    #include <stdio.h>
    #include <GeoIP.h>
    #include <GeoIPCity.h>
    
    int main()
    {
        GeoIP *gi;
        GeoIPRecord *gir;
    
        gi = GeoIP_open("/usr/local/share/GeoIP/GeoIPCity.dat", GEOIP_INDEX_CACHE);
    
        if (gi == NULL) {
            /* handle error */
        }
    
        gir = GeoIP_record_by_name(gi, "1.1.1.1");
        if (gir == NULL) {
            /* handle error */
        }
    
        printf("latitude: %f   longitude: %f", gir->latitude, gir->longitude);
    }