Reputation: 12022
I have created an app where all of my markers have CustomInfoWindow.But number of markers in my app getting bigger and bigger.So i saw about map-utils library and decided to implement it.I wanted to implement the marker clustering property.Before Implementing Marker clustering property i have this code in my app:
private void drawMarkerAndLine(ArrayList<StatusData> result) throws Exception{
try {
if (result.size() != 0) {
PolygonOptions options = new PolygonOptions()
.fillColor(0x330000FF)
.strokeColor(Color.BLUE)
.strokeWidth(3);
for (int i = 0; i < result.size(); i++) {
String status = result.get(i).getStatus();
String name = result.get(i).getPersonName();
String email = result.get(i).getPersonMail();
name += "/" + email;
Double lat = Double.parseDouble(result.get(i).getLatitude());
Double lng = Double.parseDouble(result.get(i).getLongitude());
if (status.length() > 20) {
status = status.substring(0, 20);
status += "...";
}
LatLng ll = new LatLng(lat, lng);
options.add(ll);
map.addMarker(new MarkerOptions().position(ll).title(name).snippet(status));
if (i == 0) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 15));
}
}
map.addPolygon(options);
}
}catch ( Exception e){
Toast.makeText(getActivity(),"Internet Connection Error.",Toast.LENGTH_SHORT).show();
}
Now i wanted to remain all my previous properties of like infowindow click,map.setInfoWindowAdapter and others.Just i want to implement the that when i zoom out markers will be clustered and when zoom in it will show the markers and i can click the infowindow like my previous.I have tried to change my this class but in vain.
private void drawMarkerAndLine(ArrayList<StatusData> result) throws Exception{
try {
if (result.size() != 0) {
/*PolygonOptions options = new PolygonOptions()
.fillColor(0x330000FF)
.strokeColor(Color.BLUE)
.strokeWidth(3);*/
for (int i = 0; i < result.size(); i++) {
String status = result.get(i).getStatus();
String name = result.get(i).getPersonName();
String email = result.get(i).getPersonMail();
name += "/" + email;
Double lat = Double.parseDouble(result.get(i).getLatitude());
Double lng = Double.parseDouble(result.get(i).getLongitude());
if (status.length() > 20) {
status = status.substring(0, 20);
status += "...";
}
LatLng ll = new LatLng(lat, lng);
//options.add(ll);
MyItem item = new MyItem(lat,lng);
clusterManager.addItem(item);
map.addMarker(new MarkerOptions().position(ll).title(name).snippet(status));
if (i == 0) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 15));
}
}
//map.addPolygon(options);
}
}catch ( Exception e){
Toast.makeText(getActivity(),"Internet Connection Error.",Toast.LENGTH_SHORT).show();
}
I have tried in other activity this and it works for just below code.But when i couldn't implement only the clustering property in my previous code.
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home_map_activity, null, false);
mapFrag = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
map = mapFrag.getMap();
map.setMyLocationEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
map.getUiSettings().setCompassEnabled(true);
map.getUiSettings().setMyLocationButtonEnabled(true);
map.getUiSettings().setAllGesturesEnabled(true);
map.setTrafficEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446),10));
mClusterManager = new ClusterManager<MyItem>(getActivity(), map);
map.setOnCameraChangeListener(mClusterManager);
drawMarker();
return v;
}
private void drawMarker() {
double lat = 51.5145160;
double lng = -0.1270060;
// Add ten cluster items in close proximity, for purposes of this example.
for (int i = 0; i < 10; i++) {
double offset = i / 60d;
lat = lat + offset;
lng = lng + offset;
MyItem offsetItem = new MyItem(lat, lng);
mClusterManager.addItem(offsetItem);
}
}
How can i implement this in my previous code with the infowindow click and other unchanged as i don't need any onclick listener in clustermanager?? Thanks in advance.
Upvotes: 0
Views: 660
Reputation: 682
A couple of things are wrong there.
1- You dont have to create the marker as before, just adding 'MyItem' to the cluster manager is enough.
2- The marker's title, snippet, position and any data you want to show should be inside of your 'MyItem' class.
So your 'MyItem' class should look somethingk like this:
public class MyItem implements ClusterItem{
private final LatLng position;
private final String status;
private final String name;
private final String email;
public MyItem(LatLng position2, String status2, String name2, String email2) {
position = position2;
status = status2;
name = name2;
email = email2;
}
@Override
public LatLng getPosition() {
return position;
}
public String getStatus() {
return status;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
Then in you activity create the items like
clusterManager.addItem(new MyItem(......));
And finally in the class where you extend DefaultClusterRenderer< MyItem >
@Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
super.onBeforeClusterItemRendered(item, markerOptions);
markerOptions.position(item.getPosition()).title(item.getName()).snippet(item.getStatus);
}
and you would get the same as before.
If you want to create a custom info window it gets a little bit more complicated.
Upvotes: 1