Tutorial: Get Location

Introduction

This tutorial shows how to use the playground web service interface to obtain the location of a handset from a java client. The tutorial is based on the Terminal Location Parlay X web service, and illustrates how to create a java client invoking a web service. As a prerequisite we assume that you are familiar with the following technologies:

  • HTML
  • XML, SOAP and Web Services
  • Eclipse - as an environment for Java and web service development
  • Java, inclusive basic HTTP/URL handling
  • Geographical data / Coordinates

Follow the steps below to create a client which invokes the Terminal Location web service.

Place the cursor over this icon to expand screenshots from Eclipse.

  1. Download and install the Eclipse IDE (Web Tools Platform).
  2. Create a new project by selecting File | New | Java Project from the menu bar. This initiates the "New Java Project" wizard.
  3. Give the project a name, and choose your preferences on Java version and project organization. In this tutorial, we named the project "Get handset location". The default values are used for all other selections.
  4. Push "Finish" to continue and close the wizard. The project is created, and the next steps describe how to start adding the required content to your project.
  5. Next, download and store locally the file, playground.jar
  6. Add the library to your project. To do this, use the right mouse button on your project, and select "Properties", then click "Java Build Path" and "Add External JAR's" button, to select the JAR files. Click the OK button, when you are done.
  7. In addition to playground.jar you should add the following libraries to your project:
    • activation.jar
    • mail.jar
    • axis.jar - the axis binary download also includes the following required libraries:
      • commons-discovery-0.2.jar
      • commons-logging-1.0.4.jar
      • wsdl4j-1.5.1.jar
      • jaxrpc.jar
      • saaj.jar
  8. Add a new Java class to the project. Right click your project, and select "New -> Class" from the menu, to start the "New Java Class" wizard. Name the Java class, in this example "MyGetLocationDemo" and check for automatically generating the main method. Click "Finish" and the java class is generated.
  9. Finally, cut & paste from the source code below into your java class file

import java.net.URL;
import java.util.Calendar;

import org.apache.axis.client.Stub;
import org.apache.axis.types.URI;

import com.telenor.playground.toolbox.location.DelayTolerance;
import com.telenor.playground.toolbox.location.LocationInfo;
import com.telenor.playground.toolbox.location.ParlayXLocation_0_1V0_WS;
import com.telenor.playground.toolbox.location.ParlayXLocation_0_1V0_WSLocator;
import com.telenor.playground.toolbox.location.TerminalLocation;


public class MyGetLocationDemo {

 public MyGetLocationDemo(String _username,String _pwd, String _msisdn)
 {
  try{
   /**
    * Get the Location Web service 
    */
   ParlayXLocation_0_1V0_WS getLocationService = 
    new ParlayXLocation_0_1V0_WSLocator();
   String locationServiceEndpoint = 
    getLocationService.getTerminalLocationPortAddress();
   TerminalLocation terminalLocation = 
    getLocationService.getTerminalLocationPort(new URL(locationServiceEndpoint));
   /**
    * Prepare the parameters needed for getting location 
    * C = Country code
    * N = Phone Number
    */
   URI addr = new URI("tel:" + _msisdn);
   /**
    * Add the necessary authentication data
    */
   Stub stub = (Stub) terminalLocation;
   stub.setUsername(_username);
   stub.setPassword(_pwd);
   /**
    * Check the location if the terminal
    */
   LocationInfo li = terminalLocation.getLocation(addr, 
     1, 1, null, null, DelayTolerance.LowDelay);
   /**
    * Handle the output 
    */
   if(li != null){
    float lat = li.getLatitude();
    float lon = li.getLongitude();
    Calendar when = li.getTimestamp();

    System.out.println("Terminal: " + addr.getSchemeSpecificPart() 
      + " is at: (lat)" + lat 
      + " (lon)" + lon + " " 
      + when.getTime()
      + " accuracy: " + li.getAccuracy() 
      + " altitude: " + li.getAltitude());
   }  
   else System.out.println("Could not obtain location");
  }
  catch (Exception e)
  {
   // Handle your exceptions here
   e.printStackTrace();
  }
 }

 /**
  * Use your main method to initiate an instance of the class
  * and perform the logic placed in the constructor.
  * 
  * Constructor arguments (username, password) are received
  * in your welcome to the lab email. 
  * msisdn must be registered with playground in order for 
  * the location service to work
  */
 public static void main(String[] args) {
  new MyGetLocationDemo("username","password","msisdn");
 }
}