Skip to main content

Learn to Use Google Maps With Multiple Markers on Android

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.
wolaris_learn_to_use_google_maps_with_multiple_markers_on_android

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.
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.
Learn to Use Google Maps With Multiple Markers on Android

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.
    Learn to Use Google Maps With Multiple Markers on Android
  • Copy the credentials provided in the google_maps_api.xml file.
    Learn to Use Google Maps With Multiple Markers on Android
    Learn to Use Google Maps With Multiple Markers on Android
Add the following dependency to your app's Gradle build 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 , which declares the ClusterManager to be of type Place.
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" />

Popular posts from this blog

Tutorial Firebase Push Notification With Image on Android

Firebase cloud messaging (FCM) is a new version of Google Cloud Messaging (GCM). Using FCM you can send notification messages to your client application in order to drive user engagement. Notification messages can contain an optional data payload, which is delivered when users tap on the notification. Use notification messages when you want Firebase cloud messaging (FCM) to handle displaying a notification on your client app’s behalf. Use data messages when you want to process the messages on your client app. Create a new Android 1. Create a new Android Project in Android Studio. Give it a name and select the Minimum SDK on which your app will run on. I chose API 16 : Android 4.1 (JELLY_BEAN). 2. When you are prompted to add an activity to your application choose Blank Activity and click on next button. 3. In the next step click on Finish and in few seconds your application should be loaded in Android Studio. 4. Open build.gradle(Module:App) file of your application and add the ...

Learn to Make Ground Overlay Google Maps on Android

A land overlay is an image that is a permanent map. Unlike markers , ground cover on the ground to accommodate the screen oriented, turn, tilt, or zoom the map to change the orientation of the image. Terrestrial overlays are useful when you want to define an image in a region on the map. If you want to add rich images that cover a large part of the map, you should consider covering tiles. 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. 3. Enter your app name, company domain, and project location, as prompted. Then click Next. ...