Retrieve geospatial coordindates for an address by using geocoding

You can retrieve geospatial coordinates by invoking Locator.geocode(). When you invoke Locator.geocode(), you must specify a street address by using an AddressInfo object or a String object of address information. Your application must request geocoding information outside of the event dispatch thread. A successful request for geocoding information will return an array of Landmark objects.
  1. Import the required classes.
    import net.rim.device.api.lbs.*;
    import javax.microedition.location.*;
  2. Create a class and a constructor.
    public class myGeocode
    {
        public myGeocode()
        {
        }
    }
  3. In the class, create a private variable of type Thread.
    private Thread geocoder;
  4. In the constructor, create an instance of the Thread class. You cannot retrieve geocoding information on the application's primary thread.
    geocoder = new Thread(thread);
    geocoder.setPriority(Thread.MIN_PRIORITY);
    geocoder.start();
  5. In the class, create a Thread that invokes a public run() method. In run(), create an instance of the AddressInfo class. Populate the object with address information. Create an instance of the Coordinates class by passing the latitude, longitude, and altitude values to the constructor to start the geocode search. In the following code sample, the positive northern latitude and negative western longitude of the Waterloo, Ontario region are used by the LBS Locate Server to perform the geocode search. Create a try/catch block. In this block, invoke geocode() to find the address and return the results in a Landmark array.
    Runnable thread = new Runnable()
    {
        public void run()
        {
            AddressInfo addrInfo = new AddressInfo();
    
            addrInfo.setField(AddressInfo.STREET, "455 Phillip Street");
            addrInfo.setField(AddressInfo.CITY, "Waterloo");
            addrInfo.setField(AddressInfo.STATE, "Ontario");
            addrInfo.setField(AddressInfo.POSTAL_CODE, "N2L 3X2");
            addrInfo.setField(AddressInfo.COUNTRY, "Canada");
    
            Coordinates coord = new Coordinates(43.28, -80.31, Float.NaN);
    
            try
            {
                Landmark[] results = Locator.geocode(addrInfo, coord);
            }
            catch ( LocatorException lex )
            {
            }
        }
    };

Code sample: Retrieving geospatial coordinates for an address by using geocoding

import net.rim.device.api.lbs.*;
import javax.microedition.location.*;

public class myGeocode
{
    private Thread geocoder;

    public myGeocode()
    {
        geocoder = new Thread(thread);
        geocoder.setPriority(Thread.MIN_PRIORITY);
        geocoder.start();
    }

    Runnable thread = new Runnable()
    {
        public void run()
        {
            AddressInfo addrInfo = new AddressInfo();

            addrInfo.setField(AddressInfo.STREET, "455 Phillip Street");
            addrInfo.setField(AddressInfo.CITY, "Waterloo");
            addrInfo.setField(AddressInfo.STATE, "Ontario");
            addrInfo.setField(AddressInfo.POSTAL_CODE, "N2L 3X2");
            addrInfo.setField(AddressInfo.COUNTRY, "Canada");

            Coordinates coord = new Coordinates(43.28, -80.31, Float.NaN);

            try
            {
                Landmark[] results = Locator.geocode(addrInfo, coord);
            }
            catch ( LocatorException lex )
            {
            }
        }
    };
}

Was this information helpful? Send us your comments.