How to detect touch and its position on Google map in Android?


This example demonstrates how do I detect touch and its position on Google map in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Google Maps Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/map"
   android:name="com.google.android.gms.maps.SupportMapFragment"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MapsActivity" />

Step 3 − Add the following code to src/MainActivity.java

import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import android.widget.Toast;
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.MarkerOptions;
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback, GoogleMap.OnCameraMoveStartedListener,
   GoogleMap.OnCameraMoveListener,
   GoogleMap.OnCameraMoveCanceledListener,
   GoogleMap.OnCameraIdleListener {
   
   private GoogleMap mMap;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);
      // Obtain the SupportMapFragment and get notified when the map is ready to be used.
      SupportMapFragment mapFragment = (SupportMapFragment)
         getSupportFragmentManager().findFragmentById(R.id.map);
      mapFragment.getMapAsync(this);
   }
   @Override
   public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;
      // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(-34, 151);
      mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
      mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
      mMap.setOnCameraIdleListener(this);
      mMap.setOnCameraMoveStartedListener(this);
      mMap.setOnCameraMoveListener(this);
      mMap.setOnCameraMoveCanceledListener(this);
   }
   @Override
   public void onCameraMoveStarted(int reason) {
      if (reason == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE) {
         Toast.makeText(this, "The user gestured on the map.", Toast.LENGTH_SHORT).show();
      }
      else if (reason == GoogleMap.OnCameraMoveStartedListener.REASON_API_ANIMATION) {
         Toast.makeText(this, "The user tapped something on the map.", Toast.LENGTH_SHORT).show();
      }
      else if (reason == GoogleMap.OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION) {
         Toast.makeText(this, "The app moved the camera.", Toast.LENGTH_SHORT).show();
      }
   }
   @Override
   public void onCameraMove() {
      Toast.makeText(this, "The camera is moving.", Toast.LENGTH_SHORT).show();
   }
   @Override
   public void onCameraMoveCanceled() {
      Toast.makeText(this, "Camera movement canceled.", Toast.LENGTH_SHORT).show();
   }
   @Override
   public void onCameraIdle() {
      Toast.makeText(this, "The camera has stopped moving.", Toast.LENGTH_SHORT).show();
   }
}

Step 4 − Add the ma key in the google_maps_api.xml

<resources>
   <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR MAP KEY</string>
</resources>

Step 5 − Add the following code to androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.myapplication">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity
         android:name=".MapsActivity"
         android:label="@string/title_activity_maps">
         <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="@string/google_maps_key" />
   </application>
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Click here to download the project code.

Azhar
Azhar

e

Updated on: 22-Nov-2019

570 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements