This tutorial the use of aggregate tags when data requires a large number of data points on the map.
The Tag Collection tool helps you manage multiple tags at different magnification levels. To be precise, the "signs" are actually 'objects' at this stage and only become "signs" when they are taken. But for clarity, this document will be called "signs" around.
To use the marker clustering utility, you will need to add markers as ClusterItem objects to the ClusterManager. The ClusterManager passes the markers to the Algorithm, which transforms them into a set of clusters. The ClusterRenderer takes care of the rendering, by adding and removing clusters and individual markers. The ClusterRenderer and Algorithm are pluggable and can be customized.
Go to the Google API Console in your browser.
Implement ClusterItem to represent a marker on the map. The cluster item returns the position of the marker as a LatLng object, and an optional title or snippet. Create new java class:
Place.java
In your map activity, add the ClusterManager and feed it the cluster items. Note the type argument, which declares the ClusterManager to be of type Place.
MapsActivity.java
activity_maps.xml
Google Maps Android Marker Clustering Utility
By clustering your markers, you can put a large number of markers on a map without making the map hard to read.To use the marker clustering utility, you will need to add markers as ClusterItem objects to the ClusterManager. The ClusterManager passes the markers to the Algorithm, which transforms them into a set of clusters. The ClusterRenderer takes care of the rendering, by adding and removing clusters and individual markers. The ClusterRenderer and Algorithm are pluggable and can be customized.
Create a new android maps project
1. Start Android Studio.
2. Create a new project as follows:
If you see the Welcome to Android Studio dialog, choose Start a new Android Studio project, available under 'Quick Start' on the right of the dialog.
If you see the Welcome to Android Studio dialog, choose Start a new Android Studio project, available under 'Quick Start' on the right of the dialog. Otherwise, click File in the Android Studio menu bar, then New, New Project.
If you see the Welcome to Android Studio dialog, choose Start a new Android Studio project, available under 'Quick Start' on the right of the dialog. Otherwise, click File in the Android Studio menu bar, then New, New Project.
3. Enter your app name, company domain, and project location, as prompted. Then click Next.
4. Select the form factors you need for your app. If you're not sure what you need, just select Phone and Tablet. Then click Next.
5. Select Google Maps Activity in the 'Add an activity to Mobile' dialog. Then click Next.
6. Enter the activity name, layout name and title as prompted. The default values are fine. Then click Finish.
Get a Google Maps API key
Your application needs to access the Google Maps server API key. Key to the type of key you need to limit API keys with Android apps. The key is free. You can use it with any of your applications to call Google Maps for Android application programming interfaces (APIs), and the number of users supported is unlimited.Go to the Google API Console in your browser.
- Click Get Key Create new poject or use exixting.
- Copy the credentials provided in the google_maps_api.xml file.
dependencies {
compile 'com.google.maps.android:android-maps-utils:0.5+'
}
Replace: compile 'com.google.android.gms:play-services:10.2.1'
To: compile 'com.google.android.gms:play-services-maps:10.2.1'
Implement ClusterItem to represent a marker on the map. The cluster item returns the position of the marker as a LatLng object, and an optional title or snippet. Create new java class:
Place.java
package com.wolaris.mapsmarkers.model;
import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.clustering.ClusterItem;
public class Place implements ClusterItem {
private final LatLng mPosition;
private String mTitle;
private String mSnippet;
public Place(double lat, double lng) {
mPosition = new LatLng(lat, lng);
mTitle = null;
mSnippet = null;
}
public Place(double lat, double lng, String title, String snippet) {
mPosition = new LatLng(lat, lng);
mTitle = title;
mSnippet = snippet;
}
@Override
public LatLng getPosition() {
return mPosition;
}
@Override
public String getTitle() { return mTitle; }
@Override
public String getSnippet() { return mSnippet; }
public void setTitle(String title) {
mTitle = title;
}
public void setSnippet(String snippet) {
mSnippet = snippet;
}
}
In your map activity, add the ClusterManager and feed it the cluster items. Note the type argument
MapsActivity.java
package com.wolaris.mapsmarkers;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.maps.android.clustering.ClusterManager;
import com.wolaris.mapsmarkers.model.Place;
import java.util.Random;
public class MapsActivity extends FragmentActivity
implements OnMapReadyCallback {
private GoogleMap mMap;
private static final double LAT = -8.101379;
private static final double LNG = 112.147751;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpClusterer();
}
private ClusterManager<Place> mClusterManager;
private void setUpClusterer() {
// Position the map.
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(LAT,LNG), 10));
// Initialize the manager with the context and the map.
// (Activity extends context, so we can pass 'this' in the constructor.)
mClusterManager = new ClusterManager<Place>(this, mMap);
// Point the map's listeners at the listeners implemented by the cluster
// manager.
mMap.setOnCameraIdleListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
// Add cluster items (markers) to the cluster manager.
addPlaces();
}
private void addPlaces() {
for (int i = 0; i < 10; i++) {
Random random = new Random();
double radiusInDegrees = 1000 / 111000f;
double u = random.nextDouble();
double v = random.nextDouble();
double w = radiusInDegrees * Math.sqrt(u);
double t = 2 * Math.PI * v;
double x = w * Math.cos(t);
double y = w * Math.sin(t);
double new_x = x / Math.cos(LAT);
double longitude = new_x + LNG;
double latitude = y + LAT;
Place offsetItem = new Place(latitude, longitude, "Place "+ i,
"Lat " + String.valueOf(latitude) + " \nLng " + String.valueOf(longitude));
mClusterManager.addItem(offsetItem);
}
}
}
activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.wolaris.mapsmarkers.MapsActivity" />