Location picker

Use a location picker to permit BlackBerry® device users to select a location from a list of options that you define.

Class LocationPicker
Supported since BlackBerry® Java® SDK 5.0
Example This screen shows a location picker.

For more information about location pickers, see the UI Guidelines.

Code sample: Creating a location picker

import net.rim.blackberry.api.invoke.*;
import net.rim.device.api.gps.*;
import net.rim.device.api.lbs.picker.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import javax.microedition.location.*;
import java.util.Enumeration;


public class LocationPickerApp extends UiApplication
{
    public static void main(String[] args)
    {
        LocationPickerApp app = new LocationPickerApp();
        app.enterEventDispatcher();        
    }

    public LocationPickerApp()
    {
        pushScreen(new LocationPickerAppScreen());
    }
    
    static class LocationPickerAppScreen extends MainScreen implements LocationPicker.Listener, FieldChangeListener 
    { 
        private LocationPicker _locationPicker;
        private ButtonField _buttonField;
        private LabelField _nameLabel;                
        private LabelField _descLabel;
        private LabelField _coordLabel;
        private boolean _mapsPresent = false;
        
        LocationPickerAppScreen()
        {
            setTitle("Location Picker Demo");
            _buttonField = new ButtonField("Choose location", ButtonField.NEVER_DIRTY);
            _buttonField.setChangeListener(this);
            add(_buttonField);
            _nameLabel = new LabelField();
            _descLabel = new LabelField();
            _coordLabel = new LabelField();
            add(_nameLabel);              
            add(_descLabel);
            add(_coordLabel);            
            
            Landmark[] landmarks = new Landmark[] {new Landmark("New York", "Times Square", new QualifiedCoordinates(40.757682, -73.98571, Float.NaN, Float.NaN, Float.NaN), null), new Landmark("New York","Central Park", new QualifiedCoordinates(40.783333, -73.966667, Float.NaN, Float.NaN, Float.NaN), null)}; 
                          
            int arraySize = 7; 
            
            LocationPicker.Picker mapsLocationPicker = null;   
            try       
            {
                mapsLocationPicker = MapsLocationPicker.getInstance();       
                _mapsPresent = true;
            }
            catch(IllegalStateException ise)
            {
                arraySize--;
            }
            
            boolean gpsSupported = GPSInfo.getGPSDataSource() != null;
            if(!gpsSupported)
            {
                arraySize--;
            }                   
            
            LocationPicker.Picker[] locationPickersArray = new LocationPicker.Picker[arraySize];            
            locationPickersArray[--arraySize] = EnterLocationPicker.getInstance(false);
            locationPickersArray[--arraySize] = SuggestedLocationPicker.getInstance("App specific...", landmarks );            
            locationPickersArray[--arraySize] = RecentLocationPicker.getInstance();                      
            locationPickersArray[--arraySize] = ContactsLocationPicker.getInstance(false);      
            locationPickersArray[--arraySize] = GeotaggedPhotoPicker.getInstance();
            
            if(_mapsPresent)
            {
                locationPickersArray[--arraySize] = mapsLocationPicker;
            }
            
            if(gpsSupported)
            {
                locationPickersArray[--arraySize] = GPSLocationPicker.getInstance();
            }          
            
            _locationPicker = LocationPicker.getInstance(locationPickersArray);
            Enumeration globalPickers = _locationPicker.getGlobalLocationPickers();
            
            while (globalPickers.hasMoreElements())
            {
                _locationPicker.addLocationPicker((LocationPicker.Picker)globalPickers.nextElement());
            }
            
           _locationPicker.setListener(this);     
        }       
              
        public void locationPicked (LocationPicker.Picker picker, Landmark location)
        {
            if(location != null)
            {
                _nameLabel.setText("Location name: " + location.getName());
                _descLabel.setText("Description: " + location.getDescription());
                
                QualifiedCoordinates coordinates = location.getQualifiedCoordinates();
                if(coordinates != null)
                {
                    StringBuffer buff = new StringBuffer("Coordinates: ");
                    double latitude = coordinates.getLatitude();
                    double longitude = coordinates.getLongitude();
                    buff.append("Latitude:");
                    buff.append(latitude);
                    buff.append(", Longitude: ");
                    buff.append(longitude);                        
                    _coordLabel.setText(buff.toString());                    
                }
                
                if(_mapsPresent)
                {
                    Landmark[] landmark = {location};
                    MapsArguments mapsArgs = new MapsArguments(landmark);
                    Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, mapsArgs);   
                }
            }
        }
        
        public void fieldChanged(Field field, int context)
        {
            if(field == _buttonField)
            {
                _locationPicker.show();                            
            }                        
        }  
    }
}
Next topic: Tree field
Previous topic: Spin box

Was this information helpful? Send us your comments.