forked from OniQ/Nativescript-Danem-Google-Maps-Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
172 lines (156 loc) · 6.29 KB
/
index.android.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils = require("@nativescript/core/utils");
var ClusterManager = com.google.maps.android.clustering.ClusterManager;
var DefaultClusterRenderer = com.google.maps.android.clustering.view.DefaultClusterRenderer;
var HeatmapTileProvider = com.google.maps.android.heatmaps.HeatmapTileProvider;
var TileOverlayOptions = com.google.android.gms.maps.model.TileOverlayOptions;
var Gradient = com.google.maps.android.heatmaps.Gradient;
var _mapView = {};
var heatmaps = {
provider: "",
overlay: "",
}
var imageModule = require("@nativescript/core/ui/image");
var imageSourceModule = require('@nativescript/core/image-source');
function moveCamera(latitude, longitude) {
if (_mapView.gMap === undefined) {
console.log("NO INIT MAPVIEW")
} else {
try {
var cameraUpdate = new com.google.android.gms.maps.CameraUpdateFactory.newLatLng(new com.google.android.gms.maps.model.LatLng(latitude, longitude))
_mapView.gMap.animateCamera(cameraUpdate)
} catch (e) {
console.log(e)
}
}
}
exports.moveCamera = moveCamera;
/***************************************** CLUSTERING *****************************************/
const CustomClusterItem = java.lang.Object.extend({
interfaces: [com.google.maps.android.clustering.ClusterItem],
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;
},
});
function setupMarkerCluster(mapView, markers) {
_mapView = mapView
const CustomClusterRenderer = DefaultClusterRenderer.extend({
init: function () { },
onBeforeClusterItemRendered: function (item, markerOptions) {
this.super.onBeforeClusterItemRendered(item, markerOptions);
var icon = item.marker.infoWindowTemplate;
var value = null;
if (typeof icon === 'string') {
var resourceName = String(icon);
var imageSource = imageSourceModule.ImageSource.fromResourceSync(resourceName);
if (imageSource) {
var image = new imageModule.Image();
image.imageSource = imageSource;
value = image;
}
}
var androidIcon = (value) ? com.google.android.gms.maps.model.BitmapDescriptorFactory.fromBitmap(value.imageSource.android) : null;
markerOptions.icon(androidIcon);
}
});
var clusterManager = new ClusterManager(utils.ad.getApplicationContext(), _mapView.gMap);
var renderer = new CustomClusterRenderer(utils.ad.getApplicationContext(), _mapView.gMap, clusterManager);
clusterManager.mapView = _mapView;
if (_mapView.gMap.setOnCameraIdleListener) {
_mapView.gMap.setOnCameraIdleListener(clusterManager);
}
else if (_mapView.gMap.setOnCameraChangeListener) {
_mapView.gMap.setOnCameraChangeListener(clusterManager);
}
clusterManager.setRenderer(renderer);
_mapView.gMap.setOnInfoWindowClickListener(clusterManager);
_mapView.gMap.setOnMarkerClickListener(clusterManager);
clusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener({
onClusterItemClick: function (gmsMarker) {
var marker = markers.find(mk => mk.android.getPosition() === gmsMarker.getPosition());
marker && _mapView.notifyMarkerTapped(marker);
return false;
}
}));
clusterManager.setOnClusterItemInfoWindowClickListener(new ClusterManager.OnClusterItemInfoWindowClickListener({
onClusterItemInfoWindowClick: function (gmsMarker) {
var marker = markers.find(mk => mk.android.getPosition() === gmsMarker.getPosition());
marker && _mapView.notifyMarkerTapped(marker);
return false;
}
}));
clusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener({
onClusterClick: function (cluster) {
var listeMarker = cluster.getItems().toArray();
var resultListeMarkers = [];
for (var i = 0; i < listeMarker.length; i++) {
resultListeMarkers.push(markers.find(mk => mk.android.getPosition() === listeMarker[i].getPosition()))
if (i === listeMarker.length - 1) {
_mapView.notifyMarkerTapped(resultListeMarkers);
return false;
}
}
}
}));
var arrayMarker = new java.util.ArrayList()
for (var i = 0; i < markers.length; i++) {
var markerItem = new CustomClusterItem();
markerItem.marker = markers[i];
arrayMarker.add(markerItem)
if (i === markers.length - 1) {
clusterManager.addItems(arrayMarker)
}
}
clusterManager.cluster();
}
exports.setupMarkerCluster = setupMarkerCluster;
function clearMap() {
_mapView.gMap.clear()
}
exports.clearMap = clearMap;
/***************************************** HEATMAP *****************************************/
function setupHeatmap(mapView, positions, colors, startPoints) {
var list = new java.util.ArrayList();
positions.forEach(function (position) {
list.add(position.android);
});
var colrs = Array.create("int", 2);
colrs[0] = colors[0].android;
colrs[1] = colors[1].android;
var sttPoints = Array.create("float", 2);
sttPoints[0] = startPoints[0];
sttPoints[1] = startPoints[1];
var gradient = new Gradient(colrs, sttPoints);
heatmaps.provider = new HeatmapTileProvider.Builder()
.data(list)
.gradient(gradient)
.build();
heatmaps.overlay = mapView.gMap.addTileOverlay(new TileOverlayOptions().tileProvider(heatmaps.provider));
return heatmaps
}
exports.setupHeatmap = setupHeatmap;
function setOpacity(value) {
heatmaps.provider.setOpacity(value)
}
exports.setOpacity = setOpacity;
function setRadius(value) {
heatmaps.provider.setRadius(value)
}
exports.setRadius = setRadius;
function removeHeatmap() {
heatmaps.provider.remove()
}
exports.removeHeatmap = removeHeatmap;
//# sourceMappingURL=index.android.js.map