Package com.mapmidlet.geocoding

Source Code of com.mapmidlet.geocoding.CloudmadeGeocoder

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* Author: Damian Waradzyn
*/
package com.mapmidlet.geocoding;

import java.io.IOException;
import java.util.Vector;

import org.json.me.*;

import com.mapmidlet.CloudGps;
import com.mapmidlet.misc.IOTool;
import com.mapmidlet.projection.WorldCoordinate;
import com.mapmidlet.tile.ui.ScreenMarker;

/**
* An geocoder implementation using CloudMade service.
*
* @author Damian Waradzyn
*/
public class CloudmadeGeocoder extends Geocoder {

    public Vector search(String location, int maxResults) {
        Vector results = new Vector();
        String url = "http://geocoding.cloudmade.com/e4b1777b4b5154d69dbfc4678216183a/geocoding/find/"
                + URLencode(location) + ".js?results=" + maxResults;
        try {
            JSONObject json = new JSONObject(IOTool.download(url));
            if (json.has("found")) {
                JSONArray array = json.getJSONArray("features");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject feature = array.getJSONObject(i);
                    ScreenMarker result = new ScreenMarker();
                    result.name = feature.getJSONObject("properties").getString("name");
                    JSONArray coordinates = feature.getJSONObject("centroid").getJSONArray("coordinates");
                    WorldCoordinate worldCoordinate = new WorldCoordinate();
                    worldCoordinate.latitude = coordinates.getDouble(0);
                    worldCoordinate.longitude = coordinates.getDouble(1);
                    result.worldCoordinate = worldCoordinate;
                    result.iconName = "search_result.png";
                    result.visible = true;
                    results.addElement(result);
                }
            }
        } catch (IOException e) {
            CloudGps.setError(e);
            return null;
        } catch (JSONException e) {
            CloudGps.setError(e);
            return null;
        }
        return results;
    }

    public static String URLencode(String s) {
        if (s != null) {
            StringBuffer tmp = new StringBuffer();
            int i = 0;
            while (i < s.length()) {
                int b = (int) s.charAt(i++);
                if ((b >= 0x30 && b <= 0x39) || (b >= 0x41 && b <= 0x5A) || (b >= 0x61 && b <= 0x7A)) {
                    tmp.append((char) b);
                } else {
                    tmp.append("%");
                    if (b <= 0xf)
                        tmp.append("0");
                    tmp.append(Integer.toHexString(b));
                }
            }
            return tmp.toString();
        }
        return null;
    }
}
TOP

Related Classes of com.mapmidlet.geocoding.CloudmadeGeocoder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.