AI2 Component  (Version nb184)
MapFeatureBase.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2016-2017 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime;
7 
28 import org.locationtech.jts.geom.Geometry;
29 import org.osmdroid.util.GeoPoint;
30 
31 @SimpleObject
32 public abstract class MapFeatureBase implements MapFeature, HasStroke {
33  protected MapFeatureContainer container = null;
34  protected Map map = null;
35  private boolean visible = true;
36  private int strokeColor = COLOR_BLACK;
37  private float strokeOpacity = 1;
38  private int strokeWidth = 1;
39  private String title = "";
40  private String description = "";
41  private boolean draggable = false;
42  private boolean infobox = false;
43  private GeoPoint centroid = null;
44  private final MapFeatureVisitor<Double> distanceComputation;
45  private MapFeatureVisitor<Double> distanceToPoint = new MapFeatureVisitor<Double>() {
46  @Override
47  public Double visit(MapMarker marker, Object... arguments) {
48  return GeometryUtil.distanceBetween(marker, (GeoPoint) arguments[0]);
49  }
50 
51  @Override
52  public Double visit(MapLineString lineString, Object... arguments) {
53  if ((Boolean) arguments[1]) {
54  return GeometryUtil.distanceBetweenCentroids(lineString, (GeoPoint) arguments[0]);
55  } else {
56  return GeometryUtil.distanceBetweenEdges(lineString, (GeoPoint) arguments[0]);
57  }
58  }
59 
60  @Override
61  public Double visit(MapPolygon polygon, Object... arguments) {
62  if ((Boolean) arguments[1]) {
63  return GeometryUtil.distanceBetweenCentroids(polygon, (GeoPoint) arguments[0]);
64  } else {
65  return GeometryUtil.distanceBetweenEdges(polygon, (GeoPoint) arguments[0]);
66  }
67  }
68 
69  @Override
70  public Double visit(MapCircle circle, Object... arguments) {
71  if ((Boolean) arguments[1]) {
72  return GeometryUtil.distanceBetweenCentroids(circle, (GeoPoint) arguments[0]);
73  } else {
74  return GeometryUtil.distanceBetweenEdges(circle, (GeoPoint) arguments[0]);
75  }
76  }
77 
78  @Override
79  public Double visit(MapRectangle rectangle, Object... arguments) {
80  if ((Boolean) arguments[1]) {
81  return GeometryUtil.distanceBetweenCentroids(rectangle, (GeoPoint) arguments[0]);
82  } else {
83  return GeometryUtil.distanceBetweenEdges(rectangle, (GeoPoint) arguments[0]);
84  }
85  }
86  };
87 
92  private Geometry geometry = null;
93 
94  @SuppressWarnings("WeakerAccess")
96  MapFeatureVisitor<Double> distanceComputation) {
97  this.container = container;
98  this.map = container.getMap();
99  this.distanceComputation = distanceComputation;
100  Description("");
101  Draggable(false);
102  EnableInfobox(false);
104  StrokeOpacity(1);
105  StrokeWidth(1);
106  Title("");
107  Visible(true);
108  }
109 
111  this.map = container.getMap();
112  }
113 
114  @Override
115  public void removeFromMap() {
117  }
118 
119  @SuppressWarnings("squid:S00100")
121  defaultValue = "True")
123  public void Visible(boolean visibility) {
124  if (visible == visibility) {
125  return;
126  }
127  this.visible = visibility;
128  if (this.visible) {
129  map.getController().showFeature(this);
130  } else {
131  map.getController().hideFeature(this);
132  }
133  map.getView().invalidate();
134  }
135 
141  description = "Specifies whether the %type% should be visible on the screen. "
142  + "Value is true if the component is showing and false if hidden.")
143  public boolean Visible() {
144  return visible;
145  }
146 
147  @Override
149  defaultValue = Component.DEFAULT_VALUE_COLOR_BLACK)
151  public void StrokeColor(int argb) {
152  strokeColor = argb;
154  }
155 
159  @Override
161  description = "The paint color used to outline the %type%.")
162  @IsColor
163  public int StrokeColor() {
164  return strokeColor;
165  }
166 
167  @Override
169  defaultValue = "1.0")
171  public void StrokeOpacity(float opacity) {
172  strokeOpacity = opacity;
173  strokeColor = (strokeColor & 0x00FFFFFF) | (Math.round(0xFF * opacity) << 24);
175  }
176 
181  @Override
183  description = "The opacity of the stroke used to outline the map feature.")
184  public float StrokeOpacity() {
185  return strokeOpacity;
186  }
187 
188  @Override
190  defaultValue = "1")
192  public void StrokeWidth(int width) {
193  strokeWidth = width;
195  }
196 
200  @Override
202  description = "The width of the stroke used to outline the %type%.")
203  public int StrokeWidth() {
204  return strokeWidth;
205  }
206 
207  @Override
209  defaultValue = "False")
211  public void Draggable(boolean draggable) {
212  this.draggable = draggable;
214  }
215 
220  @Override
221  @SimpleProperty(description = "The Draggable property is used to set whether or not the user " +
222  "can drag the %type% by long-pressing and then dragging the %type% to a new location.")
223  public boolean Draggable() {
224  return this.draggable;
225  }
226 
227  @Override
230  public void Title(String title) {
231  this.title = title;
233  }
234 
239  @Override
241  description = "The title displayed in the info window that appears when the user clicks " +
242  "on the %type%.")
243  public String Title() {
244  return title;
245  }
246 
247  @Override
250  public void Description(String description) {
251  this.description = description;
253  }
254 
259  @Override
261  description = "The description displayed in the info window that appears when the user " +
262  "clicks on the %type%.")
263  public String Description() {
264  return description;
265  }
266 
267  @Override
269  defaultValue = "False")
271  public void EnableInfobox(boolean enable) {
272  this.infobox = enable;
274  }
275 
279  @Override
281  description = "Enable or disable the infobox window display when the user taps the %type%.")
282  public boolean EnableInfobox() {
283  return infobox;
284  }
285 
290  @Override
291  @SimpleFunction(description = "Show the infobox for the %type%. This will show the infobox " +
292  "even if EnableInfobox is set to false.")
293  public void ShowInfobox() {
294  map.getController().showInfobox(this);
295  }
296 
300  @Override
301  @SimpleFunction(description = "Hide the infobox if it is shown. If the infobox is not " +
302  "visible this function has no effect.")
303  public void HideInfobox() {
304  map.getController().hideInfobox(this);
305  }
306 
307  @SuppressWarnings("squid:S00100")
308  public YailList Centroid() {
310  }
311 
326  @SuppressWarnings("squid:S00100")
327  @SimpleFunction(description = "Compute the distance, in meters, between a %type% and a " +
328  "latitude, longitude point.")
329  public double DistanceToPoint(double latitude, double longitude, boolean centroid) {
330  return accept(distanceToPoint, new GeoPoint(latitude, longitude), centroid);
331  }
332 
345  @SuppressWarnings("squid:S00100")
346  @SimpleFunction(description = "Compute the distance, in meters, between two map features.")
347  public double DistanceToFeature(MapFeature mapFeature, final boolean centroids) {
348  return mapFeature == null ? -1 : mapFeature.accept(distanceComputation, this, centroids);
349  }
350 
351  // Component Events
352 
356  @Override
357  @SimpleEvent(description = "The user clicked on the %type%.")
358  public void Click() {
359  EventDispatcher.dispatchEvent(this, "Click");
360  container.FeatureClick(this);
361  }
362 
368  @Override
369  @SimpleEvent(description = "The user long-pressed on the %type%. This event will only " +
370  "trigger if Draggable is false.")
371  public void LongClick() {
372  EventDispatcher.dispatchEvent(this, "LongClick");
374  }
375 
381  @Override
382  @SimpleEvent(description = "The user started a drag operation.")
383  public void StartDrag() {
384  EventDispatcher.dispatchEvent(this, "StartDrag");
386  }
387 
391  @Override
392  @SimpleEvent(description = "The user dragged the %type%.")
393  public void Drag() {
394  EventDispatcher.dispatchEvent(this, "Drag");
395  container.FeatureDrag(this);
396  }
397 
401  @Override
402  @SimpleEvent(description = "The user stopped a drag operation.")
403  public void StopDrag() {
404  EventDispatcher.dispatchEvent(this, "StopDrag");
406  }
407 
408  @Override
410  return map.getDispatchDelegate();
411  }
412 
413  @Override
414  public final synchronized GeoPoint getCentroid() {
415  if (centroid == null) {
417  }
418  return centroid;
419  }
420 
421  @Override
422  public final synchronized Geometry getGeometry() {
423  if (geometry == null) {
424  geometry = computeGeometry();
425  }
426  return geometry;
427  }
428 
429  @SuppressWarnings("WeakerAccess")
430  protected final synchronized void clearGeometry() {
431  centroid = null;
432  geometry = null;
433  }
434 
435  protected abstract Geometry computeGeometry();
436 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.util.MapFactory.MapController.hideFeature
void hideFeature(MapFeature feature)
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_FLOAT
static final String PROPERTY_TYPE_FLOAT
Definition: PropertyTypeConstants.java:76
com.google.appinventor.components.runtime.MapFeatureBase.StrokeOpacity
float StrokeOpacity()
Definition: MapFeatureBase.java:184
com.google.appinventor.components.runtime.util.YailList
Definition: YailList.java:26
com.google.appinventor.components.runtime.util.MapFactory.MapCircle
Definition: MapFactory.java:1028
com.google.appinventor.components.runtime.util.GeometryUtil.asYailList
static YailList asYailList(IGeoPoint point)
Definition: GeometryUtil.java:80
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.HandlesEventDispatching
Definition: HandlesEventDispatching.java:15
com.google.appinventor.components.runtime.MapFeatureBase.StrokeOpacity
void StrokeOpacity(float opacity)
Definition: MapFeatureBase.java:171
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.util.MapFactory
Definition: MapFactory.java:30
com.google.appinventor.components.runtime.MapFeatureBase.Draggable
boolean Draggable()
Definition: MapFeatureBase.java:223
com.google.appinventor.components.runtime.util.GeometryUtil.distanceBetween
static double distanceBetween(IGeoPoint a, IGeoPoint b)
Definition: GeometryUtil.java:229
com.google.appinventor.components.runtime.MapFeatureBase.StrokeWidth
void StrokeWidth(int width)
Definition: MapFeatureBase.java:192
com.google.appinventor.components.runtime.MapFeatureBase.Title
void Title(String title)
Definition: MapFeatureBase.java:230
com.google.appinventor.components.runtime.MapFeatureBase.HideInfobox
void HideInfobox()
Definition: MapFeatureBase.java:303
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer.FeatureLongClick
void FeatureLongClick(MapFeature feature)
com.google.appinventor.components.runtime.MapFeatureBase.Drag
void Drag()
Definition: MapFeatureBase.java:393
com.google.appinventor.components.runtime.MapFeatureBase.DistanceToPoint
double DistanceToPoint(double latitude, double longitude, boolean centroid)
Definition: MapFeatureBase.java:329
com.google.appinventor.components.runtime.util.MapFactory.MapController.updateFeatureStroke
void updateFeatureStroke(HasStroke feature)
com.google.appinventor.components
com.google.appinventor.components.runtime.util.MapFactory.MapLineString
Definition: MapFactory.java:1373
com.google.appinventor.components.runtime.util.MapFactory.MapPolygon
Definition: MapFactory.java:1410
com.google.appinventor.components.runtime.MapFeatureBase.EnableInfobox
boolean EnableInfobox()
Definition: MapFeatureBase.java:282
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer.FeatureStopDrag
void FeatureStopDrag(MapFeature feature)
com.google.appinventor.components.runtime.MapFeatureBase.Visible
boolean Visible()
Definition: MapFeatureBase.java:143
com.google.appinventor.components.runtime.Map.getController
MapController getController()
Definition: Map.java:667
com.google.appinventor.components.runtime.MapFeatureBase.EnableInfobox
void EnableInfobox(boolean enable)
Definition: MapFeatureBase.java:271
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer.getMap
Map getMap()
com.google.appinventor.components.runtime.util.MapFactory.MapFeature.accept
< T > T accept(MapFeatureVisitor< T > visitor, Object... arguments)
com.google.appinventor.components.runtime.util.MapFactory.MapController.updateFeatureDraggable
void updateFeatureDraggable(MapFeature feature)
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.runtime.MapFeatureBase.StrokeColor
int StrokeColor()
Definition: MapFeatureBase.java:163
com.google.appinventor.components.runtime.util.GeometryUtil.distanceBetweenEdges
static double distanceBetweenEdges(MapMarker marker, MapLineString lineString)
Definition: GeometryUtil.java:249
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.util.MapFactory.MapRectangle
Definition: MapFactory.java:1099
com.google.appinventor.components.runtime.MapFeatureBase
Definition: MapFeatureBase.java:32
com.google.appinventor.components.runtime.MapFeatureBase.Description
void Description(String description)
Definition: MapFeatureBase.java:250
com.google.appinventor.components.runtime.util.GeometryUtil.distanceBetweenCentroids
static double distanceBetweenCentroids(MapMarker marker, MapLineString lineString)
Definition: GeometryUtil.java:327
com.google.appinventor.components.runtime.MapFeatureBase.setMap
void setMap(MapFactory.MapFeatureContainer container)
Definition: MapFeatureBase.java:110
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_INTEGER
static final String PROPERTY_TYPE_INTEGER
Definition: PropertyTypeConstants.java:88
com.google.appinventor.components.runtime.MapFeatureBase.Draggable
void Draggable(boolean draggable)
Definition: MapFeatureBase.java:211
com.google.appinventor.components.runtime.MapFeatureBase.container
MapFeatureContainer container
Definition: MapFeatureBase.java:33
com.google.appinventor.components.runtime.MapFeatureBase.StartDrag
void StartDrag()
Definition: MapFeatureBase.java:383
com.google.appinventor.components.runtime.MapFeatureBase.getCentroid
final synchronized GeoPoint getCentroid()
Definition: MapFeatureBase.java:414
com.google.appinventor.components.runtime.MapFeatureBase.map
Map map
Definition: MapFeatureBase.java:34
com.google.appinventor.components.runtime.util.MapFactory.MapMarker
Definition: MapFactory.java:1205
com.google.appinventor.components.runtime.util.MapFactory.MapFeature
Definition: MapFactory.java:588
com.google.appinventor.components.runtime.MapFeatureBase.LongClick
void LongClick()
Definition: MapFeatureBase.java:371
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer.FeatureDrag
void FeatureDrag(MapFeature feature)
com.google.appinventor.components.runtime.util.MapFactory.MapController.showFeature
void showFeature(MapFeature feature)
com.google.appinventor.components.runtime.AndroidViewComponent.getDispatchDelegate
HandlesEventDispatching getDispatchDelegate()
Definition: AndroidViewComponent.java:276
com.google.appinventor.components.runtime.util.MapFactory.MapController.hideInfobox
void hideInfobox(MapFeature feature)
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.util.GeometryUtil
Definition: GeometryUtil.java:41
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer.FeatureClick
void FeatureClick(MapFeature feature)
com.google.appinventor.components.runtime.util.MapFactory.HasStroke
Definition: MapFactory.java:919
com.google.appinventor.components.runtime.Map.getView
View getView()
Definition: Map.java:123
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_VISIBILITY
static final String PROPERTY_TYPE_VISIBILITY
Definition: PropertyTypeConstants.java:278
com.google.appinventor.components.runtime.util.MapFactory.MapController.updateFeatureText
void updateFeatureText(MapFeature feature)
com.google.appinventor.components.runtime.Component.COLOR_BLACK
static final int COLOR_BLACK
Definition: Component.java:55
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer.FeatureStartDrag
void FeatureStartDrag(MapFeature feature)
com.google.appinventor.components.runtime.MapFeatureBase.StopDrag
void StopDrag()
Definition: MapFeatureBase.java:403
com.google.appinventor.components.runtime.util.GeometryUtil.jtsPointToGeoPoint
static GeoPoint jtsPointToGeoPoint(Point p)
Definition: GeometryUtil.java:221
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.MapFeatureBase.getGeometry
final synchronized Geometry getGeometry()
Definition: MapFeatureBase.java:422
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.runtime.util.MapFactory.MapController.showInfobox
void showInfobox(MapFeature feature)
com.google.appinventor.components.runtime.Map
Definition: Map.java:84
com.google.appinventor.components.runtime.MapFeatureBase.Title
String Title()
Definition: MapFeatureBase.java:243
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.runtime.MapFeatureBase.StrokeWidth
int StrokeWidth()
Definition: MapFeatureBase.java:203
com.google.appinventor.components.runtime.MapFeatureBase.getDispatchDelegate
HandlesEventDispatching getDispatchDelegate()
Definition: MapFeatureBase.java:409
com.google.appinventor.components.runtime.MapFeatureBase.StrokeColor
void StrokeColor(int argb)
Definition: MapFeatureBase.java:151
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.MapFeatureBase.ShowInfobox
void ShowInfobox()
Definition: MapFeatureBase.java:293
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_COLOR
static final String PROPERTY_TYPE_COLOR
Definition: PropertyTypeConstants.java:63
com.google.appinventor.components.runtime.MapFeatureBase.Visible
void Visible(boolean visibility)
Definition: MapFeatureBase.java:123
com.google
com
com.google.appinventor.components.runtime.MapFeatureBase.Description
String Description()
Definition: MapFeatureBase.java:263
com.google.appinventor.components.runtime.MapFeatureBase.clearGeometry
final synchronized void clearGeometry()
Definition: MapFeatureBase.java:430
com.google.appinventor.components.runtime.MapFeatureBase.Centroid
YailList Centroid()
Definition: MapFeatureBase.java:308
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer
Definition: MapFactory.java:800
com.google.appinventor.components.runtime.MapFeatureBase.Click
void Click()
Definition: MapFeatureBase.java:358
com.google.appinventor.components.runtime.MapFeatureBase.DistanceToFeature
double DistanceToFeature(MapFeature mapFeature, final boolean centroids)
Definition: MapFeatureBase.java:347
com.google.appinventor.components.annotations.IsColor
Definition: IsColor.java:13
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureVisitor
Definition: MapFactory.java:745
com.google.appinventor.components.annotations.PropertyCategory.APPEARANCE
APPEARANCE
Definition: PropertyCategory.java:16
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor.components.runtime.Component.DEFAULT_VALUE_COLOR_BLACK
static final String DEFAULT_VALUE_COLOR_BLACK
Definition: Component.java:71
com.google.appinventor.components.runtime.MapFeatureBase.computeGeometry
abstract Geometry computeGeometry()
com.google.appinventor.components.runtime.MapFeatureBase.removeFromMap
void removeFromMap()
Definition: MapFeatureBase.java:115
com.google.appinventor
com.google.appinventor.components.runtime.util.MapFactory.MapController.removeFeature
void removeFeature(MapFeature feature)