4

I had written a simple Activity to test presence of Geocoder, calling Geocoder.isPresent() always returns false.

Class:

public class LocationTestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LocationManager locationManager = (LocationManager)  this.getSystemService(Context.LOCATION_SERVICE);
        Log.d( "LocationTestActivity", "Geocoder.isPresent : " + Geocoder.isPresent() );

    }
}


AndroidManifest.xml ALSO has following entries before "application" element:

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


Environment : Eclipse Indigo 3.7.1, ICS 4.0 emulator on XP Professional 2002 SP 3

Please help me understand:
1. Why Geocoder.isPresent() is always returing false?
2. What changes to make so that Geocoder.isPresent() returns true?

Thanks much in advance!

lupchiazoem
  • 8,026
  • 6
  • 36
  • 42
  • I even tried by making "Google API" as target in my emulator but, no use. – lupchiazoem Jan 31 '12 at 07:11
  • I also tried by including maps.jar (\android-sdk\add-ons\addon-google_apis-google_inc_-14\libs) in build path but, no use. – lupchiazoem Jan 31 '12 at 07:32
  • Ah! at last got it to work! I had created a new AVD whose target I had set as "Google API" and relaunched my app. Surprisingly, I did this long back as can be seen from my frist comment of my question. But, it didn't work that time (updated existing AVD. but, still should have worked. wonder why it didn't work!). Anyhow, to summarize, to use Geocoder, target must be set to "Google API" in AVD. That's it! You don't even need maps.jar in your build path. Hope this answers help many. NOTE: I did not make any changes to code as posted here. – lupchiazoem Jan 31 '12 at 14:02
  • Also, Geocoder does not require INTERNET permission. In other words, AndroidManifest.xml file need not have entry. – lupchiazoem Feb 01 '12 at 03:01
  • 1
    I could use some clarification. Whether or not the Geocoder "backend" is present still only depends on the user's device correct? That is, even if we build against the "Google API addon" which includes the external maps library, it doesn't mean that our App will include the "backend" when we publish to the Market correct? So even though the [Google APIs addon](http://developer.android.com/sdk/RELEASENOTES.html) includes the "backend service implementation", there is no guarantee that the end user's device will? – Tony Chan Feb 05 '12 at 00:51
  • **Or** does the fact that we build against the "Google API addon" automatically bundle the missing Geocoder implementation/backend service into our published .apk (so we never have to worry)? – Tony Chan Feb 05 '12 at 00:55
  • As per documentation[[http://developer.android.com/sdk/RELEASENOTES.html]] - The Google APIs add-on gives your application access to the com.google.android.maps external library that is included on many (if not most) Android-powered devices. So, yes, backend implementation, still, depends on device provider/manufacturer. – lupchiazoem Feb 05 '12 at 14:04
  • 1
    An app (.apk) will never, implicitly, include Geocoder backend service implementation. Though, you may explicitly choose to do so. It's like writing a JDBC class(part of an application) and deploying in different environments. The environments must provide JDBC implementations or you may choose to bundle JDBC implementation/jar file in your application. So, even if environments do not provide an implementation, your application can still access database if you bundled JDBC implementation/jar file in your application. – lupchiazoem Feb 05 '12 at 14:04
  • Thanks for the clarification. I find it interesting that the .INTERNET permission is not required since it is obviously making a network call. Maybe it is an oversight? – Tony Chan Feb 07 '12 at 02:41
  • 1
    My pleasure. Though the phone is making a network call, actually it's leveraging network SIGNAL and not INTERNET CONNECTION. Unfortunately, many examples, out there, use INTERNET permission in Geocoder examples. When my code was not working, I googled and blindly followed examples. Later, when I discovered solution, I realized INTERNET permission is not required, as clearly stated by [documentation](http://developer.android.com/guide/topics/location/obtaining-user-location.html). Hope this helps... – lupchiazoem Feb 07 '12 at 03:26

3 Answers3

5

Actually the Geocoder need a Service running in the background by the framework.

From the documentation:

The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.

so if we look at the documentation of isPresent(), it states.

Returns true if the Geocoder methods getFromLocation and getFromLocationName are implemented. Lack of network connectivity may still cause these methods to return null or empty lists.

Note: keep in mind that isPresent() is not available in Pre-Api 9 plateforms.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • I agree. So, if you see my AndroidManifest.xml snippet, I use API level 14. – lupchiazoem Jan 31 '12 at 07:07
  • using API level 14 doesn't guarantee you about the background service is running.. – Adil Soomro Jan 31 '12 at 07:32
  • 1
    Fine, I also tried by including maps.jar (\android-sdk\add-ons\addon-google_apis-google_inc_-14\libs) in build path but, no use. So, how do I use/enable a backend service and which one is best? – lupchiazoem Jan 31 '12 at 07:34
  • Actually, [The Google APIs add-on also includes a Geocoder backend service implementation.](http://developer.android.com/sdk/RELEASENOTES.html) – lupchiazoem Jan 31 '12 at 07:44
  • Actually when I came to this problem, I make a Geocoder by myself that is always returning me the response. So I'll suggest you to don't rely on Geocoder. – Adil Soomro Jan 31 '12 at 07:57
2

Use AsyncTask to fetch coordinates from server using geocoder. For example, getFromLocationName() should be called using AsyncTask. UI thread (main activity) does not allow the tasks which take too much time, hence the method returns empty list.

Sujeet Sinha
  • 2,417
  • 2
  • 18
  • 27
  • I think OP wanted to know why the Geocoder `isPresent` method is returning false. It's not a concurrency issue, the issue is that the Geocoder underlying service is not available to the OP. – Gi0rgi0s Jul 24 '16 at 18:11
1

Testing this code in Emulator or device ? I have faced same problem when I was using GeoCoder on 2.2 emulator. But code works fine on 2.1 emulator. Try to use 2.1

And code must be running fine on device.

Hardik Trivedi
  • 5,677
  • 5
  • 31
  • 51
  • As my Activity does not have any functionality, emulator just displays a layout with a static string in TextView declared in a resource. But, as I log a message, LogCat always display false. – lupchiazoem Jan 31 '12 at 07:21