AI2 Component  (Version nb184)
Marker.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright © 2016-2017 Massachusetts Institute of Technology, 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 
12 import org.locationtech.jts.geom.Geometry;
13 import org.osmdroid.api.IGeoPoint;
14 import org.osmdroid.util.GeoPoint;
15 
33 
34 import android.util.Log;
35 
43 @DesignerComponent(version = YaVersion.MARKER_COMPONENT_VERSION,
44  category = ComponentCategory.MAPS,
45  description = "<p>An icon positioned at a point to indicate information on a map. Markers " +
46  "can be used to provide an info window, custom fill and stroke colors, and custom " +
47  "images to convey information to the user.</p>")
48 @SimpleObject
49 @UsesLibraries(libraries = "osmdroid.aar, androidsvg.jar")
50 public class Marker extends MapFeatureBaseWithFill implements MapMarker {
51  private static final String TAG = Marker.class.getSimpleName();
52 
57  private String imagePath = "";
58 
63  private int anchorHAlign = 3;
64 
69  private int anchorVAlign = 3;
70 
74  private GeoPoint location = new GeoPoint(0.0, 0.0);
75 
79  private int width = LENGTH_PREFERRED;
80 
84  private int height = LENGTH_PREFERRED;
85 
86  private static final MapFeatureVisitor<Double> distanceComputation = new MapFeatureVisitor<Double>() {
87  @Override
88  public Double visit(MapMarker marker, Object... arguments) {
89  return GeometryUtil.distanceBetween((Marker)arguments[0], marker);
90  }
91 
92  @Override
93  public Double visit(MapLineString lineString, Object... arguments) {
94  if ((Boolean) arguments[1]) {
95  return GeometryUtil.distanceBetweenCentroids((Marker) arguments[0], lineString);
96  } else {
97  return GeometryUtil.distanceBetweenEdges((Marker) arguments[0], lineString);
98  }
99  }
100 
101  @Override
102  public Double visit(MapPolygon polygon, Object... arguments) {
103  if ((Boolean) arguments[1]) {
104  return GeometryUtil.distanceBetweenCentroids((Marker) arguments[0], polygon);
105  } else {
106  return GeometryUtil.distanceBetweenEdges((Marker) arguments[0], polygon);
107  }
108  }
109 
110  @Override
111  public Double visit(MapCircle circle, Object... arguments) {
112  if ((Boolean) arguments[1]) {
113  return GeometryUtil.distanceBetweenCentroids((Marker) arguments[0], circle);
114  } else {
115  return GeometryUtil.distanceBetweenEdges((Marker) arguments[0], circle);
116  }
117  }
118 
119  @Override
120  public Double visit(MapRectangle rectangle, Object... arguments) {
121  if ((Boolean) arguments[1]) {
122  return GeometryUtil.distanceBetweenCentroids((Marker) arguments[0], rectangle);
123  } else {
124  return GeometryUtil.distanceBetweenEdges((Marker) arguments[0], rectangle);
125  }
126  }
127  };
128 
129  private static final MapFeatureVisitor<Double> bearingComputation = new MapFeatureVisitor<Double>() {
130  @Override
131  public Double visit(MapMarker marker, Object... arguments) {
132  return GeometryUtil.bearingTo((Marker) arguments[0], marker);
133  }
134 
135  @Override
136  public Double visit(MapLineString lineString, Object... arguments) {
137  if ((Boolean) arguments[1]) {
138  return GeometryUtil.bearingToCentroid((MapMarker) arguments[0], lineString);
139  } else {
140  return GeometryUtil.bearingToEdge((MapMarker) arguments[0], lineString);
141  }
142  }
143 
144  @Override
145  public Double visit(MapPolygon polygon, Object... arguments) {
146  if ((Boolean) arguments[1]) {
147  return GeometryUtil.bearingToCentroid((MapMarker) arguments[0], polygon);
148  } else {
149  return GeometryUtil.bearingToEdge((MapMarker) arguments[0], polygon);
150  }
151  }
152 
153  @Override
154  public Double visit(MapCircle circle, Object... arguments) {
155  if ((Boolean) arguments[1]) {
156  return GeometryUtil.bearingToCentroid((MapMarker) arguments[0], circle);
157  } else {
158  return GeometryUtil.bearingToEdge((MapMarker) arguments[0], circle);
159  }
160  }
161 
162  @Override
163  public Double visit(MapRectangle rectangle, Object... arguments) {
164  if ((Boolean) arguments[1]) {
165  return GeometryUtil.bearingToCentroid((MapMarker) arguments[0], rectangle);
166  } else {
167  return GeometryUtil.bearingToEdge((MapMarker) arguments[0], rectangle);
168  }
169  }
170  };
171 
179  super(container, distanceComputation);
180  container.addFeature(this);
181  ShowShadow(false);
183  AnchorVertical(ComponentConstants.GRAVITY_BOTTOM);
184  ImageAsset("");
185  Width(LENGTH_PREFERRED);
186  Height(LENGTH_PREFERRED);
187  Latitude(0);
188  Longitude(0);
189  }
190 
196  @Override
197  public String Type() {
198  return MapFactory.MapFeatureType.TYPE_MARKER;
199  }
200 
210  defaultValue = "0")
212  public void Latitude(double latitude) {
213  Log.d(TAG, "Latitude");
214  if (latitude < -90 || latitude > 90) {
215  container.$form().dispatchErrorOccurredEvent(this, "Latitude", ErrorMessages.ERROR_INVALID_LATITUDE, latitude);
216  } else {
217  location.setLatitude(latitude);
218  clearGeometry();
219  map.getController().updateFeaturePosition(this);
220  }
221  }
222 
227  public double Latitude() {
228  return location.getLatitude();
229  }
230 
240  defaultValue = "0")
242  public void Longitude(double longitude) {
243  Log.d(TAG, "Longitude");
244  if (longitude < -180 || longitude > 180) {
245  container.$form().dispatchErrorOccurredEvent(this, "Longitude", ErrorMessages.ERROR_INVALID_LONGITUDE, longitude);
246  } else {
247  location.setLongitude(longitude);
248  clearGeometry();
249  map.getController().updateFeaturePosition(this);
250  }
251  }
252 
257  public double Longitude() {
258  return location.getLongitude();
259  }
260 
269  public void ImageAsset(String path) {
270  Log.d(TAG, "ImageAsset");
271  this.imagePath = path;
272  map.getController().updateFeatureImage(this);
273  }
274 
279  @SimpleProperty(description = "The ImageAsset property is used to provide an alternative image " +
280  "for the Marker.")
281  public String ImageAsset() {
282  return imagePath;
283  }
284 
290  public void StrokeColor(int argb) {
291  super.StrokeColor(argb);
292  map.getController().updateFeatureStroke(this);
293  }
294 
295  @Override
297  defaultValue = "3")
299  public void AnchorHorizontal(int horizontal) {
300  if (horizontal == anchorHAlign) {
301  return;
302  } else if (horizontal > 3 || horizontal < 1) {
303  container.$form().dispatchErrorOccurredEvent(this, "AnchorHorizontal", ErrorMessages.ERROR_INVALID_ANCHOR_HORIZONTAL, horizontal);
304  return;
305  }
306  anchorHAlign = horizontal;
307  map.getController().updateFeaturePosition(this);
308  }
309 
314  @Override
315  @SimpleProperty(description = "The horizontal alignment property controls where the Marker's " +
316  "anchor is located relative to its width.")
317  public int AnchorHorizontal() {
318  return anchorHAlign;
319  }
320 
321  @Override
323  defaultValue = "3")
325  public void AnchorVertical(int vertical) {
326  if (vertical == anchorVAlign) {
327  return;
328  } else if (vertical > 3 || vertical < 1) {
329  container.$form().dispatchErrorOccurredEvent(this, "AnchorVertical", ErrorMessages.ERROR_INVALID_ANCHOR_VERTICAL, vertical);
330  return;
331  }
332  anchorVAlign = vertical;
333  map.getController().updateFeaturePosition(this);
334  }
335 
340  @Override
341  @SimpleProperty(description = "The vertical alignment property controls where the Marker's " +
342  "anchor is located relative to its height.")
343  public int AnchorVertical() {
344  return anchorVAlign;
345  }
346 
347  @Override
348  @SimpleProperty(userVisible = false)
349  public void ShowShadow(boolean show) {
350  // This method has been deprecated.
351  }
352 
353  @Override
354  @SimpleProperty(description = "Gets whether or not the shadow of the Marker is shown.")
355  public boolean ShowShadow() {
356  return false;
357  }
358 
364  @Override
366  public void Width(int width) {
367  this.width = width;
368  map.getController().updateFeatureSize(this);
369  }
370 
374  @Override
376  public int Width() {
377  if (this.width == LENGTH_FILL_PARENT) {
378  return map.getView().getWidth();
379  } else if (this.width < LENGTH_PERCENT_TAG) {
380  return (int)(((double) -this.width + LENGTH_PERCENT_TAG)/100.0 * map.getView().getWidth());
381  }
382  return this.width;
383  }
384 
391  @SuppressWarnings("squid:S00100")
393  public void WidthPercent(int pCent) {
394  this.width = LENGTH_PERCENT_TAG - pCent;
395  map.getController().updateFeatureSize(this);
396  }
397 
403  @Override
405  public void Height(int height) {
406  this.height = height;
407  map.getController().updateFeatureSize(this);
408  }
409 
413  @Override
415  public int Height() {
416  if (this.height == LENGTH_FILL_PARENT) {
417  return map.getView().getHeight();
418  } else if (this.height < LENGTH_PERCENT_TAG) {
419  return (int)(((double) -this.height + LENGTH_PERCENT_TAG)/100.0 * map.getView().getHeight());
420  }
421  return this.height;
422  }
423 
430  @SuppressWarnings("squid:S00100")
432  public void HeightPercent(int pCent) {
433  this.height = LENGTH_PERCENT_TAG - pCent;
434  map.getController().updateFeatureSize(this);
435  }
436 
443  @SimpleFunction(description = "Set the location of the marker.")
444  public void SetLocation(double latitude, double longitude) {
445  Log.d(TAG, "SetLocation");
446  location.setCoords(latitude, longitude);
447  clearGeometry();
448  map.getController().updateFeaturePosition(this);
449  }
450 
461  @Override
462  public double DistanceToPoint(double latitude, double longitude, boolean centroid) {
463  return DistanceToPoint(latitude, longitude);
464  }
465 
473  @SuppressWarnings("squid:S00100")
474  @SimpleFunction(description = "Compute the distance, in meters, between a Marker and a " +
475  "latitude, longitude point.")
476  public double DistanceToPoint(double latitude, double longitude) {
477  return GeometryUtil.distanceBetween(this, new GeoPoint(latitude, longitude));
478  }
479 
488  @SuppressWarnings("squid:S00100")
489  @SimpleFunction(description = "Returns the bearing from the Marker to the given latitude and " +
490  "longitude, in degrees " +
491  "from due north.")
492  public double BearingToPoint(double latitude, double longitude) {
493  return location.bearingTo(new GeoPoint(latitude, longitude));
494  }
495 
507  @SuppressWarnings("squid:S00100")
508  @SimpleFunction(description = "Returns the bearing from the Marker to the given map feature, " +
509  "in degrees from due north. If the centroids parameter is true, the bearing will be to the " +
510  "center of the map feature. Otherwise, the bearing will be computed to the point in the " +
511  "feature nearest the Marker.")
512  public double BearingToFeature(MapFeature mapFeature, final boolean centroids) {
513  return mapFeature == null ? -1 : mapFeature.accept(bearingComputation, this, centroids);
514  }
515 
516  @Override
517  public IGeoPoint getLocation() {
518  return location;
519  }
520 
521  @Override
522  public void updateLocation(double latitude, double longitude) {
523  this.location = new GeoPoint(latitude, longitude);
524  clearGeometry();
525  }
526 
527  @Override
528  public <T> T accept(MapFeatureVisitor<T> visitor, Object... arguments) {
529  return visitor.visit(this, arguments);
530  }
531 
532  @Override
533  protected Geometry computeGeometry() {
534  return GeometryUtil.createGeometry(location);
535  }
536 }
com.google.appinventor.components.runtime.util.MapFactory.MapCircle
Definition: MapFactory.java:1028
com.google.appinventor.components.runtime.Marker.Width
void Width(int width)
Definition: Marker.java:366
com.google.appinventor.components.runtime.Marker.getLocation
IGeoPoint getLocation()
Definition: Marker.java:517
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.annotations.UsesLibraries
Definition: UsesLibraries.java:21
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
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.util.GeometryUtil.distanceBetween
static double distanceBetween(IGeoPoint a, IGeoPoint b)
Definition: GeometryUtil.java:229
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_INVALID_LATITUDE
static final int ERROR_INVALID_LATITUDE
Definition: ErrorMessages.java:242
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.runtime.util.GeometryUtil.bearingTo
static double bearingTo(MapMarker from, MapMarker to)
Definition: GeometryUtil.java:399
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.Marker.updateLocation
void updateLocation(double latitude, double longitude)
Definition: Marker.java:522
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_INVALID_LONGITUDE
static final int ERROR_INVALID_LONGITUDE
Definition: ErrorMessages.java:243
com.google.appinventor.components
com.google.appinventor.components.runtime.Marker.StrokeColor
void StrokeColor(int argb)
Definition: Marker.java:290
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.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.runtime.Marker.computeGeometry
Geometry computeGeometry()
Definition: Marker.java:533
com.google.appinventor.components.runtime.util.GeometryUtil.distanceBetweenEdges
static double distanceBetweenEdges(MapMarker marker, MapLineString lineString)
Definition: GeometryUtil.java:249
com.google.appinventor.components.runtime.util.MapFactory.MapRectangle
Definition: MapFactory.java:1099
com.google.appinventor.components.runtime.util.GeometryUtil.distanceBetweenCentroids
static double distanceBetweenCentroids(MapMarker marker, MapLineString lineString)
Definition: GeometryUtil.java:327
com.google.appinventor.components.runtime.Marker.Marker
Marker(MapFactory.MapFeatureContainer container)
Definition: Marker.java:178
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureVisitor.visit
T visit(MapMarker marker, Object... arguments)
com.google.appinventor.components.runtime.Marker.DistanceToPoint
double DistanceToPoint(double latitude, double longitude, boolean centroid)
Definition: Marker.java:462
com.google.appinventor.components.runtime.util.MapFactory.MapMarker
Definition: MapFactory.java:1205
com.google.appinventor.components.runtime.Marker.Longitude
double Longitude()
Definition: Marker.java:257
com.google.appinventor.components.runtime.util.MapFactory.MapFeature
Definition: MapFactory.java:588
com.google.appinventor.components.runtime.Marker.Type
String Type()
Definition: Marker.java:197
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_HORIZONTAL_ALIGNMENT
static final String PROPERTY_TYPE_HORIZONTAL_ALIGNMENT
Definition: PropertyTypeConstants.java:42
com.google.appinventor.components.runtime.Marker.Latitude
void Latitude(double latitude)
Definition: Marker.java:212
com.google.appinventor.components.runtime.Marker.AnchorVertical
void AnchorVertical(int vertical)
Definition: Marker.java:325
com.google.appinventor.components.runtime.Marker.AnchorHorizontal
void AnchorHorizontal(int horizontal)
Definition: Marker.java:299
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.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.Marker.Height
void Height(int height)
Definition: Marker.java:405
com.google.appinventor.components.runtime.util.GeometryUtil.createGeometry
static Geometry createGeometry(GeoPoint point)
Definition: GeometryUtil.java:120
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Marker.ImageAsset
void ImageAsset(String path)
Definition: Marker.java:269
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_INVALID_ANCHOR_HORIZONTAL
static final int ERROR_INVALID_ANCHOR_HORIZONTAL
Definition: ErrorMessages.java:246
com.google.appinventor.components.common.ComponentConstants.GRAVITY_CENTER_HORIZONTAL
static final int GRAVITY_CENTER_HORIZONTAL
Definition: ComponentConstants.java:57
com.google.appinventor.components.common.ComponentConstants.GRAVITY_BOTTOM
static final int GRAVITY_BOTTOM
Definition: ComponentConstants.java:61
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureType
Definition: MapFactory.java:1483
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.Marker.Height
int Height()
Definition: Marker.java:415
com.google
com
com.google.appinventor.components.runtime.Marker.Width
int Width()
Definition: Marker.java:376
com.google.appinventor.components.runtime.Marker
Definition: Marker.java:50
com.google.appinventor.components.runtime.util.GeometryUtil.bearingToCentroid
static double bearingToCentroid(MapMarker from, MapLineString to)
Definition: GeometryUtil.java:422
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_VERTICAL_ALIGNMENT
static final String PROPERTY_TYPE_VERTICAL_ALIGNMENT
Definition: PropertyTypeConstants.java:43
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_LONGITUDE
static final String PROPERTY_TYPE_LONGITUDE
Definition: PropertyTypeConstants.java:169
com.google.appinventor.components.common.ComponentConstants
Definition: ComponentConstants.java:13
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_INVALID_ANCHOR_VERTICAL
static final int ERROR_INVALID_ANCHOR_VERTICAL
Definition: ErrorMessages.java:245
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer
Definition: MapFactory.java:800
com.google.appinventor.components.runtime.util.GeometryUtil.bearingToEdge
static double bearingToEdge(MapMarker from, MapLineString to)
Definition: GeometryUtil.java:403
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureVisitor
Definition: MapFactory.java:745
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_ASSET
static final String PROPERTY_TYPE_ASSET
Definition: PropertyTypeConstants.java:22
com.google.appinventor.components.runtime.Marker.Latitude
double Latitude()
Definition: Marker.java:227
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_LATITUDE
static final String PROPERTY_TYPE_LATITUDE
Definition: PropertyTypeConstants.java:94
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.Marker.Longitude
void Longitude(double longitude)
Definition: Marker.java:242
com.google.appinventor
com.google.appinventor.components.runtime.MapFeatureBaseWithFill
Definition: MapFeatureBaseWithFill.java:20