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.
Go to the Google API Console in your browser.
MainActivity.java
activity_maps.xml
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.
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:
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.
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.gradledependencies {
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