Java Code Examples for android.location.Address#setLatitude()

The following examples show how to use android.location.Address#setLatitude() . 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: AddressLoc.java    From PocketMaps with MIT License 6 votes vote down vote up
public static Address readFromPropEntry(Entry<Object, Object> entry)
{
  Address addr = new Address(Locale.getDefault());
  addr.setAddressLine(0, entry.getKey().toString());
  String[] lines = entry.getValue().toString().split("\\" + STRING_SEP, -1);
  for (int i=0; i<(lines.length-2); i++)
  {
    addr.setAddressLine(i+1, lines[i]);
  }
  try
  { // Last two values are lat and lon!
    addr.setLatitude(Double.parseDouble(lines[lines.length-2]));
    addr.setLongitude(Double.parseDouble(lines[lines.length-1]));
  }
  catch (NumberFormatException e)
  {
    log("Can not read lat lon from stored Favourite!");
    return null;
  }
  return addr;
}
 
Example 2
Source File: StreetMatcher.java    From PocketMaps with MIT License 6 votes vote down vote up
public static boolean addToList(ArrayList<Address> addressList, String name, double lat, double lon, Locale locale)
{
  for (Address curAddr : addressList)
  {
    if (!curAddr.getAddressLine(GeocoderLocal.ADDR_TYPE_STREET).equals(name)) { continue; }
    double d = GeoMath.fastDistance(lat, lon, curAddr.getLatitude(), curAddr.getLongitude());
    d = d / GeoMath.DEGREE_PER_METER;
    if (d > 1000) { continue; }
    return false;
  }
  Address address = new Address(locale);
  address.setAddressLine(GeocoderLocal.ADDR_TYPE_COUNTRY, Variable.getVariable().getCountry());
  address.setLatitude(lat);
  address.setLongitude(lon);
  address.setAddressLine(GeocoderLocal.ADDR_TYPE_STREET, name);
  addressList.add(address);
  return true;
}
 
Example 3
Source File: NominatimGeocoder.java    From callerid-for-android with GNU General Public License v3.0 6 votes vote down vote up
protected List<Address> parseResponse(final Place[] places) {
	final List<Address> ret = new ArrayList<Address>(places.length);
	
	for(final Place place : places){
		final Address address = new Address(Locale.getDefault());
		ret.add(address);
		
		address.setThoroughfare(place.getDisplayName());
		address.setLatitude(place.getLatitude());
		address.setLongitude(place.getLongitude());
		final Place.Address placeAddress = place.getAddress();
		if(placeAddress!=null){
			address.setCountryCode(placeAddress.getCountryCode());
			address.setCountryName(placeAddress.getCountry());
			address.setPostalCode(placeAddress.getPostalCode());
			address.setSubAdminArea(placeAddress.getCountry());
		}
	}
	
	return ret;
}
 
Example 4
Source File: NominatimLocationService.java    From your-local-weather with GNU General Public License v3.0 4 votes vote down vote up
private Address parseResponse(Locale locale, JSONObject result) throws JSONException {
    if (!result.has(WIRE_LATITUDE) || !result.has(WIRE_LONGITUDE) ||
            !result.has(WIRE_ADDRESS)) {
        return null;
    }
    Address address = new Address(locale);
    address.setLatitude(result.getDouble(WIRE_LATITUDE));
    address.setLongitude(result.getDouble(WIRE_LONGITUDE));

    JSONObject a = result.getJSONObject(WIRE_ADDRESS);

    address.setThoroughfare(a.optString(WIRE_THOROUGHFARE));
    address.setSubLocality(a.optString(WIRE_SUBLOCALITY));
    address.setPostalCode(a.optString(WIRE_POSTALCODE));
    address.setSubAdminArea(a.optString(WIRE_SUBADMINAREA));
    address.setAdminArea(a.optString(WIRE_ADMINAREA));
    address.setCountryName(a.optString(WIRE_COUNTRYNAME));
    address.setCountryCode(a.optString(WIRE_COUNTRYCODE));

    if (a.has(WIRE_LOCALITY_CITY)) {
        address.setLocality(a.getString(WIRE_LOCALITY_CITY));
    } else if (a.has(WIRE_LOCALITY_TOWN)) {
        address.setLocality(a.getString(WIRE_LOCALITY_TOWN));
    } else if (a.has(WIRE_LOCALITY_VILLAGE)) {
        address.setLocality(a.getString(WIRE_LOCALITY_VILLAGE));
    }

    if (formatter != null) {
        Map<String, String> components = new HashMap<>();
        for (String s : new IterableIterator<>(a.keys())) {
            components.put(s, String.valueOf(a.get(s)));
        }
        String[] split = formatter.formatAddress(components).split("\n");
        for (int i = 0; i < split.length; i++) {
            address.setAddressLine(i, split[i]);
        }

        address.setFeatureName(formatter.guessName(components));
    }

    return address;
}
 
Example 5
Source File: GeocoderNominatim.java    From PocketMaps with MIT License 4 votes vote down vote up
/**
 * Build an Android Address object from the Nominatim address in JSON
 * format. Current implementation is mainly targeting french addresses, and
 * will be quite basic on other countries.
 *
 * @param jResult ...
 * @return ...
 * @throws JSONException ...
 */
protected Address buildAndroidAddress(JSONObject jResult) throws JSONException {
    Address gAddress = new Address(mLocale);
    gAddress.setLatitude(jResult.getDouble("lat"));
    gAddress.setLongitude(jResult.getDouble("lon"));

    JSONObject jAddress = jResult.getJSONObject("address");

    int addressIndex = 0;
    if (jAddress.has("road")) {
        gAddress.setAddressLine(addressIndex++, jAddress.getString("road"));
        gAddress.setThoroughfare(jAddress.getString("road"));
    }
    if (jAddress.has("suburb")) {
        //gAddress.setAddressLine(addressIndex++, jAddress.getString("suburb"));
        //not kept => often introduce "noise" in the address.
        gAddress.setSubLocality(jAddress.getString("suburb"));
    }
    if (jAddress.has("postcode")) {
        gAddress.setAddressLine(addressIndex++, jAddress.getString("postcode"));
        gAddress.setPostalCode(jAddress.getString("postcode"));
    }

    if (jAddress.has("city")) {
        gAddress.setAddressLine(addressIndex++, jAddress.getString("city"));
        gAddress.setLocality(jAddress.getString("city"));
    } else if (jAddress.has("town")) {
        gAddress.setAddressLine(addressIndex++, jAddress.getString("town"));
        gAddress.setLocality(jAddress.getString("town"));
    } else if (jAddress.has("village")) {
        gAddress.setAddressLine(addressIndex++, jAddress.getString("village"));
        gAddress.setLocality(jAddress.getString("village"));
    }

    if (jAddress.has("county")) { //France: departement
        gAddress.setSubAdminArea(jAddress.getString("county"));
    }
    if (jAddress.has("state")) { //France: region
        gAddress.setAdminArea(jAddress.getString("state"));
    }
    if (jAddress.has("country")) {
        gAddress.setAddressLine(addressIndex++, jAddress.getString("country"));
        gAddress.setCountryName(jAddress.getString("country"));
    }
    if (jAddress.has("country_code"))
        gAddress.setCountryCode(jAddress.getString("country_code"));

    /* Other possible OSM tags in Nominatim results not handled yet: subway,
     * golf_course, bus_stop, parking,... house, house_number, building
     * city_district (13e Arrondissement) road => or highway, ... sub-city
     * (like suburb) => locality, isolated_dwelling, hamlet ...
     * state_district */

    return gAddress;
}
 
Example 6
Source File: LatLonActivity.java    From PocketMaps with MIT License 4 votes vote down vote up
@Override
public void onClick(View v)
{
  if (v.getId()==R.id.latLonEnterOk)
  {
    log("Selected: Ok for LatLon");
    double lat = 0; 
    double lon = 0; 
    try
    {
      lat = Double.parseDouble(txtLat.getText().toString());
      lon = Double.parseDouble(txtLon.getText().toString());
    }
    catch (NumberFormatException e)
    {
      logUser("Nuber format error! Enter number as 111.123");
      return;
    }
    if (lat>90)
    {
      logUser("Latitude must not be more than 90");
      return;
    }
    if (lat<-90)
    {
      logUser("Latitude must not be less than -90");
      return;
    }
    if (lon>180)
    {
      logUser("Longitude must not be more than 180");
      return;
    }
    if (lon<-180)
    {
      logUser("Longitude must not be less than -180");
      return;
    }
    Address address = new Address(Locale.GERMANY);
    address.setLatitude(lat);
    address.setLongitude(lon);
    address.setAddressLine(0, "" + lat + ", " + lon);
    LatLonActivity.this.finish();
    callbackListener.onClick(address);
  }
}
 
Example 7
Source File: BackendService.java    From NominatimGeocoderBackend with Apache License 2.0 4 votes vote down vote up
private Address parseResponse(Locale locale, JSONObject result) throws JSONException {
    if (!result.has(WIRE_LATITUDE) || !result.has(WIRE_LONGITUDE) ||
            !result.has(WIRE_ADDRESS)) {
        return null;
    }
    Address address = new Address(locale);
    address.setLatitude(result.getDouble(WIRE_LATITUDE));
    address.setLongitude(result.getDouble(WIRE_LONGITUDE));

    JSONObject a = result.getJSONObject(WIRE_ADDRESS);

    address.setThoroughfare(a.optString(WIRE_THOROUGHFARE));
    address.setSubLocality(a.optString(WIRE_SUBLOCALITY));
    address.setPostalCode(a.optString(WIRE_POSTALCODE));
    address.setSubAdminArea(a.optString(WIRE_SUBADMINAREA));
    address.setAdminArea(a.optString(WIRE_ADMINAREA));
    address.setCountryName(a.optString(WIRE_COUNTRYNAME));
    address.setCountryCode(a.optString(WIRE_COUNTRYCODE));

    if (a.has(WIRE_LOCALITY_CITY)) {
        address.setLocality(a.getString(WIRE_LOCALITY_CITY));
    } else if (a.has(WIRE_LOCALITY_TOWN)) {
        address.setLocality(a.getString(WIRE_LOCALITY_TOWN));
    } else if (a.has(WIRE_LOCALITY_VILLAGE)) {
        address.setLocality(a.getString(WIRE_LOCALITY_VILLAGE));
    }

    if (formatter != null) {
        Map<String, String> components = new HashMap<>();
        for (String s : new IterableIterator<>(a.keys())) {
            components.put(s, String.valueOf(a.get(s)));
        }
        String[] split = formatter.formatAddress(components).split("\n");
        for (int i = 0; i < split.length; i++) {
            Log.d(TAG, split[i]);
            address.setAddressLine(i, split[i]);
        }

        address.setFeatureName(formatter.guessName(components));
    }

    return address;
}