aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/location/AndroidGeocoder.java
blob: d0d07e8cbb547dae6d6f02cf01cb3d956148e4ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cgeo.geocaching.location;

import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.RxUtils;

import org.apache.commons.collections4.CollectionUtils;
import org.eclipse.jdt.annotation.NonNull;

import rx.Observable;
import rx.functions.Func0;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

import java.util.List;
import java.util.Locale;

public class AndroidGeocoder {
    private final Geocoder geocoder;

    public AndroidGeocoder(final Context context) {
        geocoder = new Geocoder(context, Locale.getDefault());
    }

    /**
     * Retrieve addresses from a textual location using Android geocoding API. The work happens on the network
     * scheduler.
     *
     * @param keyword
     *            the location
     * @return an observable containing zero or more locations
     *
     * @see Geocoder#getFromLocationName(String, int)
     */
    public Observable<Address> getFromLocationName(@NonNull final String keyword) {
        if (!Geocoder.isPresent()) {
            return Observable.error(new RuntimeException("no Android geocoder"));
        }
        return Observable.defer(new Func0<Observable<Address>>() {
            @Override
            public Observable<Address> call() {
                try {
                    return addressesToObservable(geocoder.getFromLocationName(keyword, 20));
                } catch (final Exception e) {
                    Log.i("Unable to use Android reverse geocoder: " + e.getMessage());
                    return Observable.error(e);
                }
            }
        }).subscribeOn(RxUtils.networkScheduler);
    }

    /**
     * Retrieve the physical address for coordinates. The work happens on the network scheduler.
     *
     * @param coords the coordinates
     * @return an observable containing one location or an error
     */
    public Observable<Address> getFromLocation(@NonNull final Geopoint coords) {
        if (!Geocoder.isPresent()) {
            return Observable.error(new RuntimeException("no Android reverse geocoder"));
        }
        return Observable.defer(new Func0<Observable<Address>>() {
            @Override
            public Observable<Address> call() {
                try {
                    return addressesToObservable(geocoder.getFromLocation(coords.getLatitude(), coords.getLongitude(), 1));
                } catch (final Exception e) {
                    Log.i("Unable to use Android reverse geocoder: " + e.getMessage());
                    return Observable.error(e);
                }
            }
        }).subscribeOn(RxUtils.networkScheduler).first();
    }

    private static Observable<Address> addressesToObservable(final List<Address> addresses) {
        return CollectionUtils.isEmpty(addresses) ?
                Observable.<Address>error(new RuntimeException("no result from Android geocoder")) :
                Observable.from(addresses);
    }

}