public Place(JSONObject _place) throws JSONException { // e.g. {"id":"0a3e119020705b64","place_type":"city", // "bounding_box":{"type":"Polygon", // "coordinates":[[[-95.519568,37.303542],[-95.227853,37.303542],[-95.227853,37.383978],[-95.519568,37.383978]]]}, // "name":"Parsons","attributes":{}, // "country_code":"US", // "url":"http://api.twitter.com/1/geo/id/0a3e119020705b64.json", // "full_name":"Parsons, KS","country":"United States"} id = InternalUtils.jsonGet("id", _place); if (id == null) { // a Yahoo ID? id = InternalUtils.jsonGet("woeid", _place); // TODO Test Me! // TODO should we have a separate id field for Yahoo? } type = InternalUtils.jsonGet("place_type", _place); // name and full_name seem to be much the same, e.g. // "City of Edinburgh"? name = InternalUtils.jsonGet("full_name", _place); if (name == null) { name = InternalUtils.jsonGet("name", _place); } countryCode = InternalUtils.jsonGet("country_code", _place); country = InternalUtils.jsonGet("country", _place); // bounding box Object bbox = _place.opt("bounding_box"); if (bbox instanceof JSONObject) { this.boundingBox = parseCoords((JSONObject) bbox); } Object geo = _place.opt("geometry"); if (geo instanceof JSONObject) { this.geometry = parseCoords((JSONObject) geo); } }
private List<LatLong> parseCoords(JSONObject bbox) throws JSONException { JSONArray coords = bbox.getJSONArray("coordinates"); // pointless nesting? coords = coords.getJSONArray(0); List<LatLong> coordinates = new ArrayList(); for (int i = 0, n = coords.length(); i < n; i++) { // these are longitude, latitude pairs JSONArray pt = coords.getJSONArray(i); LatLong x = new LatLong(pt.getDouble(1), pt.getDouble(0)); coordinates.add(x); } return coordinates; }