开发者

What is the best way to interpret user's location beside Geocoder

So I made an app which access the user location and then interpret (reverse geocoding) it with the Geocoder like this:

Geocoder gc = new Geocoder(ctx, Locale.getDefault());
List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults); 
Address address = addresses.get(0);
town = address.getLocality();
countryCd = address.getCountryCode();

开发者_如何学PythonHowever I find the geocoder service to be really unreliable with IOException and service unavailable too often.

I've read solutions involving spamming the Geocoder service until it gives a result, and others says the service will only allow one hit per IP addr every 15 seconds.

Sometimes rebooting my device will make the service available again.

Is there another API or approach to this subject?


there is a solution there: android problem to get latitude & longitude from a given address

Just to mention an alternative, you can use the Google Maps' REST based reverse geocoder. You can probably use this as a fallback. As you would be using Google Maps in your app too, you should be good with the API rules.


public void getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = obj.getAddressLine(0);
        GUIStatics.currentAddress = obj.getSubAdminArea() + ","
                + obj.getAdminArea();
        GUIStatics.latitude = obj.getLatitude();
        GUIStatics.longitude = obj.getLongitude();
        GUIStatics.currentCity= obj.getSubAdminArea();
        GUIStatics.currentState= obj.getAdminArea();
        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();

        Log.v("IGA", "Address" + add);
        // Toast.makeText(this, "Address=>" + add,
        // Toast.LENGTH_SHORT).show();

        // TennisAppActivity.showDialog(add);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

Use it may be help full to you.


Your probably getting IOException and ServiceUnavailble due to your implementaton.

Have a read of this best practice : http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

(It's better than an API) and opensource!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜