the GoogleMap is an object that is received on a onMapReady() event
MarkerOptions is the builder class of a Marker, and is used to add one marker to a map.
Google Play Services SDK installed.
A Google Console Account.
A Google Maps API Key obtained in Google Console.
Adding markers to a map
To add markers to a Google Map, for example from an ArrayList of MyLocation Objects, we can do it this way.
The MyLocation holder class:
Here is a method that would take a list of MyLocation Objects and place a Marker for each one:
For the purpose of this example, mMap is a class member variable of the Activity, where we've assigned it to the map reference received in the onMapReady() override.
By changing mappoint x and y values as you need you can change offset possition of google map,by default it will be in the center of the map view. Call below method where you want to change it! Better to use it inside your onLocationChanged like changeOffsetCenter(location.getLatitude(),location.getLongitude());
Custom Google Map Styles
Google Maps come with a set of different styles to be applied, using this code :
The different map styles are :
Typical road map. Roads, some man-made features, and important natural features such as rivers are shown. Road and feature labels are also visible.
Satellite photograph data with road maps added. Road and feature labels are also visible.
Satellite photograph data. Road and feature labels are not visible.
Topographic data. The map includes colors, contour lines and labels, and perspective shading. Some roads and labels are also visible.
No tiles. The map will be rendered as an empty grid with no tiles loaded.
OTHER STYLE OPTIONS
At high zoom levels, the map will show floor plans for indoor spaces. These are called indoor maps, and are displayed only for the 'normal' and 'satellite' map types.
to enable or disable indoor maps, this is how it's done :
We can add custom styles to maps.
In onMapReady method add the following code snippet
under res folder create a folder name raw and add the styles json file.
Sample style.json file
This Activity code will provide basic functionality for including a Google Map using a SupportMapFragment.
The Google Maps V2 API includes an all-new way to load maps.
Activities now have to implement the OnMapReadyCallBack interface, which comes with a onMapReady() method override that is executed everytime we run SupportMapFragment.getMapAsync(OnMapReadyCallback); and the call is successfully completed.
Notice that the code above inflates a layout, which has a SupportMapFragment nested inside the container Layout, defined with an ID of R.id.map. The layout file is shown below:
Do not launch Google Maps when the map is clicked (lite mode)
When a Google Map is displayed in lite mode clicking on a map will open the Google Maps application. To disable this functionality you must call setClickable(false) on the MapView, e.g.:
Get debug SHA1 fingerprint
Open Android Studio
Open Your Project
Click on Gradle (From Right Side Panel, you will see Gradle Bar)
Click on Refresh (Click on Refresh from Gradle Bar, you will see List Gradle scripts of your Project)
Click on Your Project (Your Project Name form List (root))
Click on Tasks
Click on android
Double Click on signingReport (You will get SHA1 and MD5 in Run Bar)
InfoWindow Click Listener
Here is an example of how to define a different action for each Marker's InfoWindow click event.
Use a HashMap in which the marker ID is the key, and the value is the corresponding action it should take when the InfoWindow is clicked.
Then, use a OnInfoWindowClickListener to handle the event of a user clicking the InfoWindow, and use the HashMap to determine which action to take.
In this simple example we will open up a different Activity based on which Marker's InfoWindow was clicked.
Declare the HashMap as an instance variable of the Activity or Fragment:
Then, each time you add a Marker, make an entry in the HashMap with the Marker ID and the action it should take when it's InfoWindow is clicked.
For example, adding two Markers and defining an action to take for each:
In the InfoWindow click listener, get the action from the HashMap, and open up the corresponding Activity based on the action of the Marker:
Note If the code is in a Fragment, replace MainActivity.this with getActivity().
MapView: embedding a GoogleMap in an existing layout
It is possible to treat a GoogleMap as an Android view if we make use of the provided MapView class. Its usage is very similar to MapFragment.
In your layout use MapView as follows:
Your activity needs to implement the OnMapReadyCallback interface in order to work:
Obtaining the SH1-Fingerprint of your certificate keystore file
In order to obtain a Google Maps API key for your certificate, you must provide the API console with the SH1-fingerprint of your debug/release keystore.
You can obtain the keystore by using the JDK's keytool program as described here in the docs.
Another approach is to obtain the fingerprint programmatically by running this snippet with your app signed with the debug/release certificate and printing the hash to the log.
Show Current Location in a Google Map
Here is a full Activity class that places a Marker at the current location, and also moves the camera to the current position.
There are a few thing going on in sequence here:
Check Location permission
Once Location permission is granted, call setMyLocationEnabled(), build the GoogleApiClient, and connect it
Once the GoogleApiClient is connected, request location updates
Show explanation if needed on Marshmallow and Nougat using an AlertDialog (this case happens when the user had previously denied a permission request, or had granted the permission and then later revoked it in the settings):
Prompt the user for Location permission on Marshmallow and Nougat by calling ActivityCompat.requestPermissions():
Move camera to current location and place Marker when the Location permission is granted:
Using UISettings, the appearance of the Google Map can be modified.
Here is an example of some common settings:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0