Monday, January 17, 2011

Map Services -- Reverse Geocoding

Reverse Geocoding is to find a detail address name based on its latitude and longitude. The following example is to find what’s the address locate in -31.948275, 115.857562. which is "109-123 James St, Northbridge WA 6003, Australia"

public class MapReverseGeocodingMIDP extends MapDemoMIDP implements CommandListener,
        IReverseGeocodingListener {

    private Command mapFindAddressCommand = new Command("Find Address", Command.OK, 1);

    public void startApp() {

        init();
        canvas.addCommand(mapFindAddressCommand);
        map.setReverseGeocodingListener(this);
        canvas.setCommandListener(this);
        GeoLatLng center = new GeoLatLng(-31.948275, 115.857562);
        map.setCenter(center, 13, MapType.GOOGLEMAP);        Display.getDisplay(this).setCurrent(canvas);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == mapFindAddressCommand) {
            map.getReverseLocations("-31.948275,115.857562");
        }
    }

    public void done(String arg0, MapPoint[] result) {
        if (result != null) {
            map.panTo(result[0].getPoint());
        }
    }
}





Remember the string format, latitude comes first, and don’t put space in between. The result is also returned as an array. Normaly only the first result is most appropriate, the rest is for bigger area, i.e the city, the state and then the country the address is in.

No comments:

Post a Comment