This repository was archived by the owner on Apr 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathMapKit.java
More file actions
473 lines (425 loc) · 18.7 KB
/
MapKit.java
File metadata and controls
473 lines (425 loc) · 18.7 KB
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
package com.phonegap.plugins.mapkit;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.app.Dialog;
import android.content.DialogInterface;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.CameraPosition;
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 com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.VisibleRegion;
public class MapKit extends CordovaPlugin {
protected ViewGroup root; // original Cordova layout
protected RelativeLayout main; // new layout to support map
protected MapView mapView;
private CallbackContext cCtx;
private String TAG = "MapKitPlugin";
private Marker lastClicked;
double latitude = 0, longitude = 0;
int height = 460;
boolean atBottom = false;
int offsetTop = 0;
int zoomLevel = 0;
boolean infoWindowOpen = false;
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
main = new RelativeLayout(cordova.getActivity());
}
public void showMap(final JSONObject options) {
try {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mapView != null) {
mapView.setVisibility(mapView.VISIBLE);
} else {
LOG.e(TAG, "hello world");
LOG.e(TAG, options);
try {
height = options.getInt("height");
latitude = options.getDouble("lat");
longitude = options.getDouble("lon");
offsetTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, options.getInt("offsetTop"), cordova.getActivity().getResources().getDisplayMetrics());
zoomLevel = options.getInt("zoomLevel");
atBottom = options.getBoolean("atBottom");
LOG.e(height);
} catch (JSONException e) {
LOG.e(TAG, "Error reading options");
}
final int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(cordova.getActivity());
if (resultCode == ConnectionResult.SUCCESS) {
mapView = new MapView(cordova.getActivity(),
new GoogleMapOptions());
root = (ViewGroup) webView.getParent();
root.removeView(webView);
main.addView(webView);
cordova.getActivity().setContentView(main);
MapsInitializer.initialize(cordova.getActivity());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, height);
if (atBottom) {
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
mapView.setPadding(0, offsetTop, 0, 0);
} else {
params.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
mapView.setPadding(0, offsetTop, 0, 0);
}
params.addRule(RelativeLayout.CENTER_HORIZONTAL,
RelativeLayout.TRUE);
mapView.setLayoutParams(params);
mapView.onCreate(null);
mapView.onResume(); // FIXME: I wish there was a better way
// than this...
main.addView(mapView);
mapView.getMap().setMyLocationEnabled(true);
mapView.getMap().getUiSettings().setMyLocationButtonEnabled(false);
// Moving the map to lot, lon
mapView.getMap().moveCamera(
CameraUpdateFactory.newLatLngZoom(new LatLng(
latitude, longitude), 15));
cCtx.success();
mapView.getMap().setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
webView.loadUrl(
"javascript:annotationTap('" +
marker.getSnippet() +
"'.toString(), " +
marker.getPosition().latitude +
", " +
marker.getPosition().longitude +
");");
//set variable so we can close it later
lastClicked = marker;
// Log.d("MYTAG", "on Marker click: " + marker.getSnippet());
// Log.d("MYTAG", "on Marker click: " + marker.getPosition().latitude);
// Log.d("MYTAG", "on Marker click: " + marker.getPosition().longitude);
return false;
}
});
mapView.getMap().setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
VisibleRegion vr = mapView.getMap().getProjection().getVisibleRegion();
double left = vr.latLngBounds.southwest.longitude;
double top = vr.latLngBounds.northeast.latitude;
double right = vr.latLngBounds.northeast.longitude;
double bottom = vr.latLngBounds.southwest.latitude;
double longDelta = left - right;
double latDelta = top - bottom;
webView.loadUrl(
"javascript:geo.onMapMove(" +
position.target.latitude +
"," +
position.target.longitude +
"," +
latDelta +
"," +
longDelta +
");");
// Log.d("MYTAG", "on Marker click: " + marker.getSnippet());
// Log.d("MYTAG", "on Marker click: " + marker.getPosition().latitude);
// Log.d("MYTAG", "on Marker click: " + marker.getPosition().longitude);
return;
}
});
// set variables when infoWindows open, so we can tell when they close
mapView.getMap().setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(final Marker marker) {
// Log.d("MYTAG", "on infowindow: " + marker);
if (infoWindowOpen == false) {
infoWindowOpen = true;
}
return null;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
});
// when the map is clicked (not a pin or an infowindow),
// find out if we just closed an infowindow and if so, call a javascript function
mapView.getMap().setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(final LatLng latlng) {
if (infoWindowOpen == true) {
//Log.d("MYTAG", "on infowindow close: " + latlng.latitude);
infoWindowOpen = false;
webView.loadUrl("javascript:annotationDeselect();");
}
}
});
} else if (resultCode == ConnectionResult.SERVICE_MISSING ||
resultCode == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ||
resultCode == ConnectionResult.SERVICE_DISABLED) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, cordova.getActivity(), 1,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
cCtx.error("com.google.android.gms.common.ConnectionResult " + resultCode);
}
}
);
dialog.show();
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
cCtx.error("MapKitPlugin::showMap(): An exception occured");
}
}
public void setMapData(final JSONObject options) {
//Log.d("MYTAG", "setMapData");
try {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,options.getInt("height"), cordova.getActivity().getResources().getDisplayMetrics());
latitude = options.getDouble("lat");
longitude = options.getDouble("lon");
offsetTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,options.getInt("offsetTop"), cordova.getActivity().getResources().getDisplayMetrics());
zoomLevel = options.getInt("zoomLevel");
atBottom = options.getBoolean("atBottom");
} catch (JSONException e) {
LOG.e(TAG, "Error reading options");
}
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, height + offsetTop);
if (atBottom) {
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
mapView.setPadding(0, offsetTop, 0, 0);
} else {
params.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
mapView.setPadding(0, offsetTop, 0, 0);
}
params.addRule(RelativeLayout.CENTER_HORIZONTAL,
RelativeLayout.TRUE);
mapView.setLayoutParams(params);
mapView.getMap().animateCamera(
CameraUpdateFactory.newLatLngZoom(new LatLng(
latitude, longitude), zoomLevel));
cCtx.success();
}
});
} catch (Exception e) {
e.printStackTrace();
cCtx.error("MapKitPlugin::showMap(): An exception occured");
}
}
private void hideMap() {
try {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
String hideMethod = "";
// if we're not destroying the map, then just hide it...
if (mapView != null && !hideMethod.equals("destroy")) {
//Log.d("MYTAG", "true");
// AlphaAnimation animation2 = new AlphaAnimation(1.0f, 0.0f);
// animation2.setDuration(1000);
// mapView.startAnimation(animation2);
if (lastClicked != null) {
lastClicked.hideInfoWindow();
}
mapView.setVisibility(mapView.GONE);
cCtx.success();
} else {
//Log.d("MYTAG", "false");
mapView.onDestroy();
main.removeView(webView);
main.removeView(mapView);
root.addView(webView);
cordova.getActivity().setContentView(root);
mapView = null;
cCtx.success();
}
}
});
} catch (Exception e) {
e.printStackTrace();
cCtx.error("MapKitPlugin::hideMap(): An exception occured");
}
}
public void addMapPins(final JSONArray pins) {
try {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mapView != null) {
try {
for (int i = 0, j = pins.length(); i < j; i++) {
double latitude = 0, longitude = 0;
JSONObject options = pins.getJSONObject(i);
latitude = options.getDouble("lat");
longitude = options.getDouble("lon");
MarkerOptions mOptions = new MarkerOptions();
mOptions.position(new LatLng(latitude,
longitude));
if(options.has("title")) {
mOptions.title(options.getString("title"));
}
if(options.has("snippet")) {
mOptions.snippet(options.getString("snippet"));
}
if(options.has("icon")) {
BitmapDescriptor bDesc = getBitmapDescriptor(options);
if(bDesc != null) {
mOptions.icon(bDesc);
}
}
// adding Marker
// This is to prevent non existing asset resources to crash the app
try {
mapView.getMap().addMarker(mOptions);
} catch(NullPointerException e) {
LOG.e(TAG, "An error occurred when adding the marker. Check if icon exists");
}
}
cCtx.success();
} catch (JSONException e) {
e.printStackTrace();
LOG.e(TAG, "An error occurred while reading pins");
cCtx.error("An error occurred while reading pins");
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
cCtx.error("MapKitPlugin::addMapPins(): An exception occured");
}
}
private BitmapDescriptor getBitmapDescriptor( final JSONObject iconOption ) {
try {
Object o = iconOption.get("icon");
String type = null, resource = null;
if( o.getClass().getName().equals("org.json.JSONObject" ) ) {
JSONObject icon = (JSONObject)o;
if(icon.has("type") && icon.has("resource")) {
type = icon.getString("type");
resource = icon.getString("resource");
if(type.equals("asset")) {
return BitmapDescriptorFactory.fromAsset(resource);
}
}
} else {
//this is a simple change in the icon's color
return BitmapDescriptorFactory.defaultMarker(Float.parseFloat(o.toString()));
}
} catch (JSONException e){
e.printStackTrace();
}
return null;
}
public void clearMapPins() {
try {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mapView != null) {
mapView.getMap().clear();
cCtx.success();
}
}
});
} catch (Exception e) {
e.printStackTrace();
cCtx.error("MapKitPlugin::clearMapPins(): An exception occured");
}
}
public void changeMapType(final JSONObject options) {
try{
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if( mapView != null ) {
int mapType = 0;
try {
mapType = options.getInt("mapType");
} catch (JSONException e) {
LOG.e(TAG, "Error reading options");
}
//Don't want to set the map type if it's the same
if(mapView.getMap().getMapType() != mapType) {
mapView.getMap().setMapType(mapType);
}
}
cCtx.success();
}
});
} catch (Exception e) {
e.printStackTrace();
cCtx.error("MapKitPlugin::changeMapType(): An exception occured ");
}
}
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
cCtx = callbackContext;
if (action.compareTo("showMap") == 0) {
showMap(args.getJSONObject(0));
} else if (action.compareTo("hideMap") == 0) {
hideMap();
} else if (action.compareTo("addMapPins") == 0) {
addMapPins(args.getJSONArray(0));
} else if (action.compareTo("clearMapPins") == 0) {
clearMapPins();
} else if( action.compareTo("changeMapType") == 0 ) {
changeMapType(args.getJSONObject(0));
}
LOG.d(TAG, action);
return true;
}
@Override
public void onPause(boolean multitasking) {
LOG.d(TAG, "MapKitPlugin::onPause()");
if (mapView != null) {
mapView.onPause();
}
super.onPause(multitasking);
}
@Override
public void onResume(boolean multitasking) {
LOG.d(TAG, "MapKitPlugin::onResume()");
if (mapView != null) {
mapView.onResume();
}
super.onResume(multitasking);
}
@Override
public void onDestroy() {
LOG.d(TAG, "MapKitPlugin::onDestroy()");
if (mapView != null) {
mapView.onDestroy();
}
super.onDestroy();
}
}