Welcome folks today in this blog post we will be building an android app
where we will embed google maps
and add markers
using maps sdk
library in java. All the full source code of the application is shown below.
Get Started
In order to get started you need to make a new android
project inside the android studio and then you will see the below directory
structure as shown below
Now we need to edit the AndroidManifest.xml
file you need to include the location
permissions to allow the android
to get the user
location
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.ImageCropper" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.geo.API_KEY" android:value="##yourapikey##"/> </application> </manifest> |
Now we need to edit the activity_main.xml
layout file where we will have the fragment
where we will be displaying the google maps
a shown below.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" tools:ignore="HardcodedText"> <fragment android:id="@+id/map_fragment" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> |
Adding Libraries in Gradle
Now we need to edit the build.gradle
file and copy paste the following code
build.gradle
1 2 3 |
dependencies { implementation 'com.google.android.gms:play-services-maps:17.0.1' } |
As you can see we are adding the google maps
service libraries and now we need to press the sync
now to build the gradle
of the app
And now we need to edit the MainAcitvity.java
file and copy paste the following code
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.example.imagecropper; import android.os.Bundle; import android.widget.Toast; import androidx.fragment.app.FragmentActivity; 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.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; public class MainActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapClickListener { private GoogleMap googleMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get a reference to the map fragment SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map_fragment); // Load the map asynchronously mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { // Save a reference to the Google Map this.googleMap = googleMap; // Set the map type to hybrid (satellite + roads) googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); // Move the camera to a default location LatLng defaultLocation = new LatLng(37.4220, -122.0841); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLocation, 10)); // Add a click listener to the map googleMap.setOnMapClickListener(this); } @Override public void onMapClick(LatLng latLng) { // Add a marker at the clicked location MarkerOptions markerOptions = new MarkerOptions() .position(latLng) .title("New Marker"); Marker marker = googleMap.addMarker(markerOptions); // Show a toast message with the marker's title Toast.makeText(this, marker.getTitle(), Toast.LENGTH_SHORT).show(); } } |
As you can see we are first we are embeding the google maps
when we load the android
app and then we are adding the marker
inside the map and then we will be showing the toast
notification as shown below.