IBM Support

How to develop location-aware apps in IBM MobileFirst/Worklight, Part 1?

Question & Answer


Question

How to develop location-aware apps in IBM MobileFirst/Worklight, Part 1?

Answer

On Demand Consulting
Author: Siwen Wang

IBM MobileFirst/Worklight supports for both basic and advanced needs of location-aware applications. In this blog post, Hisham Abdel-Hafez examines how you can develop a MobileFirst/Worklight application that addresses the basic needs.

Modern devices have the ability to get their current coordinates in a few different ways:
 
  • GPS - the most accurate way to get current coordinates, but it's slow, may need a clear sky view and consumes more of the device's battery
  • Mobile networks and WiFi - less accurate than GPS but much faster and have no special requirements other than connectivity

Getting current coordinates comes as part of HTML5 application programming interfaces (APIs) using the method geolocation.getCurrentPosition. If a device does not support this API, the Apache Cordova library bundled with IBM MobileFirst/Worklight implements it for you. The code snippet below shows how it can be used:
 
navigator.geolocation.getCurrentPosition(onLocationSuccess, onLocationError, geolocationOptions);
function onLocationSuccess (position) {
console.log("onLocationSuccess. Latitude is:" + 
    position.coords.latitude + " Longitude is:" + position.coords.longitude);
} 
function onLocationError (error) {
console.log("Error occured. Code is:" + error.code);
}
  • Since getting the current location is done with an asynchronouscall, it is always safest to set a timeout for it. You can do this by adding a timeout value to the options object.
  • To get all of this working on Android, you have to make some changes in the AndroidMainfest.xmlfile. Add the following permissions to the file in order for the Android operating system to give you access to the needed device resources.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
Geocoding and reverse geocoding
Geocoding is the transformation from human-readable addresses into longitude and latitude values. Reverse geocoding is the opposite of this transformation. To do geocoding and reverse geocoding, you will need to use an external location API. Many vendors offer such services now. We will use Google Maps APIs in this post.
To add Google Maps support to your IBM Worklight application, add this line to your main application HTML file:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script>
Both geocoding and reverse geocoding requests use the same object google.maps.Geocoder and the same function geocode. How the app will operate depends on what you pass in the request. If you pass the coordinates, then it will perform a reverse geocoding operation, and the response will be a human-readable address. If you pass an address string, then it will do a geocoding, and the response will be the coordinates along with a better-formatted address. So, first create the Geocoder object.
var geocoder = new google.maps.Geocoder();
Then, depending on what operation you are doing, construct the request object.
Geocoding
var request = {
  address : "Raleigh Durham airport"??? 
};
Reverse geocoding
var latlng = new google.maps.LatLng(31, 30);
  var request = {
      latLng : latlng 
  };
After that, call the geocode function. As usual, this is an asynchronous call, so you need to pass a callback function to be called when your data is ready.
geocoder.geocode(request, function(data, status){
  if(status = google.maps.GeocoderStatus.OK){
      if(data[0] != null){
           alert("longitude is: + data[0].geometry.location.lng() + " latitude is :" +   data[0].geometry.location.lat());
           alert("address is: " + data[0].formatted_address);
           } else {
                alert("No address available");
           }
     }
The results format is the same for geocoding and reverse geocoding operations, so parse it for the output data you need.
Conclusion
IBM MobileFirst/Worklight leverages open web standards that make building location-aware mobile applications easier than ever. This gives application owners a huge opportunity to reach out to users in very unique and unexpected ways.

You can refer to the original post for more info though the following link
Developing location aware apps in IBM MobileFirst/Worklight Part 1

[{"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Product":{"code":"SSQTW3","label":"IBM On Demand Consulting for Hybrid Cloud"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"","label":""}}]

Document Information

Modified date:
11 February 2019

UID

ibm10771903