Search code examples
phpgeocodingmaxmind

PHP convert city name into latitude/longitude with Maxmind Geolite2


How do I get the latitude/longitude from inputing a city/state name using the Maxmind geolite2 database?

(I installed the geolite2 php API already for ip lookups but now in addition I want to use it in reverse, so to speak)

If there is another practical way to do this in php by all means let me know but I DO NOT want to use the google geo-encoder API or any other external API for this.

THANK YOU!


Solution

  • Simply put: you can't. Not only does the API not provide the means to do this (MaxMind is an IP-based geocoding provider,) the database doesn't come remotely close to containing every city you might want to look up.

    To get at the few cities that are in there you can grab the city database in CSV format and import the tables (there are two: "locations" has city and state names while "blocks" has latitude and longitude information) into your own SQL database to do the lookups from there.

    If you change your mind about using another API, you might want to take a look at geocoder-php and choose a provider that supports address-based geocoding which is what you're after.