Android Geocoder - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android Geocoder class.

This code example shows how to use the following methods:
 
    private Geocoder geocoder;
 
    @Before 
    public void setUp() throws Exception { 
        geocoder = new Geocoder(new Activity());
    } 
 
    @Test 
    public void shouldRecordLastLocationName() throws Exception { 
        geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1);
        String lastLocationName = shadowOf(geocoder).getLastLocationName();
         
        assertEquals("731 Market St, San Francisco, CA 94103", lastLocationName);
    } 
 
    @Test 
    public void setsUpHasLocationInAddressFromLocationName() throws Exception { 
        shadowOf(geocoder).setSimulatedHasLatLong(true, true);
        Address address = geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1).get(0);
7
CodeRank
This code example shows how to use the following methods:getFromLocation
  public static Address getAddress(final Context context,
      final Location location) {
    if (location == null)
      return null; 
 
    final Geocoder geocoder = new Geocoder(context);
    final List<Address> addresses;
    try { 
      addresses = geocoder.getFromLocation(location.getLatitude(),
          location.getLongitude(), 1);
    } catch (IOException e) {
      return null; 
    } 
    if (addresses != null && !addresses.isEmpty())
      return addresses.get(0);
    else 
      return null; 
  } 
} 
8
CodeRank
This code example shows how to use the following methods:getFromLocation
            mContext = context;
        } 
 
        @Override 
        protected Void doInBackground(Location... params) {
            Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
 
            Location loc = params[0];
            List<Address> addresses = null;
            try { 
                addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
            } catch (IOException e) {
                e.printStackTrace();
                // Update address field with the exception. 
                Message.obtain(mHandler, UPDATE_ADDRESS, e.toString()).sendToTarget();
            } 
            if (addresses != null && addresses.size() > 0) {
                Address address = addresses.get(0);
                // Format the first line of address (if available), city, and country name. 
                String addressText = String.format("%s, %s, %s",
6
CodeRank
This code example shows how to use the following methods:getFromLocationName
@RunWith(WithTestDefaultsRunner.class) 
public class GeocoderTest { 
 
    @Test 
    public void shouldRecordLastLocationName() throws Exception { 
        Geocoder geocoder = new Geocoder(new Activity());
        geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1);
        String lastLocationName = shadowOf(geocoder).getLastLocationName();
         
        assertEquals("731 Market St, San Francisco, CA 94103", lastLocationName);
    } 
} 
7
CodeRank
This code example shows how to use the following methods:getFromLocationName
    } 
 
    /** Return List of <Address> from searched location */ 
    public static List<Address> geoDecode(String searchedLocation) {
        try { 
            Geocoder geo = new Geocoder(XmppService.getInstance().getBaseContext(), Locale.getDefault());
            List<Address> addresses = geo.getFromLocationName(searchedLocation, 10);
            if (addresses != null && addresses.size() > 0) {
               return addresses;
            } 
        } 
        catch(Exception ex) {
        } 
 
        return null; 
    } 
 
    /** launches an activity on the url */ 
    public static void launchExternal(String url) {
        Intent popup = new Intent(XmppService.getInstance(), GeoPopup.class);
7
CodeRank
This code example shows how to use the following methods:
 
	private Geocoder geocoder;
	private SocializeLogger logger;
	 
	public void init(Context context) {
		geocoder = new Geocoder(context, Locale.getDefault());
	} 
	 
	public Address geoCode(double lat, double lng)  {
		try { 
			List<Address> fromLocation = geocoder.getFromLocation(lat, lng, 1);
			 
			if(fromLocation != null && fromLocation.size() > 0) {
				return fromLocation.get(0);
			} 
		} 
		catch (IOException e) {
			if(logger != null) {
				logger.error("Error geocoding location [" +
						lat +
6
CodeRank
This code example shows how to use the following methods:getFromLocationName
 
	private List<Address> getDestinationAddresses() throws IOException {
		List<Address> destinations = new ArrayList<Address>();
         
        Address dest1 = null;
        List<Address> destBuffer = geocoder.getFromLocationName("300 West Martin Luther King Junior Boulevard, Austin, TX", 1);
        if (destBuffer != null && destBuffer.size() > 0) {
        	dest1 = destBuffer.get(0);
        	destinations.add(dest1);
        	Log.d(TAG, "dest1: " + dest1.getAddressLine(0) + " - " + dest1.getLatitude() + ", " + dest1.getLongitude());
        } else { 
        	Log.e(TAG, "Couldn't reverse geocode dest1");
        } 
         
        Address dest2 = null;
        destBuffer = geocoder.getFromLocationName("2902 Medical Arts Street, Austin, TX", 1);
        if (destBuffer != null && destBuffer.size() > 0) {
        	dest2 = destBuffer.get(0);
        	destinations.add(dest2);
        	Log.d(TAG, "dest2: " + dest2.getAddressLine(0) + " - " + dest2.getLatitude() + ", " + dest2.getLongitude());

5
CodeRank
This code example shows how to use the following methods:getFromLocation
        mListener = listener;
    } 
 
    @Override 
    protected Void doInBackground(Location... locations) {
        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
 
        Location loc = locations[0];
        List<Address> addresses = null;
        try { 
            // get all the addresses fro the given latitude, and longitude 
            addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
        } catch (IOException e) {
            mAddress = null;
        } 
 
        // if we have at least one address, use it 
        if (addresses != null && addresses.size() > 0) {
            mAddress = addresses.get(0);
        } 
6
CodeRank
This code example shows how to use the following methods:getFromLocation, getFromLocationName
@Suppress 
public class GeocoderTest extends AndroidTestCase {
 
    public void testGeocoder() throws Exception { 
        Locale locale = new Locale("en", "us");
        Geocoder g = new Geocoder(mContext, locale);
 
        List<Address> addresses1 = g.getFromLocation(37.435067, -122.166767, 2);
        assertNotNull(addresses1);
        assertEquals(1, addresses1.size());
 
        Address addr = addresses1.get(0);
        assertEquals("94305", addr.getFeatureName());
        assertEquals("Palo Alto, CA 94305", addr.getAddressLine(0));
        assertEquals("USA", addr.getAddressLine(1));
        assertEquals("94305", addr.getPostalCode());
        assertFalse(Math.abs(addr.getLatitude() - 37.4240385) > 0.1);
 
        List<Address> addresses2 = g.getFromLocationName("San Francisco, CA", 1);
        assertNotNull(addresses2);

4
CodeRank
This code example shows how to use the following methods:getFromLocation

Related Android Geocoder Questions & Answers:

See Code Examples for Android Geocoder Methods: