-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkerSpiderfier.java
156 lines (139 loc) · 5.84 KB
/
MarkerSpiderfier.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
package com.Amir_P.MarkerSpiderfier;
public class MarkerSpiderfier extends AppCompatActivity implements OnMapReadyCallback, ClusterManager.OnClusterItemClickListener, ClusterManager.OnClusterClickListener, GoogleMap.OnMarkerClickListener, GoogleMap.OnCameraIdleListener, GoogleMap.OnMapClickListener {
GoogleMap Map;
float focus = 5f;
ClusterManager<mClusterItem> mClusterManager;
List<Marker> markers = new ArrayList<>();
List<Polyline> polylines = new ArrayList<>();
IconGenerator mIcon;
TextView markerTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mIcon = new IconGenerator(this);
markerTxt = (TextView) getLayoutInflater().inflate(R.layout.marker, null);
mIcon.setContentView(markerTxt);
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
Map = googleMap;
getMapReady();
}
@Override
public boolean onClusterItemClick(ClusterItem clusterItem) {
//
return true;
}
@Override
public boolean onClusterClick(Cluster cluster) {
for (int i = 0; i < markers.size(); i++) {
markers.get(i).remove();
polylines.get(i).remove();
}
markers.clear();
polylines.clear();
double radians = (360 / cluster.getSize() - 1) * Math.PI / 180;
LatLng clusterPosition = cluster.getPosition();
Object[] objects = cluster.getItems().toArray();
LatLng farLeft = Map.getProjection().getVisibleRegion().farLeft;
double center = Map.getCameraPosition().target.latitude;
double unit = Math.abs(farLeft.latitude - center) / 2;
MarkerOptions markerOptions = new MarkerOptions();
for (int i = 0; i < objects.length; i++) {
PolylineOptions options = new PolylineOptions().width(5).color(getResources().getColor(R.color.colorPrimary));
mClusterItem prevClusterItem = (mClusterItem) objects[i];
markerTxt.setText(" " + prevClusterItem.getTitle() + " ");
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(mIcon.makeIcon());
LatLng latLng = new LatLng(clusterPosition.latitude + (Math.cos(radians * i) * unit), clusterPosition.longitude + (Math.sin(radians * i) * unit));
markerOptions.icon(bitmapDescriptor)
.position(latLng)
.title(prevClusterItem.latLng.latitude + ":" + prevClusterItem.latLng.longitude).zIndex(1000);
options.add(latLng).add(clusterPosition);
markers.add(Map.addMarker(markerOptions));
polylines.add(Map.addPolyline(options));
}
return true;
}
@Override
public boolean onMarkerClick(Marker marker) {
if (markers.contains(marker)) {
String[] split = marker.getTitle().split(":");
LatLng markersRealLocation = new LatLng(Double.parseDouble(split[0]), Double.parseDouble(split[1]));
} else {
mClusterManager.onMarkerClick(marker);
}
return true;
}
@Override
public void onCameraIdle() {
mClusterManager.onCameraIdle();
if (Map.getCameraPosition().zoom != focus) {
focus = Map.getCameraPosition().zoom;
for (int i = 0; i < markers.size(); i++) {
markers.get(i).remove();
polylines.get(i).remove();
}
markers.clear();
polylines.clear();
}
}
public void getMapReady() {
mClusterManager = new ClusterManager<>(this, Map);
mClusterManager.setRenderer(new mClusterRenderer());
mClusterManager.setOnClusterItemClickListener(this);
mClusterManager.setOnClusterClickListener(this);
Map.setOnCameraIdleListener(this);
Map.setOnMarkerClickListener(this);
Map.setOnMapClickListener(this);
Map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(32.4279, 53.6880), focus));
}
@Override
public void onMapClick(LatLng latLng) {
for (int i = 0; i < markers.size(); i++) {
markers.get(i).remove();
polylines.get(i).remove();
}
markers.clear();
polylines.clear();
}
class mClusterRenderer extends DefaultClusterRenderer<mClusterItem> {
mClusterRenderer() {
super(getApplicationContext(), Map, mClusterManager);
}
@Override
protected void onBeforeClusterItemRendered(mClusterItem item, MarkerOptions markerOptions) {
markerTxt.setText(item.getTitle());
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(mIcon.makeIcon());
markerOptions.icon(bitmapDescriptor);
}
@Override
protected void onBeforeClusterRendered(Cluster<mClusterItem> cluster, MarkerOptions markerOptions) {
if (cluster.getSize() > 9) {
markerTxt.setText((cluster.getSize() / 10 * 10) + "+");
} else {
markerTxt.setText(cluster.getSize());
}
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(mIcon.makeIcon()));
}
@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
return cluster.getSize() > 1;
}
}
class mClusterItem implements ClusterItem {
private final LatLng latLng;
private final String title;
mClusterItem(LatLng latLng, String title) {
this.latLng = latLng;
this.title = title;
}
public String getTitle() {
return title;
}
@Override
public LatLng getPosition() {
return latLng;
}
}