In the previous post I showed how additional GEO data could be loaded into your QlikView document based on IP address data already available.
This installment illustrates how additional GEO data can be loaded based just on place name data using the Yahoo! PlaceFinder API. The technique actually is almost identical, just needing to take into account the different XML schema returned from the PlaceFinder API which looks something like:
For the following request
http://where.yahooapis.com/geocode?q=Guatemala&appid=[yourappidhere]
You can then choose any of the additional data elements from the xml in your load script. For example in the following script I am loading in the latitude and longitude based on the place name:
((the 'latitude' argument in the above can be replaced with any of the XML element names in the Result element of the xml.)
In order for this to work, simply paste the code at the end of the post into the Macro module of your QlikView document, ensure JScript is selected as the 'Scripting Engine' and that the macro has system access.
You can also download the following (zipped QlikView) demo file which illustrates the technique:
Merging GEO Data from the Yahoo PlaceFinder API.zip (197.74 kb)
Don't forget that you should replace the APP_ID parameter in the JavaSctipt with your own which you can get from the Yahoo! site.
var
APP_ID = "SPECIFY_YOUR_APP_ID_HERE",
placesVsData = {},
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"),
xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
function getDataForPlace(place, key) {
if (placesVsData[place] == null) setUp(place);
if (placesVsData[place].Err) {
return placesVsData[place].Err;
}
else {
return placesVsData[place].Node.selectNodes(key)[0].text;
}
}
function setUp(place) {
xmlhttp.open("POST", "http://where.yahooapis.com/geocode?q=" + place + "&appid=" + APP_ID, false);
xmlhttp.send();
if (xmlhttp.status != "200") {
placesVsData[ip] = { Err: "Error (status = " + xmlhttp.status + ")" };
return;
}
else {
var res = xmlhttp.responseText;
if (!xmlDoc.loadXML(res)) {
placesVsData[ip] = { Err: "Error loading xml" };
}
else {
var status = xmlDoc.selectNodes("/ResultSet/Error")[0].text;
if (status == "0") {
placesVsData[place] = { Node: xmlDoc.selectNodes("/ResultSet/Result")[0] };
}
else {
placesVsData[place] = { Err: "Status = " + status };
}
}
}
}