开发者

RPC Error with Geocoder

After several days to learn how Android works, I decided to build my first App : A simple app which displays the GMap from a address provided by the user with a simple EditText widget.

I use Geocoder to do that.

After a little bugtracking with logcat, here is the exception returned by adb :

E/LocationMasfClient(   53): forwardGeocode(): RPC failed with status 1
W/System.err(  262): java.io.IOException: RPC failed with status 1
W/System.err(  262):    at   android.location.Geocoder.getFromLocationName(Geocoder.java:166)
W/System.err(  262):    at fr.meetopia.tinymap.MapViewActivity.testGeoCoder(MapViewActivity.java:104)
W/System.err(  262):    at fr.meetopia.tinymap.MapViewActivity.onCreate(MapViewActivity.java:38)
W/System.err(  262):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err(  262):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
W/System.err(  262):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
W/System.err(  262):    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
W/System.err(  262):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)

And now here is the code pointed by the stack trace :

Geocoder geoCoder = new Geocoder(this);
MapView mapView = (MapView) findViewById(R.id.mapview);
    MapController mc = mapView.getController();
    GeoPoint p;
    try {
        List<Address> addresses = geoCoder.getFromLocationName("paris france", 50);
        if (addresses.size() > 0) {
 开发者_JS百科           p = new GeoPoint(
                    (int) (addresses.get(0).getLatitude() * 1E6), 
                    (int) (addresses.get(0).getLongitude() * 1E6));
            mc.animateTo(p);    
            mapView.invalidate();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Obviously, this code is a portion of a MapActivity instantiation.

I made some tests with different versions of Google APIS (8, 7, 4) and different versions of SDK platform (2.2, 2.1, 1.6), but no luck. I just noticed that the Google API 8 contains a known bug and returned a Service Unavailable Exception instead of RPC Failed 1.

On the other hand, I tried to run my app directly on my device (HTC Desire 2.1) and I noticed the same behavior (that is to say: no behavior, a Map with USA in its center).

Here comes the user-permission of the Android Manifest :

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />


Please visit my post Can't mark while showing the current location in 'mapview' I think with some prior modification you can be able to do whatever you aim for.


I think, there are two reasons for you.

  1. Didn't test, but Geocoder will still be unavailable when running in simulator. Even if geocoder.isPresent() returns true. There are workaround in the web, just read Issue 8816 - android for getFromLocationName( .. ) in Comment 21 and it's attached ReverseGeocoder.java

  2. Like Stefan said: When launched on device there the api-key is needed. Has to be added in the layout.xml of your mapview.

Best way to create and test a mapview for me was to implement it like normal and log and try the workaround if "Service not Available".equals( exception.getMessage() ).


Thats my geocoding function, its working fine on google API 7. Hope its helpful

public GeoPoint geoCoding(String name){
         Geocoder geoCoder = new Geocoder(context, Locale.getDefault() );   
         GeoPoint p = null;
            try {
                List<Address> addresses = geoCoder.getFromLocationName(
                    name, 5);
                if (addresses.size() > 0) {
                    p = new GeoPoint(
                            (int) (addresses.get(0).getLatitude() * 1E6), 
                            (int) (addresses.get(0).getLongitude() * 1E6));

                }    
            } catch (IOException e) {
                e.printStackTrace();
            }
            return p;
       }


The Geocoder class is buggy. It works wierd at times. I recommend not to use for getting location. Instead Local Search API and Location manager for using location services.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜