Code example for Address

Methods: setAddressLine, setAdminArea, setExtras, setFeatureName, setLatitude, setLocality, setLongitude, setSubAdminArea

0
				Log.e(TAG, "Geocoder API response status not ok -- status=" + status);
				throw new RoutyException("There was a problem understanding an address."); 
			} else {  
				// TODO status is ok - parse the XML into an Address object 
				int lineNumber = 0;
				Address result = new Address(Locale.getDefault());
				 
				expr = "/GeocodeResponse/result";
				NodeList results = (NodeList) xpath.evaluate(expr, root, XPathConstants.NODESET);
				if (results.getLength() == 0) {
					return null; 
				} 
				 
				// GET THE PLACE NAME (ESTABLISHMENT) IF THERE IS ONE (EX. "UNIVERSITY OF TEXAS") 
				expr = "/GeocodeResponse/result[1]/address_component[type=\"establishment\"]/long_name";
				String establishment = (String) xpath.evaluate(expr, root, XPathConstants.STRING);
				result.setFeatureName(establishment);
//				Log.v(TAG, "establishment=" + establishment); 
				 
				// GET THE STREET NUMBER AND NAME 
				expr = "/GeocodeResponse/result[1]/address_component[type=\"street_number\"]/long_name";
				String streetNumber = (String) xpath.evaluate(expr, root, XPathConstants.STRING);
//				Log.v(TAG, "street number=" + streetNumber); 
				 
				expr = "/GeocodeResponse/result[1]/address_component[type=\"route\"]/long_name";
				String streetName = (String) xpath.evaluate(expr, root, XPathConstants.STRING);
//				Log.v(TAG, "street name=" + streetName); 
				 
				result.setAddressLine(lineNumber, (streetNumber==null?"":streetNumber) + (streetName==null?"":(" " + streetName)));
				lineNumber++;
				 
				 
				StringBuffer line1 = new StringBuffer();
					 
				// GET THE CITY, STATE AND ZIPCODE 
				expr = "/GeocodeResponse/result[1]/address_component[type=\"locality\"]/long_name";
				String cityName = (String) xpath.evaluate(expr, root, XPathConstants.STRING);
				if (cityName != null) {
					result.setLocality(cityName);
					 
					line1.append(cityName);
					line1.append(", ");
				} 
				 
				expr = "/GeocodeResponse/result[1]/address_component[type=\"administrative_area_level_1\"]/long_name";
				String stateName = (String) xpath.evaluate(expr, root, XPathConstants.STRING);
				if (stateName != null) {
					result.setAdminArea(stateName);
				} 
				 
				expr = "/GeocodeResponse/result[1]/address_component[type=\"administrative_area_level_1\"]/short_name";
				String stateAbbr = (String) xpath.evaluate(expr, root, XPathConstants.STRING);
				if (stateAbbr != null) {
					result.setSubAdminArea(stateAbbr);
					 
					line1.append(stateAbbr);
					line1.append(", ");
				} 
				 
				expr = "/GeocodeResponse/result[1]/address_component[type=\"postal_code\"]/long_name";
				String zipcode = (String) xpath.evaluate(expr, root, XPathConstants.STRING);
				if (zipcode != null) {
					result.setAdminArea(zipcode);
					 
					line1.append(zipcode);
				} 
				 
				result.setAddressLine(lineNumber, line1.toString());
				lineNumber++;
				 
				 
				// GET THE COORDINATE INFORMATION 
				expr = "/GeocodeResponse/result[1]/geometry/location/lat";
				String latitude = (String) xpath.evaluate(expr, root, XPathConstants.STRING);
				if (latitude != null && latitude.length() > 0) {
					result.setLatitude(Double.parseDouble(latitude));
				} 
				 
				expr = "/GeocodeResponse/result[1]/geometry/location/lng";
				String longitude = (String) xpath.evaluate(expr, root, XPathConstants.STRING);
				if (longitude != null && longitude.length() > 0) {
					result.setLongitude(Double.parseDouble(longitude));
				} 
				 
				 
				// GET THE FORMATTED ADDRESS 
				expr = "/GeocoderResponse/result[1]/formatted_address";
				String formattedAddress = (String) xpath.evaluate(expr, root, XPathConstants.STRING);
				Bundle extras = new Bundle();
				extras.putString("formatted_address", formattedAddress);
				result.setExtras(extras);
				 
				Log.v(TAG, "Done parsing XML.");
				return result;
			}