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:
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:
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:
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') };
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' });
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'),
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);
Now perform the following steps:
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 = {
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);
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); },
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; }
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); },
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"); } };
Now perform the following steps:
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);
multiProvider
lookup is performed on the click event of findButton
.findButton.addEventListener('click',function(e){
if(!Ti.Network.online){ alert("You must be online to run this recipe"); return; }
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); });
3.144.123.147