Java Code Examples for android.location.Geocoder

The following are top voted examples for showing how to use android.location.Geocoder. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: AndroidAppBoilerplate   File: LocationHelper.java   View source code 11 votes vote down vote up
/**
 * 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;
    }
}
 
Example 2
Project: GitHub   File: GeocodeObservable.java   View source code 9 votes vote down vote up
@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);
        }
    }
}
 
Example 3
Project: Overkill   File: MapsActivity2.java   View source code 8 votes vote down vote up
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;
    }
}
 
Example 4
Project: wheretomeet-android   File: FriendsAdapter.java   View source code 7 votes vote down vote up
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;
    }
 
Example 5
Project: Nibo   File: LocationAddress.java   View source code 7 votes vote down vote up
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());
}
 
Example 6
Project: GitHub   File: ReverseGeocodeObservable.java   View source code 6 votes vote down vote up
@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);
        }
    }
}
 
Example 7
Project: RxRetrofit-Android   File: DateTimeUtils.java   View source code 6 votes vote down vote up
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;
    }
 
Example 8
Project: civify-app   File: GeocoderAdapter.java   View source code 6 votes vote down vote up
@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;
}
 
Example 9
Project: BookED   File: Authentication.java   View source code 6 votes vote down vote up
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();
    }


}
 
Example 10
Project: Farmacias   File: FindFragment.java   View source code 6 votes vote down vote up
@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);
}
 
Example 11
Project: Farmacias   File: MapTabFragment.java   View source code 6 votes vote down vote up
@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);


}
 
Example 12
Project: MuslimMateAndroid   File: SelectPositionActivity.java   View source code 6 votes vote down vote up
@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;
}
 
Example 13
Project: MuslimMateAndroid   File: SelectLocationTabsActivity.java   View source code 6 votes vote down vote up
@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;
}
 
Example 14
Project: weex-3d-map   File: DefaultLocation.java   View source code 6 votes vote down vote up
/**
 * 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;
}
 
Example 15
Project: open-rmbt   File: MapLocationRequestTask.java   View source code 6 votes vote down vote up
@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;
}
 
Example 16
Project: androidadvanced   File: MainActivity.java   View source code 6 votes vote down vote up
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());
        }
    }
}
 
Example 17
Project: ElephantAsia   File: AddProfilFragment.java   View source code 6 votes vote down vote up
/**
 * 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());
}
 
Example 18
Project: ElephantAsia   File: AddProfilFragment.java   View source code 6 votes vote down vote up
/**
 * 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());
}
 
Example 19
Project: ElephantAsia   File: AddRegistrationFragment.java   View source code 6 votes vote down vote up
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());
  }
 
Example 20
Project: ElephantAsia   File: EditRegistrationFragment.java   View source code 6 votes vote down vote up
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());
  }
 
Example 21
Project: ElephantAsia   File: EditProfilFragment.java   View source code 6 votes vote down vote up
/**
 * 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());
}
 
Example 22
Project: ElephantAsia   File: EditProfilFragment.java   View source code 6 votes vote down vote up
/**
 * 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());
}
 
Example 23
Project: iSPY   File: ShortestDistance.java   View source code 6 votes vote down vote up
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();

    }

}
 
Example 24
Project: iSPY   File: MyLocation.java   View source code 6 votes vote down vote up
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();

    }

}
 
Example 25
Project: Android-Wear-Projects   File: MapsActivity.java   View source code 6 votes vote down vote up
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;
}
 
Example 26
Project: Android-Wear-Projects   File: MapsActivity.java   View source code 6 votes vote down vote up
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;
}
 
Example 27
Project: buildAPKsSamples   File: WhereAmI.java   View source code 6 votes vote down vote up
@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);
}
 
Example 28
Project: Find-It-Location-Finder   File: MapsActivity.java   View source code 6 votes vote down vote up
/**
     * 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));
    }
}
 
Example 29
Project: BikeLine   File: ChooseLocationActivity.java   View source code 6 votes vote down vote up
@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;
}
 
Example 30
Project: oma-riista-android   File: GeocodingTask.java   View source code 6 votes vote down vote up
@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");
    }
}
 
Example 31
Project: FindX   File: digiPune.java   View source code 6 votes vote down vote up
/**
 * 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;
}
 
Example 32
Project: FindX   File: SelectWork.java   View source code 6 votes vote down vote up
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;
}
 
Example 33
Project: FindX   File: GPSTracker.java   View source code 6 votes vote down vote up
/**
 * 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;
}
 
Example 34
Project: FindX   File: Location_event.java   View source code 6 votes vote down vote up
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;
}
 
Example 35
Project: FindX   File: SelectHome.java   View source code 6 votes vote down vote up
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;
}
 
Example 36
Project: FindX   File: MyLocation.java   View source code 6 votes vote down vote up
/**
 * 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;
}
 
Example 37
Project: phonk   File: PLocation.java   View source code 6 votes vote down vote up
@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;
}
 
Example 38
Project: FlightSight-client   File: Utils.java   View source code 6 votes vote down vote up
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;
}
 
Example 39
Project: FlightSight-client   File: Utils.java   View source code 6 votes vote down vote up
@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;
}
 
Example 40
Project: appinventor-extensions   File: LocationSensor.java   View source code 6 votes vote down vote up
/**
 * 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);
}