Finding Nearby Resources with the Geominer API
Finding Nearby Resources with the Geominer API
Again, the first step is gathering resources with which to build your base. As stated previously, resources are actually geosocial network check-ins that have been collected from either Foursquare or Gowalla (depending on the game).
To find nearby resources, we will use the Geominer API resources search in combination with the Geolocation API that we looked at in Chapter 6. The Geominer resources search follows the naming convention for all web services in the Geominer suite. The following is the URL that we will be using for Moundz: http://api.geominer.net/v1/moundz/resources.
CHAPTER 10: Integrating with Social APIs
Each web service in Geominer follows the basic format of http://api.geominer.net/api-version/appid/webservice/method. The following list describes the details:
api-version relates to the version of the Geominer API that we are using. We are using version 1, so this becomes v1.
appid refers to the application that is currently accessing the Geominer services. The Moundz application has been configured to use the Moundz application ID, so this becomes moundz.
webservice refers to the particular group of tools that we are using in the Geominer toolbox. In this case, we are using the resources tools.
method is used to indicate the particular operation that we are executing with the tools that we are using. This particular section of the call can be omitted if we want to access the default operation for the webservice. In the case of the resources tools, this is a search operation, which is what we wish to use, so this section can be omitted.
We will now add some functionality to our MOUNDZ module to enable us to retrieve those resources through the Geominer API:
MOUNDZ = (function() { // initialize constants var DEFAULT_ZOOM = 8,
GEOMINER_MOUNDZ_URL = 'http://api.geominer.net/v1/moundz';
function findResources(callback) { // get the map center position var center = map.getCenter();
$.ajax({ url: GEOMINER_MOUNDZ_URL + '/resources', dataType: 'jsonp', data: {
lat: center.lat(), lng: center.lng()
}, success: function(data) {
processResourceSearch(data); if (callback) {
callback(); } // if } });
} // findResources
var module = {
CHAPTER 10: Integrating with Social APIs
addMarker: addMarker, clearMarkers: clearMarkers,
findResources: findResources,
return module; })();
The preceding code shows the modifications required. We are adding a findResources function to our MOUNDZ module that will find resources based on the current map center position. The majority of this function is a jQuery Ajax call using JSONP, as we used earlier to access the Geonames data when first getting an understanding of JSONP. When we receive successful results from Geominer, these are passed to the processResourceSearch function. Let’s have a look at this function and its associated code now.
function markResources(resourceType, deposits) { for (var ii = 0; ii < deposits.length; ii++) { // add the marker for the resource deposit addMarker(
new google.maps.LatLng(deposits[ii].lat, deposits[ii].lng), deposits[ii].name, '<div class="resinfo">' + deposits[ii].total + ' resources at this location</div>');
} // for } // markResources
function processResourceSearch(data) { // clear any existing markers clearMarkers();
// iterate through the resource types and pin for (var ii = 0; ii < data.resourceTypes.length; ii++) {
var resType = data.resourceTypes[ii];
// mark the resources markResources(resType.typeName, resType.deposits);
} // for } // processResourceSearch
The two functions in this code are included in the MOUNDZ module as internal functions. As you saw in the findResources function, processResourceSearch is called when we have received a successful response from the Geominer API. When this happens, existing markers are first removed, and then new markers are added for each of the different resource types that have been retrieved from the API.
In the case of Moundz, we have only one type of resource for the sake of simplicity, but we have implemented a for loop here to enable us to support additional resource types with relative ease if that is something that is desired at a later stage.
For each of the resource types, the markResources function is called to display markers on the map as per the boilerplate mapping application that we explored in Chapter 8.
CHAPTER 10: Integrating with Social APIs
All we need to do now is to adjust the index.html file to make the call to the findResources function of the MOUNDZ module when the page is loaded:
<script type="text/javascript"> function initialize() {
var latlng = new google.maps.LatLng(-33.866, 151.209);
MOUNDZ.init(latlng, 17);
MOUNDZ.findResources(function() { MOUNDZ.updateDisplay(); });
} // initialize </script>
For the moment, we will leave out the location-detection part just to check that everything operates correctly. This leaves us having to make only a very simple change to our initialize function (shown in bold). Additionally, we increase the zoom level to
17 from 13, which is more appropriate given the results we will get back from Geominer (courtesy of Gowalla in this initial case). Figure 10–9 shows an example of what we should see if everything has gone according to plan.
Figure 10–9. For Moundz, the Geominer API returns a list of Gowalla spots near the center of the map. As with our earlier work with the boilerplate mapping application, the resource locations
can be selected either by tapping a marker or by navigating using the left and right navigation arrows. Additionally, more detail for that location can be displayed by tapping the title of the location as before. At this stage, we have included some simple debug information about the number of resources available. Figure 10–10 shows the example debug information that we have included for the moment.
CHAPTER 10: Integrating with Social APIs
Figure 10–10. We will expand the resource information screen to include actions relating to the resource.