summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java
blob: 6f2113ab0ee0844c67831a9fa5c39153a5f8d935 (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
package com.mapbox.mapboxsdk.style.sources;

import java.net.URL;
import java.util.HashMap;

/**
 * A GeoJson source.
 *
 * @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-geojson">the style specification</a>
 */
public class GeoJsonSource extends Source {
    public static final String TYPE = "geojson";
    private static final String DATA_KEY = "data";

    /**
     * Create a GeoJsonSource from a raw json string
     *
     * @param id      the source id
     * @param geoJson raw Json body
     */
    public GeoJsonSource(String id, String geoJson) {
        super(id, TYPE);
        if (geoJson == null || geoJson.startsWith("http")) {
            throw new IllegalArgumentException("Expected a raw json body");
        }

        //Wrap the String in a map as an Object is expected by the
        //style conversion template
        HashMap<String, String> wrapper = new HashMap<>();
        wrapper.put(DATA_KEY, geoJson);
        this.put(DATA_KEY, wrapper);
    }

    /**
     * Create a GeoJsonSource from a remote geo json file
     *
     * @param id  the source id
     * @param url remote json file
     */
    public GeoJsonSource(String id, URL url) {
        super(id, TYPE);
        this.put(DATA_KEY, url.toExternalForm());
    }

    public GeoJsonSource withCluster(boolean cluster) {
        this.put("cluster", cluster);
        return this;
    }

    public GeoJsonSource withClusterMaxZoom(float zoom) {
        this.put("clusterMaxZoom", zoom);
        return this;
    }

    public GeoJsonSource withClusterRadius(float radius) {
        this.put("clusterRadius", radius);
        return this;
    }
}