Skip to main content

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.
Learn to Make Ground Overlay Google Maps on Android

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 Make Ground Overlay Google Maps 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 Make Ground Overlay Google Maps on Android
  • Copy the credentials provided in the google_maps_api.xml file.
    Learn to Make Ground Overlay Google Maps on Android
    Learn to Make Ground Overlay Google Maps on Android
Replace:
    compile 'com.google.android.gms:play-services:10.2.1'
To:
    compile 'com.google.android.gms:play-services-maps:10.2.1'

Create a Overlay

To add a GroundOverlay, create a GroundOverlayOptions object that defines both an image and a position.
The below example demonstrates how to add a ground overlay to an existing GoogleMap object. Follow step by step to create that:
1. Instantiate a new GroundOverlayOptions object
2. Specify the image as a BitmapDescriptor.
3. Set the position of the image using one of the available methods:
position(LatLng location, float width, float height)
position(LatLng location, float width)
positionFromBounds(LatLngBounds bounds)
4. Set any optional properties, such as transparency, as desired.
5. Call GoogleMap.addGroundOverlay() to add the image to the map.
LatLng NEWARK = new LatLng(40.714086, -74.228697);

GroundOverlayOptions newarkMap = new GroundOverlayOptions()
        .image(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1922))
        .position(NEWARK, 8600f, 6500f);
map.addGroundOverlay(newarkMap);
If you wish to change or remove a ground overlay after you've added it to the map, ensure that you keep hold of the GroundOverlay object. You can modify the overlay later by making changes to this object.
LatLng NEWARK = new LatLng(40.714086, -74.228697);

GroundOverlayOptions newarkMap = new GroundOverlayOptions()
        .image(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1922))
        .position(NEWARK, 8600f, 6500f);

// Add an overlay to the map, retaining a handle to the GroundOverlay object.
GroundOverlay imageOverlay = map.addGroundOverlay(newarkMap);
You can change the ground overlay image after it's been added to the map with the GroundOverlay.setImage(BitmapDescriptor) method.
// Add an overlay, retaining a handle to the GroundOverlay object.
GroundOverlay imageOverlay = map.addGroundOverlay(newarkMap);

// Update the GroundOverlay with a new image of the same dimensions.
imageOverlay = map.setImage(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1975));

Use location to position an image

The anchor defaults to the center of the image. You can optionally provide the height of the overlay (in meters). If you do not provide the height of the overlay, it will be automatically calculated to preserve the proportions of the image.
GroundOverlayOptions newarkMap = new GroundOverlayOptions()
        .image(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1922))
        .anchor(0, 1)
        .position(new LatLng(40.714086, -74.228697), 8600f, 6500f);

Use LatLngBounds to position an image

The LatLngBounds sets the north east, and south west corners of the image. When the image is drawn on the map it will be rotated to fit the bounds. If the bounds do not match the original aspect ratio, the image will be skewed.
LatLngBounds newarkBounds = new LatLngBounds(
        new LatLng(40.712216, -74.22655),       // South west corner
        new LatLng(40.773941, -74.12544));      // North east corner
GroundOverlayOptions newarkMap = new GroundOverlayOptions()
        .image(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1922))
        .positionFromBounds(newarkBounds);

Associate data with a ground overlay

ou can call GroundOverlay.setTag() to store an arbitrary data object with a ground overlay, and retrieve the data object using GroundOverlay.getTag().
private GoogleMap mMap;
private GroundOverlay mSydneyGroundOverlay;

mSydneyGroundOverlay = mMap.addGroundOverlay(new GroundOverlayOptions()
    .image(BitmapDescriptorFactory.fromResource(R.drawable.harbour_bridge))
    .position(new LatLng(-33.873, 151.206))
    .clickable(true));

mSydneyGroundOverlay.setTag("Sydney");
Here are complete source code for the example
Dependency in app level build.gradle
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.google.android.gms:play-services-maps:10.2.1'
    compile 'com.android.support:cardview-v7:25.2.0'
    testCompile 'junit:junit:4.12'
}

MainActivity.java
package com.wolaris.mapgroundoverlay;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.widget.SeekBar;
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.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.GroundOverlay;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;

import java.util.ArrayList;
import java.util.List;

public class MapsActivity extends FragmentActivity
        implements OnMapReadyCallback, SeekBar.OnSeekBarChangeListener, GoogleMap.OnGroundOverlayClickListener {

    private static final int TRANSPARENCY_MAX = 100;

    private static final LatLng NEWARK = new LatLng(40.714086, -74.228697);

    private static final LatLng NEAR_NEWARK =
            new LatLng(NEWARK.latitude - 0.001, NEWARK.longitude - 0.025);

    private final List<BitmapDescriptor> mImages = new ArrayList<BitmapDescriptor>();

    private GroundOverlay mGroundOverlay;

    private GroundOverlay mGroundOverlayRotated;

    private SeekBar mTransparencyBar;

    private int mCurrentEntry = 0;

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        mTransparencyBar = (SeekBar) findViewById(R.id.transparencySeekBar);
        mTransparencyBar.setMax(TRANSPARENCY_MAX);
        mTransparencyBar.setProgress(0);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        mMap.setOnGroundOverlayClickListener(this);

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 11));

        mImages.clear();
        mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1922));
        mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_prudential_sunny));

        
        mGroundOverlayRotated = mMap.addGroundOverlay(new GroundOverlayOptions()
                .image(mImages.get(1)).anchor(0, 1)
                .position(NEAR_NEWARK, 4300f, 3025f)
                .bearing(30)
                .clickable(true));
        
        mGroundOverlay = mMap.addGroundOverlay(new GroundOverlayOptions()
                .image(mImages.get(mCurrentEntry)).anchor(0, 1)
                .position(NEWARK, 8600f, 6500f));

        mTransparencyBar.setOnSeekBarChangeListener(this);
        
        mMap.setContentDescription("Google Map with ground overlay.");
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        if (mGroundOverlay != null) {
            mGroundOverlay.setTransparency((float) i / (float) TRANSPARENCY_MAX);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onGroundOverlayClick(GroundOverlay groundOverlay) {
        mGroundOverlayRotated.setTransparency(0.5f - mGroundOverlayRotated.getTransparency());
    }
}

activity_maps.xml
<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        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.mapgroundoverlay.MapsActivity" />

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        map:cardBackgroundColor="@color/cardview_light_background"
        map:cardElevation="4dp"
        android:layout_margin="8dp"
        map:contentPadding="8dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/cardview_light_background"
            android:orientation="vertical">

            <TextView
                android:id="@+id/transparency_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="@string/transparency"
                android:textColor="@color/cardview_dark_background"/>

            <SeekBar
                android:id="@+id/transparencySeekBar"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </android.support.v7.widget.CardView>

</FrameLayout>
Reference here : https://developers.google.com/maps/documentation/android-api/groundoverlay

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