Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Convert annotations API to use peer model #6912

Closed
tobrun opened this issue Nov 4, 2016 · 3 comments
Closed

Convert annotations API to use peer model #6912

tobrun opened this issue Nov 4, 2016 · 3 comments
Labels
Android Mapbox Maps SDK for Android annotations Annotations on iOS and macOS or markers on Android refactor SEMVER-MAJOR Requires a major release according to Semantic Versioning rules

Comments

@tobrun
Copy link
Member

tobrun commented Nov 4, 2016

Our current annotations API relies heavily on MapboxMap/Mapview as a gateway to use jni to talk to core. There is actually no need to route through these classes if we implement our annotations as peer models, the classes themselves will be able to call into jni.

This type of construction is heavily used in runtime style implementation and separates the code more cleanly. An example of such a peer model class in details below.

cc @ivovandongen

/**
 * A GeoJson source. Exposes a {@link FeatureCollection} from Json.
 *
 * @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#sources-geojson">the style specification</a>
 */
public class GeoJsonSource extends Source {

    /**
     * Internal use
     *
     * @param nativePtr - pointer to native peer
     */
    public GeoJsonSource(long nativePtr) {
        super(nativePtr);
    }

    /**
     * Create an empty GeoJsonSource
     *
     * @param id the source id
     */
    public GeoJsonSource(String id) {
        initialize(id, null);
        setGeoJson(FeatureCollection.fromFeatures(new ArrayList<Feature>()));
    }

    /**
     * Create an empty GeoJsonSource with non-default {@link GeoJsonOptions}
     *
     * @param id      the source id
     * @param options options
     */
    public GeoJsonSource(String id, GeoJsonOptions options) {
        initialize(id, options);
        setGeoJson(FeatureCollection.fromFeatures(new ArrayList<Feature>()));
    }

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

    /**
     * Create a GeoJsonSource from a raw json string and non-default {@link GeoJsonOptions}
     *
     * @param id      the source id
     * @param geoJson raw Json body
     * @param options options
     */
    public GeoJsonSource(String id, String geoJson, GeoJsonOptions options) {
        if (geoJson == null || geoJson.startsWith("http")) {
            throw new IllegalArgumentException("Expected a raw json body");
        }
        initialize(id, options);
        setGeoJson(geoJson);
    }

    /**
     * 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) {
        initialize(id, null);
        nativeSetUrl(url.toExternalForm());
    }

    /**
     * Create a GeoJsonSource from a remote geo json file and non-default {@link GeoJsonOptions}
     *
     * @param id      the source id
     * @param url     remote json file
     * @param options options
     */
    public GeoJsonSource(String id, URL url, GeoJsonOptions options) {
        initialize(id, options);
        nativeSetUrl(url.toExternalForm());
    }

    /**
     * Create a GeoJsonSource from a {@link FeatureCollection}
     *
     * @param id       the source id
     * @param features the features
     */
    public GeoJsonSource(String id, FeatureCollection features) {
        initialize(id, null);
        setGeoJson(features);
    }

    /**
     * Create a GeoJsonSource from a {@link FeatureCollection} and non-default {@link GeoJsonOptions}
     *
     * @param id       the source id
     * @param features the features
     * @param options  options
     */
    public GeoJsonSource(String id, FeatureCollection features, GeoJsonOptions options) {
        initialize(id, options);
        setGeoJson(features);
    }

    /**
     * Updates the GeoJson
     *
     * @param features the GeoJSON {@link FeatureCollection}
     */
    public void setGeoJson(FeatureCollection features) {
        checkValidity();
        setGeoJson(features.toJson());
    }

    /**
     * Updates the GeoJson
     *
     * @param json the raw GeoJson FeatureCollection string
     */
    public void setGeoJson(String json) {
        checkValidity();
        setRawJson(json);
    }

    /**
     * Updates the url
     *
     * @param url the GeoJSON FeatureCollection url
     */
    public void setUrl(URL url) {
        checkValidity();
        setUrl(url.toExternalForm());
    }

    /**
     * Updates the url
     *
     * @param url the GeoJSON FeatureCollection url
     */
    public void setUrl(String url) {
        checkValidity();
        nativeSetUrl(url);
    }

    protected void setRawJson(String geoJson) {
        //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", geoJson);
        nativeSetGeoJson(wrapper);
    }

    protected native void initialize(String layerId, Object options);

    protected native void nativeSetUrl(String url);

    private native void nativeSetGeoJson(Object geoJson);

    @Override
    protected native void finalize() throws Throwable;

}
@tobrun tobrun added Android Mapbox Maps SDK for Android annotations Annotations on iOS and macOS or markers on Android labels Nov 4, 2016
@tobrun tobrun added this to the android-v5.0.0 milestone Nov 4, 2016
@tobrun tobrun added the refactor label Nov 4, 2016
@tobrun tobrun mentioned this issue Nov 4, 2016
4 tasks
@ivovandongen
Copy link
Contributor

@tobrun I like it. Especially since we can trim down MapboxMap and MapView a it with that.

@tobrun
Copy link
Member Author

tobrun commented Dec 2, 2016

Especially since we can trim down MapboxMap and MapView a it with that.

In #7189 I added some in line comments related to this. What needs to happen is to decouple MapView/MapboxMap from AnnotationManager by replacing it with the Projection and Transform class. The only dependencies that remain after that are MapView for showing an InfoWindow (this should be replaced with a dedicated ViewGroup as with MarkerViews) and MapboxMap for integration with Annotations (the gist behind this issue).

@zugaldia zugaldia removed this from the android-v5.0.0 milestone Mar 6, 2017
@zugaldia zugaldia added the SEMVER-MAJOR Requires a major release according to Semantic Versioning rules label Mar 6, 2017
@tobrun
Copy link
Member Author

tobrun commented Sep 19, 2018

Plan is to deprecate annotations in an upcoming release. Replacing this with the annotations plugin work happening in https://github.com/mapbox/mapbox-plugins-android/tree/master/plugin-annotation.

@tobrun tobrun closed this as completed Sep 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android annotations Annotations on iOS and macOS or markers on Android refactor SEMVER-MAJOR Requires a major release according to Semantic Versioning rules
Projects
None yet
Development

No branches or pull requests

3 participants