Building Mobile Enterprise Applications : webMethods Mobile Development Help : Code Snippets : Getting the Current GPS Position and Translating it into a Human-readable Location
Getting the Current GPS Position and Translating it into a Human-readable Location
Using Mobile Designer, it is possible to get the current GPS position. From a user experience perspective, it is often required to show a readable location to the user. This use case can be seen as complex example of how to chain multiple requests (see also Chaining Multiple Operations). You first have to get the current GPS location, and after this, you have to ask a common service to translate the coordinates into a readable location.
The first step is to access the GPS coordinates. It is recommended to use an AbstractThreadedOperation because this operation may take several seconds.
public class GetGPSCoordinates extends AbstractThreadedOperation {

protected void runInternal() throws Exception {
final LocationProvider lp = LocationProvider.getInstance(null);
final Location l = lp.getLocation(20);

final Coordinates c = l.getQualifiedCoordinates();

responseCode = 0;
if (c != null) {
result = (String.valueOf(c.getLatitude()) + "," String.valueOf(c
.getLongitude())).getBytes();
responseCode = HttpConnection.HTTP_OK;
}
}
}
For more information about the LocationProvider, see the Mobile Designer documentation.
The second step is to ask a common service provider for a readable location. This can be achieved, for example, using the public Google API:
http://maps.googleapis.com/maps/api/geocode/json
To execute this request, create a new RESTful service using the palette, enter the above URL and a name for the service class to be executed from Java. This request also requires a query parameter named latlng. In this example, the RESTful service to execute this request is named GetLocation. If GetGPSCoordinates results in HTTPConnection.HTTP_OK, you can trigger GetLocation with the result of the previous operation.
final GetLocation getLocation = new GetLocation();
getLocation.execute(operation.getResult());
Copyright © 2007-2017 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback