Utilizing Maxmind.Com's GeoIP2 (Omni) Webservice, my Drupal 7 website can fetch geographic data using the visitor's IP address.
To obtain a JSON document, I use the following code:
<script type="text/javascript" src="//js.maxmind.com/js/apis/geoip2/v2.0/geoip2.js">
</script>
<script type="text/javascript">
var onSuccess = function(location){
console.log(
"Lookup successful:\n\n"
+ JSON.stringify(location.city.names.en, undefined, 4)
);
};
var onError = function(error){
alert(
"Error:\n\n"
+ JSON.stringify(error, undefined, 4)
);
};
geoip2.omni(onSuccess, onError);
</script>
Among the retrieved values is the name of the visitor's city. How can I incorporate this value into a query string?
For instance, if 'city.names.en' = 'Detroit'
, how could I use "Detroit" as a key to fetch relevant data (e.g., local phone number) from another source like a document or table?
The ultimate goal is to dynamically insert a "local" phone number in the "contact us" section based on the visitor's location.