Multitenant geolocation

As discussed earlier in this chapter, the Ti.GeoProviders framework provides a multiprovider approach to reverse geolocation. The multitenant component includes the ability for the Ti.GeoProviders framework to fail over, if a provider is unable to find a suitable location. This multitenant approach helps to ensure your geolocation functionality works for your globally mobile employees.

The following recipe demonstrates how to use the multitenant Ti.GeoProviders framework to perform reverse location lookups using a failover approach. The following screenshots illustrate this recipe running on both an iPhone and an Android device:

Multitenant geolocation

Getting ready

This recipe uses both CommonJS and native modules. These can be downloaded from the source code provided by the book, or individually through the links provided in the See also section at the end of this recipe. Simply copy the GeoProviders folder into the Resources folder of your project and then copy the modules folder into your project as highlighted in the following screenshot:

Getting ready

After copying the mentioned folders, you will need to click on your tiapp.xml file in Titanium Studio and add a reference to the bencoding.basicgeo module as shown in the following screenshot:

Getting ready

How to do it...

Once you have added native and CommonJS modules to your project, you next need to create your application namespaces in the app.js file and use require to import the module into your code as the following code snippet demonstrates:

//Create our application namespace
var my = {
  multiProvider : 
  require('./GeoProviders/reverse_multi_geoprovider')
};

Adding providers

The following code snippet demonstrates how to add different GeoProviders to the multiProvider module by calling the addProvider method. It is advised to add the GeoProviders that you feel will best meet your requirements first since the multiProvider will execute them in the order they are added.

my.multiProvider.addProvider({
  key : 'demo',
  providerString:'GeoProviders/geonames_geoprovider'
});

my.multiProvider.addProvider({
  providerString:'GeoProviders/bencoding_geoprovider'
});
my.multiProvider.addProvider({
  providerString:'GeoProviders/google_geoprovider'
});

Adding your purpose

Using location services on iOS requires a purpose or reason for the app to access the GPS. On the first request, this message will be presented to the user. The following code demonstrates how to add this purpose to the multitenant provider using the addPurpose method:

my.multiProvider.addPurpose('Demo of Geo Provider'),

Note

Android does not require the purpose be provided; in this case the purpose defined is not used when accessing the GPS.

Building the recipe UI

The following code snippet describes how to create the UI shown in this recipe's earlier screenshots. The first step is to create the Ti.UI.Window to which all visual elements will be attached.

var win = Ti.UI.createWindow({
  backgroundColor: '#fff', title: 'Multi-Tenant Geo', 
  barColor:'#000',fullscreen:false
});

Next, a Ti.Map.View is added to the Ti.UI.Window; this will be used to display a map pin with the device's current location and address details.

var mapView = Ti.Map.createView({
  top:120, bottom:0,width:Ti.UI.FILL,
  userLocation:false	
});
win.add(mapView);

Lookup helper methods

Now perform the following steps:

  1. The lookup object is designed to help format the results of the multitenant reverse geolocation component and present the address information in a graphical way to the recipe user.
    var lookup = {
  2. The addToMap method creates a map pin and adds the information to Ti.Map.View.
      addToMap: function(lat,lng,title){
        var pin = Ti.Map.createAnnotation({
        latitude:lat,longitude:lng,
        title:title	
      });
      mapView.addAnnotation(pin);
  3. A region is created using the map pin coordinates and the setLocation function of Ti.Map.View is then called. This will zoom the map to the coordinates of the recently added pin.
      var region = {latitude:lat,longitude:lng,
        latitudeDelta:0.04,   
        longitudeDelta:0.04};
        mapView.setLocation(region);
      },
  4. The onSuccess method is provided as the success callback when the getCurrentAddress method is called. The result of the getCurrentAddress method is provided to the e parameter.
      onSuccess : function(e){
        if(!e.found){
          alert("Unable to find your location");
          return;
        }
  5. The getProvider method is called to create a reference to the provider used to return the location results. This allows for the provider-specific generateAddress method to be used.
        var provider = my.multiProvider.getProvider(
        e.provider.name);
        var title = provider.generateAddress(e);
        lookup.addToMap(e.latitude,e.longitude,title);
      },
  6. The onError method is provided as the error callback when the getCurrentAddress method is called. Error details are provided to the e parameter.
      onError: function(e){
        alert("Error finding your location");
      }
    };

Performing a multitenant reverse geolocation lookup

Now perform the following steps:

  1. The final section of this recipe is to perform the multitenant lookup using the getCurrentAddress method of the multiProvider module.
    var findButton = Ti.UI.createButton({
      title:'Find Current Location', 
      left:10, right:10,top:70,height:40
    });
    win.add(findButton);
  2. The multiProvider lookup is performed on the click event of findButton.
    findButton.addEventListener('click',function(e){
  3. As a network connection is required for the reverse geolocation, the first step in the reverse geolocation process is to validate the network connection.
      if(!Ti.Network.online){
        alert("You must be online to run this recipe");
        return;
      }	
  4. Next, the getCurrentAddress method is called, and a success and error callback method is provided. The following code snippet demonstrates calling this method with the lookup.onSuccess and lookup.OnError callback methods discussed earlier in this recipe.
      my.multiProvider.getCurrentAddress(
      lookup.onSuccess,lookup.onError);
    });	
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.57.16