Retrieve an address by using reverse geocoding

You can retrieve an address by invoking Locator.reverseGeocode(). When you invoke Locator.reverseGeocode(), you must specify the latitude and longitude coordinates by using two int fields, or by using a Coordinates object. The values for the coordinates are passed as decimal degrees, to five decimal places, with the values multiplied by 100,000. Your application must request reverse geocoding information outside of the event dispatch thread. The process uses the LBS Locate Server over the wireless radio or Wi-Fi®. A successful request for reverse 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 myReverseGeocode
    {
        public myReverseGeocode()
        {
        }
    }
  3. In the class, create a private variable of type Thread.
    private Thread reverseGeocode;
  4. In the constructor, create an instance of the Thread class. You cannot retrieve reverse geocoding information on the application's primary thread.
    reverseGeocode = new Thread(thread);
    reverseGeocode.setPriority(Thread.MIN_PRIORITY);
    reverseGeocode.start();
  5. In the class, create a Thread that invokes a public run() method. In run(), create an instance of the AddressInfo class. Create two int fields, and populate each field with the latitude and longitude values with five decimal accuracy multiplied by 100,000. Create a try/catch block. In this block, invoke reverseGeocode() to find the address and return the results in a Landmark array.
    Runnable thread = new Runnable()
    {
        public void run()
        {
            AddressInfo addrInfo = null;
    
            int latitude  = (int)(45.423488 * 100000);
            int longitude = (int)(-80.32480 * 100000);
    
            try
            {
                Landmark[] results = Locator.reverseGeocode
                  (latitude, longitude, Locator.ADDRESS );
    
                if ( results != null && results.length > 0 )
                    addrInfo = results[0].getAddressInfo();
            }
            catch ( LocatorException lex )
            {
            }
        }
    };
  6. Pass one of the following parameters to Locator.reverseGeocode():
    • Locator.ADDRESS: requests the nearest address or nearest street to the specified latitude/longitude
    • Locator.CITY: returns a value that is focused on the city level
    • Locator.COUNTRY: returns a value that is focused on the country level
    • Locator.PROVINCE_STATE: returns a value that is focused on the province or state level

Code sample: Retrieving an address by using reverse geocoding

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

public class myReverseGeocode
{
    private Thread reverseGeocode;

    public myReverseGeocode()
    {
        reverseGeocode = new Thread(thread);
        reverseGeocode.setPriority(Thread.MIN_PRIORITY);
        reverseGeocode.start();
    }

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

            int latitude  = (int)(45.423488 * 100000);
            int longitude = (int)(-80.32480 * 100000);

            try
            {
                Landmark[] results = Locator.reverseGeocode
                 (latitude, longitude, Locator.ADDRESS );

                if ( results != null && results.length > 0 )
                    addrInfo = results[0].getAddressInfo();
            }
            catch ( LocatorException lex )
            {
            }
        }
    };
}

Was this information helpful? Send us your comments.