/** * to get latitude and longitude of an address * * @param strAddress address string * @return lat and lng in comma separated string */ public String getLocationFromAddress(String strAddress) { Geocoder coder = new Geocoder(mContext); List<Address> address; try { address = coder.getFromLocationName(strAddress, 1); if (address == null) { return null; } Address location = address.get(0); double lat = location.getLatitude(); double lng = location.getLongitude(); return lat + "," + lng; } catch (Exception e) { return null; } }
public String getAddress(Context context, double lat, double lng) { Geocoder geocoder = new Geocoder(context, Locale.getDefault()); try { List<Address> addresses = geocoder.getFromLocation(lat, lng, 1); Address obj = addresses.get(0); String add = obj.getAddressLine(0); add = add + "," + obj.getAdminArea(); add = add + "," + obj.getCountryName(); return add; } catch (IOException e) { e.printStackTrace(); Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); return null; } }
public Observable<Address> getObservableAddressFromLocation(final double latitude, final double longitude, final Context context) { return new Observable<Address>() { @Override protected void subscribeActual(Observer<? super Address> observer) { Geocoder geocoder = new Geocoder(context, Locale.getDefault()); try { List<Address> addressList = geocoder.getFromLocation( latitude, longitude, 1); if (addressList != null && addressList.size() > 0) { address = addressList.get(0); observer.onNext(address); } } catch (IOException e) { Log.e(TAG, "Unable connect to Geocoder", e); } } }.subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()); }
@Override public void call(Subscriber<? super List<Address>> subscriber) { Geocoder geocoder = new Geocoder(ctx); List<Address> result; try { if (bounds != null) { result = geocoder.getFromLocationName(locationName, maxResults, bounds.southwest.latitude, bounds.southwest.longitude, bounds.northeast.latitude, bounds.northeast.longitude); } else { result = geocoder.getFromLocationName(locationName, maxResults); } if (!subscriber.isUnsubscribed()) { subscriber.onNext(result); subscriber.onCompleted(); } } catch (IOException e) { if (!subscriber.isUnsubscribed()) { subscriber.onError(e); } } }
@Override public void call(final Subscriber<? super List<Address>> subscriber) { Geocoder geocoder = new Geocoder(ctx, locale); try { subscriber.onNext(geocoder.getFromLocation(latitude, longitude, maxResults)); subscriber.onCompleted(); } catch (IOException e) { // If it's a service not available error try a different approach using google web api if (e.getMessage().equalsIgnoreCase("Service not Available")) { Observable .create(new FallbackReverseGeocodeObservable(locale, latitude, longitude, maxResults)) .subscribeOn(Schedulers.io()) .subscribe(subscriber); } else { subscriber.onError(e); } } }
public static Address getLatLng(String location, Context mContext) { Address address = null; try { Geocoder gc = new Geocoder(mContext); List<Address> addresses = gc.getFromLocationName(location, 1); // get the found Address Objects for (Address a : addresses) { if (a.hasLatitude() && a.hasLongitude()) { // Log.i(TAG, String.valueOf(location + " " + a.getLatitude() + " // " + a.getLongitude())); address = a; } else { Log.d(TAG, " this location has no entry " + location); } } } catch (IOException e) { // handle the exception } return address; }
@Nullable @Override protected Address doInBackground(String... strings) { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); Address result = null; double latitude = mLocation.getLatitude(); double longitude = mLocation.getLongitude(); try { List<Address> geocodedAddresses = geocoder.getFromLocation(latitude, longitude, 1); if (geocodedAddresses != null && !geocodedAddresses.isEmpty()) { result = geocodedAddresses.get(0); } } catch (IOException e) { Throwable cause = e.getCause(); Log.i(TAG, "Error " + (cause != null ? cause.getClass().getSimpleName() : '(' + e.getClass().getSimpleName() + ": " + e.getMessage() + ')') + " getting locality with Geocoder. " + "Trying with HTTP/GET on Google Maps API."); result = geolocateFromGoogleApis(latitude, longitude); } return result; }
public void GetLoc(final String firstname, final String lastname, final String em, final String pass, String loc,String phone, final SharedPreferences sharedPref) { Geocoder coder = new Geocoder(Authentication.this); List<Address> addresses; try { addresses = coder.getFromLocationName(loc, 5); if (addresses == null) { } Address location = addresses.get(0); double lat = location.getLatitude(); double lng = location.getLongitude(); Log.i("Lat",""+lat); Log.i("Lng",""+lng); //SetData(firstname,lastname,em,pass,loc,lat,lng,phone,sharedPref); } catch (IOException e) { e.printStackTrace(); } }
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Utils.logD(LOG_TAG, "onCreate"); mSharedPreferences = new PreferencesManagerImp(getActivity().getApplicationContext()); mLocation = mSharedPreferences.getLocation(); if (savedInstanceState != null) { mRotation = true; } LoaderProvider loaderProvider = new LoaderProvider(getContext()); LoaderManager loaderManager = getLoaderManager(); Geocoder geocoder = new Geocoder(getActivity()); // loaderManager.enableDebugLogging(true); mPresenter = new FindPresenter(mLocation, loaderManager, loaderProvider, geocoder); setHasOptionsMenu(true); mRecentSearchSuggestions = new SearchRecentSuggestions(getContext(), RecentSuggestionsProvider.AUTHORITY, RecentSuggestionsProvider.MODE); mCompositeSubscription = new CompositeSubscription(); mActivityCoordinator = (CoordinatorLayout) getActivity().findViewById(R.id.coordinator); mSnackCoordinator = (CoordinatorLayout) getActivity().findViewById(R.id.coordinatorSnackContainer); }
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //mTm = new TimeMeasure(LOG_TAG); Utils.logD(LOG_TAG, "onCreate:" + this); mSharedPreferences = new PreferencesManagerImp(getActivity().getApplicationContext()); mLocation = mSharedPreferences.getLocation(); mGeocoder = new Geocoder(getActivity(), Locale.getDefault()); mLoaderProvider = new LoaderProvider(getActivity()); mLoaderManager = getLoaderManager(); mPresenter = new MapTabPresenter(mLoaderProvider, mLoaderManager, mGeocoder, mSharedPreferences); mPresenter.setView(this); mPresenter.setLocation(mLocation); mAddress = mPresenter.onGetAddressFromLocation(mLocation); }
private String getAddress(double lat, double lng) { String address=null; Geocoder geocoder = new Geocoder(context, Locale.getDefault()); List<Address> list = null; try{ list = geocoder.getFromLocation(lat, lng, 1); } catch(Exception e){ e.printStackTrace(); } if(list == null){ System.out.println("Fail to get address from location"); return null; } if(list.size() > 0){ Address addr = list.get(0); address = removeNULL(addr.getAdminArea())+" " + removeNULL(addr.getLocality()) + " " + removeNULL(addr.getThoroughfare()) + " " + removeNULL(addr.getFeatureName()); } return address; }
@SuppressLint("LongLogTag") private String getCompleteAddressString(double LATITUDE, double LONGITUDE) { String strAdd = ""; Geocoder geocoder = new Geocoder(context, Locale.getDefault()); try { List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); if (addresses != null) { Address returnedAddress = addresses.get(0); StringBuilder strReturnedAddress = new StringBuilder(""); for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) { strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); } strAdd = strReturnedAddress.toString(); Log.w("My Current loction address", "" + strReturnedAddress.toString()); } else { Log.w("My Current loction address", "No Address returned!"); } } catch (Exception e) { e.printStackTrace(); Log.w("My Current loction address", "Canont get Address!"); } return strAdd; }
/** * get address info */ private Address getAddress(double latitude, double longitude) { if(WXEnvironment.isApkDebugable()) { WXLogUtils.d(TAG, "into--[getAddress] latitude:" + latitude + " longitude:" + longitude); } try { if (mWXSDKInstance == null || mWXSDKInstance.isDestroy()) { return null; } Geocoder gc = new Geocoder(mWXSDKInstance.getContext()); List<Address> list = gc.getFromLocation(latitude, longitude, 1); if (list != null && list.size() > 0) { return list.get(0); } } catch (Exception e) { WXLogUtils.e(TAG, e); } return null; }
@Override protected Address doInBackground(String... params) { final Geocoder geocoder = new Geocoder(activity); List<Address> addressList; try { addressList = geocoder.getFromLocationName(params[0], 1); if (addressList != null && addressList.size() > 0) { return addressList.get(0); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
private void updateUI() { if (mLastLocation != null) { try { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude(), 1); String cityName = addresses.get(0).getLocality(); String stateName = addresses.get(0).getAdminArea(); String countryName = addresses.get(0).getCountryName(); textView.setText("Photo at.. Country: " + countryName + ", State: " + stateName + ", City: " + cityName); } catch (IOException e) { e.printStackTrace(); textView.setText("Photo at: " + mLastLocation.toString()); } } }
/** * Set current location from map * TODO: Faire mieux la difference entre une location exacte et une * * @param location the location returned from the map picker */ public void setCurrentLocation(Intent data) { final Place place = PlacePicker.getPlace(getActivity(), data); Geocoder geocoder = new Geocoder(getActivity()); try { List<Address> addresses = geocoder.getFromLocation(place.getLatLng().latitude, place.getLatLng().longitude, 1); elephant.currentLoc.cityName = addresses.get(0).getAddressLine(0); if (!addresses.get(0).getAddressLine(0).equals(addresses.get(0).getSubAdminArea())) { elephant.currentLoc.districtName = addresses.get(0).getSubAdminArea(); } elephant.currentLoc.provinceName = addresses.get(0).getAdminArea(); } catch (IOException e) { e.printStackTrace(); } currentLocation.setText(elephant.currentLoc.format()); }
/** * Set birth location from map * * @param location the location returned from the map picker */ public void setBirthLocation(Intent data) { final Place place = PlacePicker.getPlace(getActivity(), data); Geocoder geocoder = new Geocoder(getActivity()); try { List<Address> addresses = geocoder.getFromLocation(place.getLatLng().latitude, place.getLatLng().longitude, 1); elephant.birthLoc.cityName = addresses.get(0).getAddressLine(0); if (!addresses.get(0).getAddressLine(0).equals(addresses.get(0).getSubAdminArea())) { elephant.birthLoc.districtName = addresses.get(0).getSubAdminArea(); } elephant.birthLoc.provinceName = addresses.get(0).getAdminArea(); } catch (IOException e) { e.printStackTrace(); } birthLocation.setText(elephant.birthLoc.format()); }
public void setRegistrationLocation(Intent data) { final Place place = PlacePicker.getPlace(getActivity(), data); Geocoder geocoder = new Geocoder(getActivity()); try { List<Address> addresses = geocoder.getFromLocation(place.getLatLng().latitude, place.getLatLng().longitude, 1); elephant.registrationLoc.cityName = addresses.get(0).getAddressLine(0); if (!addresses.get(0).getAddressLine(0).equals(addresses.get(0).getSubAdminArea())) { elephant.registrationLoc.districtName = addresses.get(0).getSubAdminArea(); } elephant.registrationLoc.provinceName = addresses.get(0).getAdminArea(); } catch (IOException e) { e.printStackTrace(); } registrationLocation.setText(elephant.registrationLoc.format()); }
private void getCompleteAddressString(double LATITUDE, double LONGITUDE) { String strAdd = ""; Geocoder geocoder = new Geocoder(getApplicationContext()); try { List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); String address = addresses.get(0).getAddressLine(0); Log.i("address",address); String city = addresses.get(0).getLocality(); Log.i("city",city); String state = addresses.get(0).getAdminArea(); Log.i("state",state); String country = addresses.get(0).getCountryName(); Log.i("country",country); String postalCode = addresses.get(0).getPostalCode(); String knownName = addresses.get(0).getFeatureName(); add=address+", "+city+", "+state+", "+country; } catch (Exception e) { e.printStackTrace(); } }
private void updateMemoryPosition(Memory memory, LatLng latLng) { Geocoder geocoder = new Geocoder(this); List<Address> matches = null; try { matches = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1); } catch (IOException e) { e.printStackTrace(); } Address bestMatch = (matches.isEmpty()) ? null : matches.get(0); int maxLine = bestMatch.getMaxAddressLineIndex(); memory.city = bestMatch.getAddressLine(maxLine - 1); memory.country = bestMatch.getAddressLine(maxLine); memory.latitude = latLng.latitude; memory.longitude = latLng.longitude; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textOut = (TextView) findViewById(R.id.textOut); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); // <5> geocoder = new Geocoder(this); // <6> // Initialize with the last known location Location lastLocation = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); // <7> if (lastLocation != null) onLocationChanged(lastLocation); }
/** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ public void onMapSearch(View view) { EditText addressBar = (EditText) findViewById(R.id.txtAddress); String strAddress = addressBar.getText().toString(); List<Address> address = new ArrayList(); Geocoder coder = new Geocoder(getApplicationContext(), Locale.getDefault()); try { address = coder.getFromLocationName(strAddress, 5); } catch (Exception ex) { ex.printStackTrace(); } if (address != null) { Address location = address.get(0); LatLng destination = new LatLng(location.getLatitude(), location.getLongitude()); mMap.addMarker(new MarkerOptions().position(destination).title("Marker in Destination")); mMap.moveCamera(CameraUpdateFactory.newLatLng(destination)); } }
@Override public boolean onQueryTextSubmit(String query) { if(query.length() == 0) return false; // get location from api Geocoder geocoder = new Geocoder(this); List<Address> addresses; try { addresses = geocoder.getFromLocationName(query, 1); } catch (IOException e) { return true; } if(addresses.size() > 0) { searchPosition = new LatLng(addresses.get(0).getLatitude(), addresses.get(0).getLongitude()); } else { // no result was found Toast.makeText(this, getString(R.string.no_result), Toast.LENGTH_SHORT).show(); searchPosition = null; return true; } searchView.clearFocus(); searchHistory.add(query); updateNowLocation(searchPosition, getString(R.string.choose_location_tag), true); return true; }
@Override protected final void onAsyncRun() throws Exception { if (isGeocoderPresent()) { Geocoder geocoder = new Geocoder(getWorkContext().getContext()); if (mLocationName != null) { mResults = geocode(geocoder); } else { mResults = geocodeReverse(geocoder); } if (mResults == null || mResults.size() == 0) { throw new RuntimeException("No geocoding results found"); } } else { throw new RuntimeException("Geocoder is not present"); } }
/** * Get list of address by latitude and longitude * @return null or List<Address> */ public List<Address> getGeocoderAddress(Context context) { if (location != null) { Geocoder geocoder = new Geocoder(context, Locale.ENGLISH); try { List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); return addresses; } catch (IOException e) { //e.printStackTrace(); Log.e("Error : Geocoder", "Impossible to connect to Geocoder", e); } } return null; }
public List<Address> getGeocoderAddress(Context context) { if (location != null) { Geocoder geocoder = new Geocoder(context, Locale.ENGLISH); try { List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); return addresses; } catch (IOException e) { //e.printStackTrace(); Log.e("Error : Geocoder", "Impossible to connect to Geocoder", e); } } return null; }
@ProtoMethod(description = "Get the location name of a given latitude and longitude", example = "") @ProtoMethodParam(params = {"latitude", "longitude"}) public String getLocationName(double lat, double lon) { String gpsLocation = ""; Geocoder gcd = new Geocoder(getContext(), Locale.getDefault()); List<Address> addresses; try { addresses = gcd.getFromLocation(lat, lon, 1); gpsLocation = addresses.get(0).getLocality(); } catch (IOException e) { e.printStackTrace(); } return gpsLocation; }
public static String getLocalityNameEng(Context context, LatLng latLng) { Geocoder gcd = new Geocoder(context, Locale.US); try { List<Address> addresses = gcd.getFromLocation(latLng.latitude, latLng.longitude, 1); if (addresses.size() > 0) { Address address = addresses.get(0); String name = address.getCountryName(); if (address.getLocality() != null) name = address.getLocality(); return name; } } catch (IOException ex) { Log.d(TAG, Log.getStackTraceString(ex)); } return null; }
@Nullable public static String getLocalityName(Context context, LatLng latLng) { Geocoder gcd = new Geocoder(context, Locale.getDefault()); try { List<Address> addresses = gcd.getFromLocation(latLng.latitude, latLng.longitude, 1); if (addresses.size() > 0) { Address address = addresses.get(0); String name = address.getCountryName(); if (address.getLocality() != null) name = address.getLocality() + ", " + name; return name; } } catch (IOException ex) { Log.d(TAG, Log.getStackTraceString(ex)); } return null; }
/** * Creates a new LocationSensor component. * * @param container ignored (because this is a non-visible component) */ public LocationSensor(ComponentContainer container) { super(container.$form()); handler = new Handler(); // Set up listener form.registerForOnResume(this); form.registerForOnStop(this); // Initialize sensor properties (60 seconds; 5 meters) timeInterval = 60000; distanceInterval = 5; // Initialize location-related fields Context context = container.$context(); geocoder = new Geocoder(context); locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); locationCriteria = new Criteria(); myLocationListener = new MyLocationListener(); allProviders = new ArrayList<String>(); // Do some initialization depending on the initial enabled state Enabled(enabled); }