How to use Google Map in Android system and apply its settings
7 minute(s) read
Published on: May 01, 2021
Updated on: Dec 14, 2021
Google Maps is used for navigation, finding a person's current location, searching for places by name, and much more. In this part of the tutorial, we want to tell you about how to install and activate this feature in Android systems for users to use. It is noteworthy that Google has changed how it adds this feature to mobile systems, but it is still possible.
Create an API Key in Google Console
To use the Google Maps API, we must have an account and use the same account to enter the following link.
1- First, click on Select a project.
2- Then click on NEW PROJECT.
3- Create a name for the project and select it.
4- Click on the CREATE button to create the project with the entered name.
5- In the window and the Credentials section that opens, we must click the Create credentials button.
6- Then, we have to select the API key.
7- After this, a key is made.
8- Click on the ENABLE APIS AND SERVICES button in the Dashboard section of the project.
9- Then, we have to select Maps SDK for Android.
Create a Google Map view project
In this part of the tutorial, we want to show a location on the map using a marker.
1- We create a new project and put its name on Google Map as desired.
2- The activity used in this part of the training and the map display project should be of the Empty Activity type.
3- The language used for this section is Java.
4- To see the map API, we need tools like Google Play Services in the SDK.
5- To do this, you must check the SDK Manager and if it is not installed, be sure to install it.
6- We need to add the play-services-maps library, a subset of the play-services library, to the project.
We do not use the play-services library because it contains some items and sub-libraries, and these sub-libraries will cause problems. So, we need to add the libraries we need to the project.
7- Go to File> Project Structure and search the play-services-maps library and enter the latest version into the project.
8- The library is added to the dependencies block inside the Build.Gradle file.
9- Access to the map is online, and we must allow access to the Internet and network in this section:
The code above allows Internet and network access.
10- It should be noted that a meta-data tag is also required and must be defined in the application tag, which includes two attributes name and value:
We set the value com.google.android.geo.API_KEY for the name attribute and the value of the key we created in the Google console.
The code that should be in the AndroidManifest.xml field is as follows:
11- We have to define a Fragment in the activity layout.
12- The code inside activity_main.xml is as follows:
Implement an interface called OnMapReadyCallback on the activity class.
The name of this interface is that it is a CallBack when the map is ready to display. The function of this interface is to check whether Google Play Services is installed on the user's device, such as mobile phone, tablet, etc., or not. It then displays the appropriate message to the user that Google Play Services should be installed on the device and returns to the program after it is installed on the user's device. Therefore, the function and function of this interface is to check whether or not Google Play Services is installed on the device, and if it is not installed, it directs the user to a window to install it on his device.
13- Add the interface method to the activity.
14- Select alt + enter on it.
15- Select the onMapReady () method.
16- The code that should be written in MainActivity.java is as follows:
17- Complete the onMapReady () and onCreate () methods.
We use SupportMapFragment to display the map in Android systems. I defined an object of this method called mapFragment in onCreate ().
When the map is ready to display, it uses the getMapAsync () method to transfer it to mapFragment. In other words, this is done when OnMapReadyCallback announces that the map is ready to display.
Map details are also defined inside the onMapReady () method. LatLng is used to determine the desired location, an example of which we have defined in this project called myLocation. It should be noted that this class takes two inputs, the first input is Latitude, and the second input is Longitude:
These two features are required to indicate spatial locations and can be used. We need to use addMarker to show the location accurately. A marker is a marker used to indicate a location on a map.
Additional position and location information is provided by position (), title (), and snippet ().
Finally, using moveCamera (), a part of the map is displayed on the screen, where our requested location and map are in the middle or part of it.
18- We will implement the project.
19- After the execution, we will find that the location we wanted shows us a map using a marker.
20- But if the user must see the map and location from a closer distance, we need to add the ability to zoom.
21-, in the code above, instead of using newLatLang, we used newLatLangZoom.
22- This method can receive both location coordinates and a float number that can be used to zoom.
The letter f must denote float numbers.
23- I will run the project again.
24- If we click on the marker, we can also see its description.
25- We can also change the marker icon.
There are different modes that the map can take and display these modes. These modes include Normal, Satellite, and Hybrid. Each of these modes can be downloaded from Google using the setMapType () method in the API and displayed on the device.
Add the following code to the onMapReady () method:
If we want the map to be displayed as a satellite image, we must use MAP_TYPE_SATELLITE.
- MAP_TYPE_NORMAL shows the same maps and locations except that it also shows the names of streets and places.
- MAP_TYPE_TERRAIN Displays topographic data.
- MAP_TYPE_HYBRID is a combination of NORMAL and SATELLITE that displays street names, locations, and other details.
- Map_TYPE_NONE is a NORMAL map with less detail.
The zoom buttons of this and zoom out can be added to the chip using the setZoomControlsEnabled () method so that users can zoom in on the map and see in more detail at shorter distances.
Also, if we want to use the + and - buttons to zoom in and zoom out, we can define a possibility that can be done by tapping on the screen to zoom in and zoom out. This can be done using the setZoomGesturesEnabled () method.
The code that should be inside the MainActivity.java file is as follows:
In this part of the tutorial, you will get acquainted with how to create and import a map into the Android system, and you can add different types of maps that you want to the system. Depending on your needs, you can choose the type of map that shows the location in more or less detail or other modes. You can apply this change by zooming in and out by pressing the + and - buttons or Tapping the screen to zoom.