/
AnnotationProxy.java
549 lines (492 loc) · 16.4 KB
/
AnnotationProxy.java
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2013-present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.map;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Message;
import android.util.Property;
import android.view.View;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.HashMap;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.AsyncResult;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiMessenger;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.TiPoint;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.view.TiDrawableReference;
@Kroll.proxy(creatableInModule = MapModule.class,
propertyAccessors = { TiC.PROPERTY_SUBTITLE, TiC.PROPERTY_SUBTITLEID, TiC.PROPERTY_TITLE,
TiC.PROPERTY_TITLEID, TiC.PROPERTY_LATITUDE, TiC.PROPERTY_LONGITUDE,
MapModule.PROPERTY_DRAGGABLE, TiC.PROPERTY_IMAGE, TiC.PROPERTY_PINCOLOR,
MapModule.PROPERTY_CUSTOM_VIEW, TiC.PROPERTY_LEFT_BUTTON, TiC.PROPERTY_LEFT_VIEW,
TiC.PROPERTY_RIGHT_BUTTON, TiC.PROPERTY_RIGHT_VIEW,
MapModule.PROPERTY_SHOW_INFO_WINDOW, MapModule.PROPERTY_CENTER_OFFSET,
MapModule.PROPERTY_HIDDEN, MapModule.PROPERTY_CLUSTER_IDENTIFIER })
public class AnnotationProxy extends KrollProxy
{
public interface AnnotationDelegate
{
public void refreshAnnotation(AnnotationProxy annotation);
}
private static final String TAG = "AnnotationProxy";
private MarkerOptions markerOptions;
private TiMarker marker;
private TiMapInfoWindow infoWindow = null;
private static final String defaultIconImageHeight = "40dip"; //The height of the default marker icon
private static final String defaultIconImageWidth = "36dip"; //The width of the default marker icon
// The height of the marker icon in the unit of "px". Will use it to analyze the touch event to find out
// the correct clicksource for the click event.
private int iconImageHeight = 0;
private int iconImageWidth = 0;
private String subTitle;
private String annoTitle;
private AnnotationDelegate delegate = null;
private static final int MSG_FIRST_ID = KrollProxy.MSG_LAST_ID + 1;
private static final int MSG_SET_LON = MSG_FIRST_ID + 300;
private static final int MSG_SET_LAT = MSG_FIRST_ID + 301;
private static final int MSG_SET_DRAGGABLE = MSG_FIRST_ID + 302;
private static final int MSG_UPDATE_INFO_WINDOW = MSG_FIRST_ID + 303;
private static final int MSG_SET_HIDDEN = MSG_FIRST_ID + 304;
private static final int MSG_SET_IMAGE = MSG_FIRST_ID + 305;
public AnnotationProxy()
{
super();
markerOptions = new MarkerOptions();
subTitle = "";
annoTitle = "";
defaultValues.put(MapModule.PROPERTY_SHOW_INFO_WINDOW, true);
defaultValues.put(MapModule.PROPERTY_CLUSTER_IDENTIFIER, null);
}
@Override
public void release()
{
if (hasProperty(MapModule.PROPERTY_CUSTOM_VIEW)) {
TiViewProxy customView = (TiViewProxy) getProperty(MapModule.PROPERTY_CUSTOM_VIEW);
customView.release();
}
/*
if (markerOptions != null) {
markerOptions = null;
}
if (clusterMarker != null) {
clusterMarker = null;
}
if (infoWindow != null) {
infoWindow = null;
}
if (delegate != null) {
delegate = null;
}
*/
super.release();
}
public void releaseMarker()
{
if (marker != null) {
marker = null;
}
}
public void setDelegate(AnnotationDelegate delegate)
{
this.delegate = delegate;
}
@Override
protected KrollDict getLangConversionTable()
{
KrollDict table = new KrollDict();
table.put(TiC.PROPERTY_SUBTITLE, TiC.PROPERTY_SUBTITLEID);
table.put(TiC.PROPERTY_TITLE, TiC.PROPERTY_TITLEID);
return table;
}
public String getTitle()
{
return annoTitle;
}
public String getSubtitle()
{
return subTitle;
}
@Override
public boolean handleMessage(Message msg)
{
AsyncResult result = null;
switch (msg.what) {
case MSG_SET_LON: {
result = (AsyncResult) msg.obj;
setPosition(TiConvert.toDouble(getProperty(TiC.PROPERTY_LATITUDE)), (Double) result.getArg());
result.setResult(null);
return true;
}
case MSG_SET_LAT: {
result = (AsyncResult) msg.obj;
setPosition((Double) result.getArg(), TiConvert.toDouble(getProperty(TiC.PROPERTY_LONGITUDE)));
result.setResult(null);
return true;
}
case MSG_SET_HIDDEN: {
result = (AsyncResult) msg.obj;
Marker m = marker.getMarker();
if (m != null) {
m.setVisible(!(Boolean) result.getArg());
}
result.setResult(null);
return true;
}
case MSG_SET_DRAGGABLE: {
result = (AsyncResult) msg.obj;
Marker m = marker.getMarker();
if (m != null) {
m.setDraggable((Boolean) result.getArg());
}
result.setResult(null);
return true;
}
case MSG_UPDATE_INFO_WINDOW: {
updateInfoWindow();
return true;
}
case MSG_SET_IMAGE:
result = (AsyncResult) msg.obj;
Marker m = marker.getMarker();
if (m != null) {
updateImage(m, result.getArg());
}
result.setResult(null);
return true;
default: {
return super.handleMessage(msg);
}
}
}
public void setPosition(double latitude, double longitude)
{
Marker m = marker.getMarker();
if (m != null) {
animateMarkerToPosition(m, new LatLng(latitude, longitude));
}
}
private void animateMarkerToPosition(Marker marker, LatLng finalPosition)
{
TypeEvaluator<LatLng> typeEvaluator = (fraction, startValue, endValue) ->
{
double lat = (endValue.latitude - startValue.latitude) * fraction + startValue.latitude;
double lng = (endValue.longitude - startValue.longitude) * fraction + startValue.longitude;
return new LatLng(lat, lng);
};
Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position");
ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition);
animator.setDuration(200);
animator.start();
}
public void processOptions()
{
double longitude = 0;
double latitude = 0;
if (hasProperty(TiC.PROPERTY_LONGITUDE)) {
longitude = TiConvert.toDouble(getProperty(TiC.PROPERTY_LONGITUDE));
}
if (hasProperty(TiC.PROPERTY_LATITUDE)) {
latitude = TiConvert.toDouble(getProperty(TiC.PROPERTY_LATITUDE));
}
markerOptions.position(new LatLng(latitude, longitude));
if (hasProperty(TiC.PROPERTY_LEFT_BUTTON) || hasProperty(TiC.PROPERTY_LEFT_VIEW)
|| hasProperty(TiC.PROPERTY_RIGHT_BUTTON) || hasProperty(TiC.PROPERTY_RIGHT_VIEW)
|| hasProperty(TiC.PROPERTY_TITLE) || hasProperty(TiC.PROPERTY_SUBTITLE)) {
getOrCreateMapInfoWindow();
Object leftButton = getProperty(TiC.PROPERTY_LEFT_BUTTON);
Object leftView = getProperty(TiC.PROPERTY_LEFT_VIEW);
Object rightButton = getProperty(TiC.PROPERTY_RIGHT_BUTTON);
Object rightView = getProperty(TiC.PROPERTY_RIGHT_VIEW);
if (leftButton != null) {
infoWindow.setLeftOrRightPane(leftButton, TiMapInfoWindow.LEFT_PANE);
} else {
infoWindow.setLeftOrRightPane(leftView, TiMapInfoWindow.LEFT_PANE);
}
if (rightButton != null) {
infoWindow.setLeftOrRightPane(rightButton, TiMapInfoWindow.RIGHT_PANE);
} else {
infoWindow.setLeftOrRightPane(rightView, TiMapInfoWindow.RIGHT_PANE);
}
if (hasProperty(TiC.PROPERTY_TITLE)) {
String title = TiConvert.toString(getProperty(TiC.PROPERTY_TITLE));
annoTitle = title;
infoWindow.setTitle(title);
} else {
infoWindow.setTitle(null);
}
if (hasProperty(TiC.PROPERTY_SUBTITLE)) {
infoWindow.setSubtitle(TiConvert.toString(getProperty(TiC.PROPERTY_SUBTITLE)));
subTitle = TiConvert.toString(getProperty(TiC.PROPERTY_SUBTITLE));
} else {
infoWindow.setSubtitle(null);
}
}
if (hasProperty(MapModule.PROPERTY_DRAGGABLE)) {
markerOptions.draggable(TiConvert.toBoolean(getProperty(MapModule.PROPERTY_DRAGGABLE)));
}
if (hasProperty(MapModule.PROPERTY_HIDDEN)) {
markerOptions.visible(!TiConvert.toBoolean(getProperty(MapModule.PROPERTY_HIDDEN)));
}
// customView, image and pincolor must be defined before adding to mapview. Once added, their values are final.
if (hasProperty(MapModule.PROPERTY_CUSTOM_VIEW)) {
handleCustomView(getProperty(MapModule.PROPERTY_CUSTOM_VIEW));
} else if (hasProperty(TiC.PROPERTY_IMAGE)) {
handleImage(getProperty(TiC.PROPERTY_IMAGE));
} else if (hasProperty(TiC.PROPERTY_PINCOLOR)) {
markerOptions.icon(
BitmapDescriptorFactory.defaultMarker(TiConvert.toFloat(getProperty(TiC.PROPERTY_PINCOLOR))));
setIconImageDimensions(-1, -1);
} else {
setIconImageDimensions(-1, -1);
}
if (hasProperty(MapModule.PROPERTY_CENTER_OFFSET)) {
HashMap centerOffsetProperty = (HashMap) getProperty(MapModule.PROPERTY_CENTER_OFFSET);
TiPoint centerOffset = new TiPoint(centerOffsetProperty, 0.0, 0.0);
float offsetX = 0.5f - ((float) centerOffset.getX().getValue() / (float) iconImageWidth);
float offsetY = 0.5f - ((float) centerOffset.getY().getValue() / (float) iconImageHeight);
markerOptions.anchor(offsetX, offsetY);
}
}
private void handleCustomView(Object obj)
{
if (obj instanceof TiViewProxy) {
TiBlob imageBlob = ((TiViewProxy) obj).toImage();
Bitmap image = imageBlob.getImage();
if (image != null) {
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(image));
setIconImageDimensions(image.getWidth(), image.getHeight());
}
return;
}
Log.w(TAG, "Unable to get the image from the custom view: " + obj);
setIconImageDimensions(-1, -1);
}
private void handleImage(Object image)
{
// Image path
if (image instanceof String) {
TiDrawableReference imageref = TiDrawableReference.fromUrl(this, (String) image);
Bitmap bitmap = imageref.getBitmap();
if (bitmap != null) {
try {
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
setIconImageDimensions(bitmap.getWidth(), bitmap.getHeight());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return;
}
}
// Image blob
if (image instanceof TiBlob) {
Bitmap bitmap = ((TiBlob) image).getImage();
if (bitmap != null) {
try {
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
setIconImageDimensions(bitmap.getWidth(), bitmap.getHeight());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return;
}
}
Log.w(TAG, "Unable to get the image from the path: " + image);
setIconImageDimensions(-1, -1);
}
public MarkerOptions getMarkerOptions()
{
return markerOptions;
}
public void updateImage(Marker m, Object value)
{
if (hasProperty(MapModule.PROPERTY_CUSTOM_VIEW)) {
// Custom view used. Update image not allowed
return;
}
if (value == null) {
m.setIcon(BitmapDescriptorFactory.defaultMarker(TiConvert.toFloat(getProperty(TiC.PROPERTY_PINCOLOR))));
setIconImageDimensions(-1, -1);
return;
}
// image not null has only effect if customView is null. Any other case, customView has more priority
if (value != null) {
// Image path
if (value instanceof String) {
TiDrawableReference imageref = TiDrawableReference.fromUrl(this, (String) value);
Bitmap bitmap = imageref.getBitmap();
if (bitmap != null) {
try {
if (m != null) {
m.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
setIconImageDimensions(bitmap.getWidth(), bitmap.getHeight());
}
} catch (Exception e) {
}
return;
}
}
// Image blob
if (value instanceof TiBlob) {
Bitmap bitmap = ((TiBlob) value).getImage();
if (bitmap != null) {
if (m != null) {
m.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
setIconImageDimensions(bitmap.getWidth(), bitmap.getHeight());
}
return;
}
}
}
}
public void setTiMarker(TiMarker m)
{
marker = m;
}
public TiMarker getTiMarker()
{
return marker;
}
public void showInfo()
{
if (marker == null) {
return;
}
Marker m = marker.getMarker();
if (m != null) {
m.showInfoWindow();
}
}
public void hideInfo()
{
if (marker == null) {
return;
}
Marker m = marker.getMarker();
if (m != null) {
m.hideInfoWindow();
}
}
public TiMapInfoWindow getMapInfoWindow()
{
return infoWindow;
}
private void setIconImageDimensions(int w, int h)
{
if (w >= 0 && h >= 0) {
iconImageWidth = w;
iconImageHeight = h;
} else { // default maker icon
TiDimension widthDimension = new TiDimension(defaultIconImageWidth, TiDimension.TYPE_UNDEFINED);
TiDimension heightDimension = new TiDimension(defaultIconImageHeight, TiDimension.TYPE_UNDEFINED);
// TiDimension needs a view to grab the window manager, so we'll just use the decorview of the current window
View view = TiApplication.getAppCurrentActivity().getWindow().getDecorView();
iconImageWidth = widthDimension.getAsPixels(view);
iconImageHeight = heightDimension.getAsPixels(view);
}
}
public int getIconImageHeight()
{
return iconImageHeight;
}
@Override
public boolean hasProperty(String name)
{
return (super.getProperty(name) != null);
}
@Override
public void onPropertyChanged(String name, Object value)
{
super.onPropertyChanged(name, value);
if (marker == null || value == null) {
return;
}
if (name.equals(TiC.PROPERTY_LONGITUDE)) {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_LON), TiConvert.toDouble(value));
} else if (name.equals(TiC.PROPERTY_LATITUDE)) {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_LAT), TiConvert.toDouble(value));
} else if (name.equals(TiC.PROPERTY_TITLE)) {
String title = TiConvert.toString(value);
annoTitle = title;
getOrCreateMapInfoWindow().setTitle(title);
updateInfoWindow();
} else if (name.equals(TiC.PROPERTY_SUBTITLE)) {
getOrCreateMapInfoWindow().setSubtitle(TiConvert.toString(value));
subTitle = TiConvert.toString(value);
updateInfoWindow();
} else if (name.equals(TiC.PROPERTY_LEFT_BUTTON)) {
getOrCreateMapInfoWindow().setLeftOrRightPane(value, TiMapInfoWindow.LEFT_PANE);
updateInfoWindow();
} else if (name.equals(TiC.PROPERTY_LEFT_VIEW) && getProperty(TiC.PROPERTY_LEFT_BUTTON) == null) {
getOrCreateMapInfoWindow().setLeftOrRightPane(value, TiMapInfoWindow.LEFT_PANE);
updateInfoWindow();
} else if (name.equals(TiC.PROPERTY_RIGHT_BUTTON)) {
getOrCreateMapInfoWindow().setLeftOrRightPane(value, TiMapInfoWindow.RIGHT_PANE);
updateInfoWindow();
} else if (name.equals(TiC.PROPERTY_RIGHT_VIEW) && getProperty(TiC.PROPERTY_RIGHT_BUTTON) == null) {
getOrCreateMapInfoWindow().setLeftOrRightPane(value, TiMapInfoWindow.RIGHT_PANE);
updateInfoWindow();
} else if (name.equals(MapModule.PROPERTY_DRAGGABLE)) {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_DRAGGABLE),
TiConvert.toBoolean(value));
} else if (name.equals(TiC.PROPERTY_PINCOLOR)) {
requestRefresh();
} else if (name.equals(MapModule.PROPERTY_HIDDEN)) {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_HIDDEN),
TiConvert.toBoolean(value));
} else if (name.equals(TiC.PROPERTY_IMAGE)) {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_IMAGE), value);
}
}
private TiMapInfoWindow getOrCreateMapInfoWindow()
{
if (infoWindow == null) {
Context context = TiApplication.getAppCurrentActivity();
if (context == null) {
context = TiApplication.getInstance();
}
infoWindow = new TiMapInfoWindow(context, this);
}
return infoWindow;
}
private void updateInfoWindow()
{
if (marker == null) {
return;
}
if (TiApplication.isUIThread()) {
Marker m = marker.getMarker();
if (m != null && m.isInfoWindowShown()) {
m.showInfoWindow();
}
} else {
getMainHandler().sendEmptyMessage(MSG_UPDATE_INFO_WINDOW);
}
}
private void requestRefresh()
{
if (this.delegate != null) {
this.delegate.refreshAnnotation(this);
}
}
public String getApiName()
{
return "Ti.Map.Annotation";
}
}