List of usage examples for android.location Address Address
public Address(Locale locale)
From source file:no.ntnu.idi.socialhitchhiking.map.GeoHelper.java
/** * Gets a list of addresses from a set of coordinates * @param lat/*w w w. ja va2 s . c o m*/ * @param lon * @param maxResults * @return */ public static List<Address> getAddressesFromLocation(double lat, double lon, int maxResults) { JSONArray responseArray = getJSONArrayFromLocation(lat, lon, maxResults); List<Address> addresses = new ArrayList<Address>(); JSONObject jsonObj; for (int i = 0; i < responseArray.length(); i++) { Address address = new Address(Locale.getDefault()); String addressLine; try { jsonObj = responseArray.getJSONObject(i); addressLine = jsonObj.getString("formatted_address"); if (addressLine.contains(",")) { String[] lines = addressLine.split(","); for (int j = 0; j < lines.length; j++) { address.setAddressLine(j, lines[j].trim()); } } addresses.add(address); } catch (JSONException e) { continue; } } return addresses; }
From source file:grandroid.geo.Geocoder.java
public static List<Address> getFromLocation(Locale locale, double lat, double lng, int maxResult) { String language = locale.getLanguage(); if (locale == Locale.TAIWAN) { language = "zh-TW"; }// www . jav a 2 s. c o m String address = String.format(locale, "http://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=false&language=" + language, lat, lng);//locale.getCountry() HttpGet httpGet = new HttpGet(address); HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(AllClientPNames.USER_AGENT, "Mozilla/5.0 (Java) Gecko/20081007 java-geocoder"); //client.getParams().setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, 5 * 1000); //client.getParams().setIntParameter(AllClientPNames.SO_TIMEOUT, 25 * 1000); HttpResponse response; List<Address> retList = null; try { response = client.execute(httpGet); HttpEntity entity = response.getEntity(); String json = EntityUtils.toString(entity, "UTF-8"); JSONObject jsonObject = new JSONObject(json); retList = new ArrayList<Address>(); if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) { JSONArray results = jsonObject.getJSONArray("results"); if (results.length() > 0) { for (int i = 0; i < results.length() && i < maxResult; i++) { JSONObject result = results.getJSONObject(i); //Log.e(MyGeocoder.class.getName(), result.toString()); Address addr = new Address(Locale.getDefault()); // addr.setAddressLine(0, result.getString("formatted_address")); JSONArray components = result.getJSONArray("address_components"); String streetNumber = ""; String route = ""; for (int a = 0; a < components.length(); a++) { JSONObject component = components.getJSONObject(a); JSONArray types = component.getJSONArray("types"); for (int j = 0; j < types.length(); j++) { String type = types.getString(j); if (type.equals("locality") || type.equals("administrative_area_level_3")) { addr.setLocality(component.getString("long_name")); } else if (type.equals("street_number")) { streetNumber = component.getString("long_name"); } else if (type.equals("route")) { route = component.getString("long_name"); } else if (type.equals("administrative_area_level_1")) { addr.setAdminArea(component.getString("long_name")); } else if (type.equals("country")) { addr.setCountryName(component.getString("long_name")); addr.setCountryCode(component.getString("short_name")); } } } addr.setAddressLine(0, route + " " + streetNumber); if (result.has("formatted_address")) { addr.setFeatureName(result.getString("formatted_address")); } addr.setLatitude( result.getJSONObject("geometry").getJSONObject("location").getDouble("lat")); addr.setLongitude( result.getJSONObject("geometry").getJSONObject("location").getDouble("lng")); if (addr.getAdminArea() == null) { addr.setAdminArea(""); } retList.add(addr); } } } } catch (ClientProtocolException e) { Log.e("grandroid", "Error calling Google geocode webservice.", e); } catch (IOException e) { Log.e("grandroid", "Error calling Google geocode webservice.", e); } catch (JSONException e) { Log.e("grandroid", "Error parsing Google geocode webservice response.", e); } return retList; }
From source file:org.microg.networklocation.backends.mapquest.NominatimGeocodeSource.java
private Address parseResponse(Locale locale, JSONObject result) throws JSONException { if (!result.has(WIRE_LATITUDE) || !result.has(WIRE_LONGITUDE) || !result.has(WIRE_ADDRESS)) { return null; }/*from ww w . jav a 2 s .c o m*/ Address address = new Address(locale); address.setLatitude(result.getDouble(WIRE_LATITUDE)); address.setLatitude(result.getDouble(WIRE_LONGITUDE)); int line = 0; JSONObject a = result.getJSONObject(WIRE_ADDRESS); if (a.has(WIRE_THOROUGHFARE)) { address.setAddressLine(line++, a.getString(WIRE_THOROUGHFARE)); address.setThoroughfare(a.getString(WIRE_THOROUGHFARE)); } if (a.has(WIRE_SUBLOCALITY)) { address.setSubLocality(a.getString(WIRE_SUBLOCALITY)); } if (a.has(WIRE_POSTALCODE)) { address.setAddressLine(line++, a.getString(WIRE_POSTALCODE)); address.setPostalCode(a.getString(WIRE_POSTALCODE)); } if (a.has(WIRE_LOCALITY_CITY)) { address.setAddressLine(line++, a.getString(WIRE_LOCALITY_CITY)); address.setLocality(a.getString(WIRE_LOCALITY_CITY)); } else if (a.has(WIRE_LOCALITY_TOWN)) { address.setAddressLine(line++, a.getString(WIRE_LOCALITY_TOWN)); address.setLocality(a.getString(WIRE_LOCALITY_TOWN)); } else if (a.has(WIRE_LOCALITY_VILLAGE)) { address.setAddressLine(line++, a.getString(WIRE_LOCALITY_VILLAGE)); address.setLocality(a.getString(WIRE_LOCALITY_VILLAGE)); } if (a.has(WIRE_SUBADMINAREA)) { address.setAddressLine(line++, a.getString(WIRE_SUBADMINAREA)); address.setSubAdminArea(a.getString(WIRE_SUBADMINAREA)); } if (a.has(WIRE_ADMINAREA)) { address.setAddressLine(line++, a.getString(WIRE_ADMINAREA)); address.setAdminArea(a.getString(WIRE_ADMINAREA)); } if (a.has(WIRE_COUNTRYNAME)) { address.setAddressLine(line++, a.getString(WIRE_COUNTRYNAME)); address.setCountryName(a.getString(WIRE_COUNTRYNAME)); } if (a.has(WIRE_COUNTRYCODE)) { address.setCountryCode(a.getString(WIRE_COUNTRYCODE)); } return address; }
From source file:no.ntnu.idi.socialhitchhiking.map.GeoHelper.java
/** * Retrieves a {@link List} of addresses that match the given {@link GeoPoint}. * The first element in the list has the best match (but is not guaranteed to be correct). <br><br> * /* w w w . ja v a2s. co m*/ * This method tries to use the {@link Geocoder} to transform a (latitude, longitude) * coordinate into addresses, and if this fails (witch it most likely will under emulation), it * tries to use a method from the {@link GeoHelper}-class. * * @param location The location that is transformed into a list of addresses * @param maxResults The maximum number of addresses to retrieve (should be small). * @param maxAddressLines The maximum number of lines in the addresses. This should be high if you want a complete address! If it is smaller than the total number of lines in the address, it cuts off the last part...) * @return Returns the {@link List} of addresses (as {@link String}s). */ public static List<String> getAddressesAtPoint(final GeoPoint location, final int maxResults, int maxAddressLines) { List<String> addressList = new ArrayList<String>(); List<Address> possibleAddresses = new ArrayList<Address>(); Address address = new Address(Locale.getDefault()); String addressString = "Could not find the address..."; ExecutorService executor = Executors.newSingleThreadExecutor(); Callable<List<Address>> callable = new Callable<List<Address>>() { @Override public List<Address> call() throws IOException { return fancyGeocoder.getFromLocation(location.getLatitudeE6() / 1E6, location.getLongitudeE6() / 1E6, maxResults); } }; Future<List<Address>> future = executor.submit(callable); try { possibleAddresses = future.get(); } catch (InterruptedException e1) { possibleAddresses = GeoHelper.getAddressesFromLocation(location.getLatitudeE6() / 1E6, location.getLongitudeE6() / 1E6, maxResults); } catch (ExecutionException e1) { possibleAddresses = GeoHelper.getAddressesFromLocation(location.getLatitudeE6() / 1E6, location.getLongitudeE6() / 1E6, maxResults); } executor.shutdown(); if (possibleAddresses.size() > 0) { for (int i = 0; i < possibleAddresses.size(); i++) { addressString = ""; address = possibleAddresses.get(i); for (int j = 0; j <= address.getMaxAddressLineIndex() && j <= maxAddressLines; j++) { addressString += address.getAddressLine(j); addressString += "\n"; } addressList.add(addressString.trim()); } } return addressList; }
From source file:eu.trentorise.smartcampus.trentinofamiglia.fragments.track.TrackDetailsFragment.java
@Override public void onStart() { super.onStart(); if (getTrack() != null) { ImageView certifiedIcon = (ImageView) this.getView().findViewById(R.id.track_details_icon); Drawable icon = null;//ww w. j a v a 2 s. co m certifiedIcon .setImageDrawable(getResources().getDrawable(CategoryHelper.getIconByType(mTrack.getType()))); // title TextView tv = (TextView) this.getView().findViewById(R.id.track_details_title); tv.setText(mTrack.getTitle()); /* * BUTTONS */ // map ImageButton mapBtn = (ImageButton) getView().findViewById(R.id.trackdetails_map); mapBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ArrayList<BaseDTObject> list = new ArrayList<BaseDTObject>(); list.add(mTrack); MapManager.switchToMapView(list, mFragment); } }); // directions ImageButton directionsBtn = (ImageButton) getView().findViewById(R.id.trackdetails_directions); directionsBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Address to = Utils.getTrackAsGoogleAddress(mTrack); Address from = null; GeoPoint mylocation = MapManager.requestMyLocation(getActivity()); if (mylocation != null) { from = new Address(Locale.getDefault()); from.setLatitude(mylocation.getLatitudeE6() / 1E6); from.setLongitude(mylocation.getLongitudeE6() / 1E6); } DTHelper.bringmethere(getActivity(), from, to); } }); /* * END BUTTONS */ // description, optional tv = (TextView) this.getView().findViewById(R.id.track_details_descr); String customDescr = mTrack.customDescription(getActivity()); if (customDescr != null && customDescr.length() > 0) { tv.setText(Html.fromHtml(customDescr)); } else { ((LinearLayout) this.getView().findViewById(R.id.trackdetails)).removeView(tv); } commentsHandler = new CommentsHandler(getTrack(), getActivity(), getView()); } }
From source file:com.hoccer.api.android.LinccLocationManager.java
public Address getAddress(Location location) throws IOException { if (location == null) { return new Address(null); }/*w w w . ja v a 2 s . c om*/ Geocoder gc = new Geocoder(mContext); Address address = null; List<Address> addresses = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (addresses.size() > 0) { address = addresses.get(0); } return address; }
From source file:com.ternup.caddisfly.fragment.FormFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_form, container, false); /*/* w w w. ja va2 s.c o m*/ TextView textView = (TextView) rootView.findViewById(R.id.section_label); textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))); */ LinearLayout linearLayout = (LinearLayout) rootView.findViewById(R.id.rootLayout); //mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); textWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { } @Override public void afterTextChanged(Editable editable) { if (mAddress == null) { MainApp mainApp = (MainApp) getActivity().getApplicationContext(); mainApp.address = new Address(Locale.getDefault()); mAddress = mainApp.address; } mAddress.setFeatureName(mPlaceEditText.getEditText().getText().toString()); } }; mPlaceEditText = new FormEditText(getActivity(), getString(R.string.place)); mPlaceEditText.getEditText().requestFocus(); linearLayout.addView(mPlaceEditText.getView()); mPlaceEditText.getEditText().addTextChangedListener(textWatcher); mThoroughfareEditText = new FormEditText(getActivity(), getString(R.string.street)); linearLayout.addView(mThoroughfareEditText.getView()); mSubLocalityEditText = new FormEditText(getActivity(), getString(R.string.town)); linearLayout.addView(mSubLocalityEditText.getView()); mLocalityEditText = new FormEditText(getActivity(), getString(R.string.city)); linearLayout.addView(mLocalityEditText.getView()); mStateEditText = new FormEditText(getActivity(), getString(R.string.state)); linearLayout.addView(mStateEditText.getView()); mCountryEditText = new FormEditText(getActivity(), getString(R.string.country)); mCountryEditText.getEditText().setImeActionLabel(getString(R.string.done), EditorInfo.IME_ACTION_DONE); linearLayout.addView(mCountryEditText.getView()); mCountryEditText.getEditText().setOnEditorActionListener(this); return rootView; }
From source file:dev.application.taxivip.helpers.LocationUtils.java
public static List<Address> getStringFromLocation(double lat, double lng) throws ClientProtocolException, IOException, JSONException { String address = String.format(Locale.getDefault(), "http://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=true&components=country:CO®ion=co&language=" + Locale.getDefault().getCountry(), lat, lng);/*www. j a v a 2 s . c o m*/ HttpGet httpGet = new HttpGet(address); HttpClient client = new DefaultHttpClient(); HttpResponse response; StringBuilder stringBuilder = new StringBuilder(); List<Address> retList = null; response = client.execute(httpGet); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); int b; while ((b = stream.read()) != -1) { stringBuilder.append((char) b); } JSONObject jsonObject = new JSONObject(); jsonObject = new JSONObject(stringBuilder.toString()); retList = new ArrayList<Address>(); if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) { JSONArray results = jsonObject.getJSONArray("results"); for (int i = 0; i < results.length(); i++) { JSONObject result = results.getJSONObject(i); String indiStr = result.getString("formatted_address"); Address addr = new Address(Locale.getDefault()); addr.setAddressLine(0, indiStr); retList.add(addr); } } return retList; }
From source file:edu.usf.cutr.opentripplanner.android.tasks.OTPGeocoding.java
protected Long doInBackground(String... reqs) { long count = reqs.length; String address = reqs[0];/*from ww w . j av a2 s .c om*/ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); if (address == null || address.equalsIgnoreCase("")) { return count; } if (address.equalsIgnoreCase(context.getString(R.string.my_location))) { String currentLat = reqs[1]; String currentLng = reqs[2]; LatLng latLng = new LatLng(Double.parseDouble(currentLat), Double.parseDouble(currentLng)); Address addressReturn = new Address(context.getResources().getConfiguration().locale); addressReturn.setLatitude(latLng.latitude); addressReturn.setLongitude(latLng.longitude); addressReturn.setAddressLine(addressReturn.getMaxAddressLineIndex() + 1, context.getString(R.string.my_location)); addressesReturn.add(addressReturn); return count; } ArrayList<Address> addresses = null; if (prefs.getBoolean(OTPApp.PREFERENCE_KEY_USE_ANDROID_GEOCODER, true)) { Log.d("test", "create a geocoder"); Geocoder gc = new Geocoder(context); try { if (selectedServer != null) { addresses = (ArrayList<Address>) gc.getFromLocationName(address, context.getResources().getInteger(R.integer.geocoder_max_results), selectedServer.getLowerLeftLatitude(), selectedServer.getLowerLeftLongitude(), selectedServer.getUpperRightLatitude(), selectedServer.getUpperRightLongitude()); } else { addresses = (ArrayList<Address>) gc.getFromLocationName(address, context.getResources().getInteger(R.integer.geocoder_max_results)); } } catch (IOException e) { e.printStackTrace(); } } addresses = filterAddressesBBox(addresses); if ((addresses == null) || addresses.isEmpty()) { addresses = searchPlaces(address); for (int i = 0; i < addresses.size(); i++) { Address addr = addresses.get(i); String str = addr.getAddressLine(0); List<String> addrLines = Arrays.asList(str.split(", ")); for (int j = 0; j < addrLines.size(); j++) { addr.setAddressLine(j, addrLines.get(j)); } } } addresses = filterAddressesBBox(addresses); addressesReturn.addAll(addresses); return count; }
From source file:eu.trentorise.smartcampus.trentinofamiglia.fragments.poi.PoiDetailsFragment.java
@Override public void onStart() { super.onStart(); if (getPOI() != null) { ImageView certifiedIcon = (ImageView) this.getView().findViewById(R.id.poi_details_icon); ImageView bannerCertifiedIcon = (ImageView) this.getView().findViewById(R.id.banner_certified); Drawable icon = null;// w w w. jav a2 s . co m if (CategoryHelper.CAT_POI_FAMILY_IN_TRENTINO.equals(mPoi.getType())) { icon = POIHelper.getDrawablePoiFamilyTrentinoDetail(getActivity(), mPoi); bannerCertifiedIcon.setVisibility(View.VISIBLE); } else if (CategoryHelper.CAT_POI_FAMILY_AUDIT.equals(mPoi.getType())) { icon = POIHelper.getDrawablePoiFamilyAuditDetail(getActivity(), mPoi); bannerCertifiedIcon.setVisibility(View.VISIBLE); } certifiedIcon.setImageDrawable(icon); // title TextView tv = (TextView) this.getView().findViewById(R.id.poi_details_title); tv.setText(mPoi.getTitle()); /* * BUTTONS */ // map ImageButton mapBtn = (ImageButton) getView().findViewById(R.id.poidetails_map); mapBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ArrayList<BaseDTObject> list = new ArrayList<BaseDTObject>(); list.add(mPoi); MapManager.switchToMapView(list, mFragment); } }); // directions ImageButton directionsBtn = (ImageButton) getView().findViewById(R.id.poidetails_directions); directionsBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Address to = Utils.getPOIasGoogleAddress(mPoi); Address from = null; GeoPoint mylocation = MapManager.requestMyLocation(getActivity()); if (mylocation != null) { from = new Address(Locale.getDefault()); from.setLatitude(mylocation.getLatitudeE6() / 1E6); from.setLongitude(mylocation.getLongitudeE6() / 1E6); } DTHelper.bringmethere(getActivity(), from, to); } }); if (mPoi.getLocation()[0] == 0 && mPoi.getLocation()[1] == 0) { mapBtn.setVisibility(View.INVISIBLE); directionsBtn.setVisibility(View.GONE); } /* * END BUTTONS */ // description, optional tv = (TextView) this.getView().findViewById(R.id.poi_details_descr); String customDesc = POIHelper.customDescription(mPoi, getActivity()); if (customDesc != null && customDesc.length() > 0) { tv.setText(Html.fromHtml(customDesc)); } else { ((LinearLayout) this.getView().findViewById(R.id.poidetails)).removeView(tv); } // // notes // tv = (TextView) this.getView().findViewById(R.id.poi_details_notes); // ((LinearLayout) this.getView().findViewById(R.id.poidetails)).removeView(tv); // location tv = (TextView) this.getView().findViewById(R.id.poi_details_loc); tv.setText(Html.fromHtml("<a href=\"\">" + Utils.getPOIshortAddress(mPoi) + "</a> ")); tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ArrayList<BaseDTObject> list = new ArrayList<BaseDTObject>(); list.add(mPoi); MapManager.switchToMapView(list, PoiDetailsFragment.this); } }); commentsHandler = new CommentsHandler(getPOI(), getActivity(), getView()); } }