Java Code Examples for android.location.Geocoder#getFromLocationName()

The following examples show how to use android.location.Geocoder#getFromLocationName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocationHelper.java    From SampleApp with Apache License 2.0 6 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
Source File: GeocoderGlobal.java    From PocketMaps with MIT License 6 votes vote down vote up
public List<Address> find_google(Context context, String searchS)
{
  stopping = false;
  log("Google geocoding started");
  Geocoder geocoder = new Geocoder(context, locale);
  if (!Geocoder.isPresent()) { return null; }
  try
  {
    List<Address> result = geocoder.getFromLocationName(searchS, 50);
    return result;
  }
  catch (IOException e)
  {
    e.printStackTrace();
  }
  return null;
}
 
Example 3
Source File: MapViewHandler.java    From PowerSwitch_Android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Find Coordinates for a given address
 *
 * @param address address as text
 * @return coordinate near the given address
 * @throws AddressNotFoundException
 */
public LatLng findCoordinates(String address) throws CoordinatesNotFoundException {
    /* get latitude and longitude from the address */
    Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
    try {
        List<Address> addresses = geoCoder.getFromLocationName(address, 5);
        if (addresses.size() > 0) {
            Double lat = (addresses.get(0).getLatitude());
            Double lon = (addresses.get(0).getLongitude());

            Log.d("lat-lon", lat + "......." + lon);
            final LatLng location = new LatLng(lat, lon);
            return location;
        } else {
            throw new CoordinatesNotFoundException(address);
        }
    } catch (IOException e) {
        Log.e(e);
    }

    return null;
}
 
Example 4
Source File: MapLocationRequestTask.java    From open-rmbt with Apache License 2.0 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 5
Source File: LocationActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void listing15_20() {
  // Listing 15-20: Geocoding an address
  Geocoder geocoder = new Geocoder(this, Locale.US);
  String streetAddress = "160 Riverside Drive, New York, New York";
  List<Address> locations = null;
  try {
    locations = geocoder.getFromLocationName(streetAddress, 5);
  } catch (IOException e) {
    Log.e(TAG, "Geocoder I/O Exception", e);
  }
}
 
Example 6
Source File: FetchAddressIntentService.java    From GitJourney with Apache License 2.0 5 votes vote down vote up
@Override
protected void onHandleIntent(@Nullable Intent intent) {
    Log.v(TAG, "onHandleIntent");
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    String errorMessage = "";
    // Get the location passed to this service through an extra.
    ArrayList<GitHubUserLocationDataEntry> locationsList = intent.getParcelableArrayListExtra(
            LocationConstants.LOCATION_DATA_EXTRA);
    ResultReceiver mReceiver = intent.getParcelableExtra(
            LocationConstants.RECEIVER);
    try {
        for (int i = 0; i < locationsList.size(); i++) {
            GitHubUserLocationDataEntry entry = locationsList.get(i);
            List<Address> addressList = geocoder.getFromLocationName(entry.getLocation(), 1);
            if (!addressList.isEmpty()) {
                Address address = addressList.get(0);
                entry.setLatitude(address.getLatitude());
                entry.setLongitude(address.getLongitude());
            }
        }
    } catch (IOException ioException) {
        // Catch network or other I/O problems.
        errorMessage = getString(R.string.service_not_available);
        Log.e(TAG, errorMessage, ioException);
    } catch (IllegalArgumentException illegalArgumentException) {
        // Catch invalid latitude or longitude values.
        errorMessage = getString(R.string.invalid_lat_long_used);
        Log.e(TAG, errorMessage, illegalArgumentException);
    }
    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList(LocationConstants.LOCATION_DATA_EXTRA, locationsList);
    mReceiver.send(0, bundle);
}
 
Example 7
Source File: OmniArea.java    From LibreTasks with Apache License 2.0 5 votes vote down vote up
/**
 * Creates OmniArea object for a given address.
 * 
 * @param context
 *          application context. Must not be null.
 * @param address
 *          address of the location to be identified (a.k.a 1600 Amphitheatre Parkway, Mountain
 *          View, CA 94043).
 * @return OmniArea object for a given address.
 * @throws IOException
 *           if Internet access is unavailable.
 * @throws IllegalArgumentException
 *           if either context or address are null, or address cannot be found.
 */
public static OmniArea getOmniArea(Context context, String address, double proximityDistance)
    throws IOException, IllegalArgumentException {

  final int MAX_RESULTS_TO_RETURN = 1;
  final int FIRST_RESULT = 0;

  if (context == null) {
    throw new IllegalArgumentException("context cannot be null.");
  }
  if (address == null) {
    throw new IllegalArgumentException("address cannot be null.");
  }

  Geocoder geocoder = new Geocoder(context, Locale.getDefault());
  List<Address> addressResult = geocoder.getFromLocationName(address, MAX_RESULTS_TO_RETURN);
  if (!addressResult.isEmpty()) {
    Address location = addressResult.get(FIRST_RESULT);
    try {
      return new OmniArea(address, location.getLongitude(), location.getLatitude(),
          proximityDistance);
    } catch (DataTypeValidationException e) {
      throw new IllegalArgumentException("address cannot be found.");
    }
  }

  throw new IllegalArgumentException("address cannot be found.");
}
 
Example 8
Source File: GeocoderTask.java    From MapsMeasure with Apache License 2.0 5 votes vote down vote up
@Override
protected Address doInBackground(final String... locationName) {
    // Creating an instance of Geocoder class
    Geocoder geocoder = new Geocoder(map.getBaseContext());
    try {
        // Get only the best result that matches the input text
        List<Address> addresses = geocoder.getFromLocationName(locationName[0], 1);
        return addresses != null && !addresses.isEmpty() ? addresses.get(0) : null;
    } catch (IOException e) {
        if (BuildConfig.DEBUG) Logger.log(e);
        e.printStackTrace();
        return null;
    }
}
 
Example 9
Source File: VPNFragment.java    From android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void success(final IPService.Data data, Response response) {
    if(mActivity == null) return;

    if(response != null && response.getStatus() == 200) {
        mShowsConnected = data.connected;
        mDetectedCountry = data.country;

        if(!mShowsConnected && mCurrentVPNState.equals(VpnStatus.ConnectionStatus.LEVEL_CONNECTED)) {
            updateIPData();
            return;
        }

        String location = null;
        if (mShowsConnected) {
            mConnectedCard.setVisibility(View.VISIBLE);
        } else {
            mConnectedCard.setVisibility(View.GONE);
            try {
                Geocoder coder = new Geocoder(mActivity);
                List<Address> addressList;
                if (!data.hasCoordinates()) {
                    addressList = coder.getFromLocationName("Country: " + data.country, 1);
                } else {
                    addressList = coder.getFromLocation(data.getLat(), data.getLng(), 1);
                }
                if (addressList != null && addressList.size() > 0) {
                    Address address = addressList.get(0);
                    if (address.getLocality() == null) {
                        location = address.getCountryName();
                    } else {
                        location = String.format("%s, %s", address.getLocality(), address.getCountryCode());
                    }

                    if (address.hasLatitude() && address.hasLongitude())
                        mCurrentLocation = new LatLng(address.getLatitude(), address.getLongitude());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (location == null && data.country != null) {
            Locale locale = new Locale("", data.country);
            location = locale.getDisplayCountry();
        }

        final String finalLocation = location;

        ThreadUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mIPText.setText(data.ip);
                mLocationText.setText(finalLocation);
            }
        });

        processServers();
        updateMapLocation();
    }
}