android.location.Geocoder# getFromLocationName ( ) 源码实例Demo

下面列出了android.location.Geocoder# getFromLocationName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: SampleApp   文件: LocationHelper.java

/**
 * 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;
    }
}
 
源代码2 项目: PocketMaps   文件: GeocoderGlobal.java

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;
}
 

/**
 * 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;
}
 

@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 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);
  }
}
 

@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);
}
 
源代码7 项目: LibreTasks   文件: OmniArea.java

/**
 * 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.");
}
 
源代码8 项目: MapsMeasure   文件: GeocoderTask.java

@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;
    }
}
 
源代码9 项目: android   文件: VPNFragment.java

@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();
    }
}