summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/annotationManager.java.ejs
blob: 6aa8ddd771dc29b0c471324b87fd8e393712ce46 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<%
  const type = locals.type;
  const properties = locals.properties;
  const doc = locals.doc;
-%>
// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`.

package com.mapbox.mapboxsdk.annotations.<%- type %>;

import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.annotation.VisibleForTesting;
import android.support.v4.util.LongSparseArray;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.style.layers.PropertyValue;
import com.mapbox.mapboxsdk.style.layers.<%- camelize(type) %>Layer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.style.layers.Property;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.*;
import static com.mapbox.mapboxsdk.annotations.symbol.Symbol.Z_INDEX;

/**
 * The <%- type %> manager allows to add <%- type %>s to a map.
 */
public class <%- camelize(type) %>Manager {

  public static final String ID_GEOJSON_SOURCE = "mapbox-android-<%- type %>-source";
  public static final String ID_GEOJSON_LAYER = "mapbox-android-<%- type %>-layer";

  // map integration components
  private MapboxMap mapboxMap;
  private GeoJsonSource geoJsonSource;
  private <%- camelize(type) %>Layer layer;

  // callback listeners
  private List<On<%- camelize(type) %>ClickListener> <%- type %>ClickListeners = new ArrayList<>();
  private final MapClickResolver mapClickResolver;

  // internal data set
  private final LongSparseArray<<%- camelize(type) %>> <%- type %>s = new LongSparseArray<>();
  private final List<Feature> features = new ArrayList<>();
  private long currentId;
<% if (type === "symbol") { -%>

  private final SymbolComparator symbolComparator = new SymbolComparator();
<% } -%>

  /**
   * Create a <%- type %> manager, used to manage <%- type %>s.
   *
   * @param mapboxMap the map object to add <%- type %>s to
   */
  @UiThread
  public <%- camelize(type) %>Manager(@NonNull MapboxMap mapboxMap) {
    this(mapboxMap, new GeoJsonSource(ID_GEOJSON_SOURCE), new <%- camelize(type) %>Layer(ID_GEOJSON_LAYER, ID_GEOJSON_SOURCE)
      .withProperties(
        getLayerDefinition()
      )
    );
  }

  /**
   * Create a <%- type %> manager, used to manage <%- type %>s.
   *
   * @param mapboxMap     the map object to add <%- type %>s to
   * @param geoJsonSource the geojson source to add <%- type %>s to
   * @param layer         the <%- type %> layer to visualise <%- camelize(type) %>s with
   */
  @VisibleForTesting
  public <%- camelize(type) %>Manager(MapboxMap mapboxMap, @NonNull GeoJsonSource geoJsonSource, <%- camelize(type) %>Layer layer) {
    this.mapboxMap = mapboxMap;
    this.geoJsonSource = geoJsonSource;
    this.layer = layer;
    mapboxMap.addSource(geoJsonSource);
    mapboxMap.addLayer(layer);
    mapboxMap.addOnMapClickListener(mapClickResolver = new MapClickResolver(mapboxMap));
  }

  /**
   * Cleanup <%- type %> manager, used to clear listeners
   */
  @UiThread
  public void onDestroy() {
    mapboxMap.removeOnMapClickListener(mapClickResolver);
    <%- type %>ClickListeners.clear();
  }
<% if (type === "circle" || type === "symbol") { -%>

  /**
   * Create a <%- type %> on the map from a LatLng coordinate.
   *
   * @param latLng place to layout the <%- type %> on the map
   * @return the newly created <%- type %>
   */
  @UiThread
  public <%- camelize(type) %> create<%- camelize(type) %>(@NonNull LatLng latLng) {
    <%- camelize(type) %> <%- type %> = new <%- camelize(type) %>(this, currentId);
    <%- type %>.setLatLng(latLng);
    <%- type %>s.put(currentId, <%- type %>);
    currentId++;
    return <%- type %>;
  }
<% } else if (type === "line") { -%>

  /**
   * Create a <%- type %> on the map from a LatLng coordinate.
   *
   * @param latLngs places to layout the <%- type %> on the map
   * @return the newly created <%- type %>
   */
  @UiThread
  public <%- camelize(type) %> create<%- camelize(type) %>(@NonNull List<LatLng> latLngs) {
    <%- camelize(type) %> <%- type %> = new <%- camelize(type) %>(this, currentId);
    <%- type %>.setLatLngs(latLngs);
    <%- type %>s.put(currentId, <%- type %>);
    currentId++;
    return <%- type %>;
  }
<% } else { -%>

  /**
   * Create a <%- type %> on the map from a LatLng coordinate.
   *
   * @param latLngs places to layout the <%- type %> on the map
   * @return the newly created <%- type %>
   */
  @UiThread
  public <%- camelize(type) %> create<%- camelize(type) %>(@NonNull List<List<LatLng>> latLngs) {
    <%- camelize(type) %> <%- type %> = new <%- camelize(type) %>(this, currentId);
    <%- type %>.setLatLngs(latLngs);
    <%- type %>s.put(currentId, <%- type %>);
    currentId++;
    return <%- type %>;
  }
<% } -%>

  /**
   * Delete a <%- type %> from the map.
   *
   * @param <%- type %> to be deleted
   */
  @UiThread
  public void delete<%- camelize(type) %>(@NonNull <%- camelize(type) %> <%- type %>) {
    <%- type %>s.remove(<%- type %>.getId());
    updateSource();
  }

  /**
   * Get a list of current <%- type %>s.
   *
   * @return list of <%- type %>s
   */
  @UiThread
  public LongSparseArray<<%- camelize(type) %>> get<%- camelize(type) %>s() {
    return <%- type %>s;
  }

  /**
   * Trigger an update to the underlying source
   */
  public void updateSource() {
    // todo move feature creation to a background thread?
    features.clear();
    <%- camelize(type) %> <%- type %>;
    for (int i = 0; i < <%- type %>s.size(); i++) {
      <%- type %> = <%- type %>s.valueAt(i);
      features.add(Feature.fromGeometry(<%- type %>.getGeometry(), <%- type %>.getFeature()));
    }
<% if (type === "symbol") { -%>
    Collections.sort(features, symbolComparator);
<% } -%>
    geoJsonSource.setGeoJson(FeatureCollection.fromFeatures(features));
  }

  /**
   * Add a callback to be invoked when a <%- type %> has been clicked.
   *
   * @param listener the callback to be invoked when a <%- type %> is clicked
   */
  @UiThread
  public void addOn<%- camelize(type) %>ClickListener(@NonNull On<%- camelize(type) %>ClickListener listener) {
    <%- type %>ClickListeners.add(listener);
  }

  /**
   * Remove a previously added callback that was to be invoked when <%- type %> has been clicked.
   *
   * @param listener the callback to be removed
   */
  @UiThread
  public void removeOn<%- camelize(type) %>ClickListener(@NonNull On<%- camelize(type) %>ClickListener listener) {
    if (<%- type %>ClickListeners.contains(listener)) {
      <%- type %>ClickListeners.remove(listener);
    }
  }

  private static PropertyValue<?>[] getLayerDefinition() {
    return new PropertyValue[]{
<% for (const property of properties) { -%>
<% if (supportsPropertyFunction(property) && property.name !== "fill-pattern" && property.name !== "line-pattern") { -%>
      <%- camelizeWithLeadingLowercase(property.name) %>(get("<%- property.name %>")),
<% } -%><% } -%>
<% if (type === "symbol") { -%>
      symbolZOrder(Property.SYMBOL_Z_ORDER_SOURCE)
<% } -%>
    };
  }

  // Property accessors
<% for (const property of properties) { -%>
<% if (!supportsPropertyFunction(property) && property.name !== "line-gradient" && property.name !== "symbol-z-order") { -%>
  /**
   * Get the <%- camelize(property.name) %> property
   *
   * @return property wrapper value around <%- propertyType(property) %>
   */
  <%- propertyTypeAnnotation(property) %><%- iff(() => propertyTypeAnnotation(property), "") %>
  public <%- propertyType(property) %> get<%- camelize(property.name) %>() {
    return layer.get<%- camelize(property.name) %>().value;
  }

  /**
   * Set the <%- camelize(property.name) %> property
   *
   * @param value property wrapper value around <%- propertyType(property) %>
   */
  public void set<%- camelize(property.name) %>(<%- propertyTypeAnnotation(property) %><%- iff(() => propertyTypeAnnotation(property), "") %> <%- propertyType(property) %> value) {
    layer.setProperties(<%- camelizeWithLeadingLowercase(property.name) %>(value));
  }

<% } -%>
<% } -%>
  /**
   * Inner class for transforming map click events into <%- type %> clicks
   */
  private class MapClickResolver implements MapboxMap.OnMapClickListener {

    private MapboxMap mapboxMap;

    private MapClickResolver(MapboxMap mapboxMap) {
      this.mapboxMap = mapboxMap;
    }

    @Override
    public void onMapClick(@NonNull LatLng point) {
      if (<%- type %>ClickListeners.isEmpty()) {
        return;
      }

      PointF screenLocation = mapboxMap.getProjection().toScreenLocation(point);
      List<Feature> features = mapboxMap.queryRenderedFeatures(screenLocation, ID_GEOJSON_LAYER);
      if (!features.isEmpty()) {
        long <%- type %>Id = features.get(0).getProperty(<%- camelize(type) %>.ID_KEY).getAsLong();
        <%- camelize(type) %> <%- type %> = <%- type %>s.get(<%- type %>Id);
        if (<%- type %> != null) {
          for (On<%- camelize(type) %>ClickListener listener : <%- type %>ClickListeners) {
            listener.on<%- camelize(type) %>Click(<%- type %>);
          }
        }
      }
    }
  }
<% if (type === "symbol") { -%>

  private class SymbolComparator implements Comparator<Feature> {
    @Override
    public int compare(Feature left, Feature right) {
      return right.getProperty(Z_INDEX).getAsInt() - left.getProperty(Z_INDEX).getAsInt();
    }
  }
<% } -%>
}