forked from naderio/nativescript-google-maps-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.android.ts
More file actions
183 lines (147 loc) · 5.02 KB
/
index.android.ts
File metadata and controls
183 lines (147 loc) · 5.02 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
import * as utils from "tns-core-modules/utils/utils";
import { MapView, Marker, Position } from "nativescript-google-maps-sdk"
declare var com: any;
const LatLng = com.google.android.gms.maps.model.LatLng;
const PolylineOptions = com.google.android.gms.maps.model.PolylineOptions;
const LatLngBounds = com.google.android.gms.maps.model.LatLngBounds;
const CameraUpdateFactory = com.google.android.gms.maps.CameraUpdateFactory;
const ClusterItem = com.google.maps.android.clustering.ClusterItem;
const ClusterManager = com.google.maps.android.clustering.ClusterManager;
const DefaultClusterRenderer = com.google.maps.android.clustering.view.DefaultClusterRenderer;
const HeatmapTileProvider = com.google.maps.android.heatmaps.HeatmapTileProvider;
const TileOverlayOptions = com.google.android.gms.maps.model.TileOverlayOptions;
const debugNull = function (...args: Array<any>): void { };
function debugDefault(...args: Array<any>) {
args = args.map((value) => {
if (typeof value === 'object' && value) {
try {
value = JSON.stringify(value);
} catch (e) {
value = value.toString();
}
}
return value;
});
args.unshift('nativescript-socket.io');
console.log.apply(console, args);
}
let debug = debugNull;
export function enableDebug(debugFn: ((...args: Array<any>) => any) = debugDefault): void {
debug = debugFn;
}
export function disableDebug(): void {
debug = debugNull;
}
const CustomClusterItem = ClusterItem.extend({
marker: null,
init: function () { },
getMarker: function () {
return this.marker;
},
getPosition: function () {
return this.marker.position.android;
},
getTitle: function () {
return this.marker.title;
},
getSnippet: function () {
return this.marker.snippet;
},
});
const CustomClusterManager = ClusterManager.extend({
mapView: null, // will be attached manually later
customCallback: null, // will be attached manually later
onCameraIdle() {
this.super.onCameraIdle();
if (this.customCallback) {
this.customCallback();
}
},
onMarkerClick(gmsMarker) {
this.super.onMarkerClick(gmsMarker);
let marker = this.mapView.findMarker((marker) => {
if (marker.android.getId) {
return marker.android.getId() === gmsMarker.getId();
}
return marker.title === gmsMarker.getTitle() && marker.snippet === gmsMarker.getSnippet() && marker.position.android.equals(gmsMarker.getPosition());
});
marker && this.mapView.notifyMarkerTapped(marker);
return false;
},
onInfoWindowClick(gmsMarker) {
this.super.onInfoWindowClick(gmsMarker);
let marker = this.mapView.findMarker((marker) => {
if (marker.android.getId) {
return marker.android.getId() === gmsMarker.getId();
}
return marker.title === gmsMarker.getTitle() && marker.snippet === gmsMarker.getSnippet() && marker.position.android.equals(gmsMarker.getPosition());
});
marker && this.mapView.notifyMarkerInfoWindowTapped(marker);
return false;
},
});
let clusterManager;
export function setupMarkerCluster(mapView: MapView, markers: Array<Marker>, options) {
debug('setupMarkerCluster');
try {
clusterManager = new CustomClusterManager(utils.ad.getApplicationContext(), mapView.gMap);
clusterManager.mapView = mapView;
if (options.customCallback) {
clusterManager.customCallback = options.customCallback;
}
if (mapView.gMap.setOnCameraIdleListener) {
mapView.gMap.setOnCameraIdleListener(clusterManager);
} else if (mapView.gMap.setOnCameraChangeListener) {
mapView.gMap.setOnCameraChangeListener(clusterManager);
}
mapView.gMap.setOnMarkerClickListener(clusterManager);
mapView.gMap.setOnInfoWindowClickListener(clusterManager);
addMarkers(mapView, markers);
} catch (e) {
console.log(e);
}
}
export function addMarkers(mapView: MapView, markers: Array<Marker>) {
try {
const clusterItems = markers.map(marker => {
let markerItem = new CustomClusterItem();
markerItem.marker = marker;
return markerItem;
});
const items = java.util.Arrays.asList(clusterItems);
clusterManager.addItems(items);
clusterManager.cluster();
} catch (e) {
console.log(e);
}
}
export function removeMarkers(mapView: MapView) {
try {
clusterManager.clearItems();
clusterManager.cluster();
} catch (e) {
console.log(e);
}
}
export interface IHeatmapConfig {
provider: any,
overlay: any,
}
export function setupHeatmap(mapView: MapView, positions: Array<Position>, config: IHeatmapConfig = null): IHeatmapConfig {
debug('setupHeatmap');
var list = new java.util.ArrayList();
positions.forEach(function (position) {
list.add(position.android);
});
if (config) {
config.overlay.clearTileCache();
config.provider.setData(list);
} else {
config = <IHeatmapConfig>{};
config.provider = new HeatmapTileProvider.Builder()
.data(list)
.build();
config.overlay = mapView.gMap.addTileOverlay(new TileOverlayOptions().tileProvider(config.provider));
}
return config;
}