Monday, January 17, 2011

Map Services -- IP Search

You can also search address based on an IP address, Guidebee Map API also provide methods to query latitude, longitude base on a IP address.
For example, the following example search for IP 58.192.32.1, and the return latitude, longitude is 118.777802, 32.061699, which is Nanjing University in China.
public class MapIpSearchMIDP extends MapDemoMIDP implements CommandListener,
        IIpAddressGeocodingListener {

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

    public void startApp() {

        init();
        canvas.addCommand(mapFindAddressCommand);
        map.setIpAddressGeocodingListener(this);
        canvas.setCommandListener(this);
        GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
        map.setCenter(center, 15, MapType.MICROSOFTCHINA);
        Display.getDisplay(this).setCurrent(canvas);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == mapFindAddressCommand) {
            map.getIpLocations("58.192.32.1");
        }
    }

    public void done(String query, IpAddressLocation result) {
        if (result != null && result.error.length() == 0 && result.longitude.length() > 0
                && result.longitude.length() > 0) {
            try {

                MapPoint mapPoint = new MapPoint();
                String latLng = "[" + result.longitude + "," + result.latitude + ",0]";
                mapPoint.point = DigitalMap.fromStringToLatLng(latLng);
                mapPoint.setName(result.organization);
                mapPoint.setNote(result.city + " " + result.country);
                map.panTo(mapPoint.point);
            } catch (Exception e) {

                result.error = "IP_NOT_FOUND";
            }
        }
    }
}


Note: the result is always returned in English. A more detail information for above IP address is as below:
ISP"China Education and Research Network"
Organization: "Nan Jing University"
Country: "CN"
City: "Nanjing"
You can also use “127.0.0.1” to query local address location.

No comments:

Post a Comment