Google Mapの地図上でタップした位置に移動しマーカーを追加してみましょう。基本のGoogle Mapにタップのリスナーや長押しでのリスナーを追加するだけです。
![[Android] Google Map タップして移動、マーカーの追加 1x1.trans - [Android] Google Map タップして移動、マーカーの追加](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
Android Studio
2021.2.1
2021.2.1
OnMapClickListener
>Google Maps Activity を選択してプロジェクトを作成すれば、ほとんど出来上がっています。API キーをセットしてください
Google Mapアプリを作る場合には、プロジェクトの新規作成でMAPアプリを選択すると簡単に出来上がります。
あとは、OnMapClickListenerのリスナーをセットするだけです。
長押しの場合はOnMapLongClickListenerを使います。
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 61 62 63 64 65 66 67 68 69 70 | //package your.package,name; import androidx.annotation.NonNull; import androidx.fragment.app.FragmentActivity; import android.os.Bundle; 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; import com.example.testgooglemaptapandmove.databinding.ActivityMapsBinding; import java.util.Locale; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {     private GoogleMap mMap;     private LatLng location;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         com.example.testgooglemaptapandmove.databinding.ActivityMapsBinding binding = ActivityMapsBinding.inflate(getLayoutInflater());         setContentView(binding.getRoot());         // Obtain the SupportMapFragment and get notified when the map is ready to be used.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()                 .findFragmentById(R.id.map);         assert mapFragment != null;         mapFragment.getMapAsync(this);     }     /**      * Manipulates the map once available.      * This callback is triggered when the map is ready to be used.      * This is where we can add markers or lines, add listeners or move the camera. In this case,      * we just add a marker near Sydney, Australia.      * If Google Play services is not installed on the device, the user will be prompted to install      * it inside the SupportMapFragment. This method will only be triggered once the user has      * installed Google Play services and returned to the app.      */     @Override     public void onMapReady(@NonNull GoogleMap googleMap) {         mMap = googleMap;         // 皇居辺りの緯度経度         location = new LatLng(35.68, 139.76);         // marker 追加         mMap.addMarker(new MarkerOptions().position(location).title("Tokyo"));         // camera 移動         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 10));         // タップした時のリスナーをセット         mMap.setOnMapClickListener(tapLocation -> {             // tapされた位置の緯度経度             location = new LatLng(tapLocation.latitude, tapLocation.longitude);             String str = String.format(Locale.US, "%f, %f", tapLocation.latitude, tapLocation.longitude);             mMap.addMarker(new MarkerOptions().position(location).title(str));             mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 14));         });         // 長押しのリスナーをセット         mMap.setOnMapLongClickListener(longpushLocation -> {             LatLng newlocation = new LatLng(longpushLocation.latitude, longpushLocation.longitude);             mMap.addMarker(new MarkerOptions().position(newlocation).title(""+longpushLocation.latitude+" :"+ longpushLocation.longitude));             mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(newlocation, 14));         });     } } | 
実行して、タップをするとマーカーが追加されるのが分かります。
 
![[Android] Google Map タップして移動、マーカーの追加 1x1.trans - [Android] Google Map タップして移動、マーカーの追加](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
 
関連ページ:
- Google Map API キー取得
- Google Map 簡単に地図を表示させる
- Google Map ズームとアイコン画像貼り付け
- Google Map タップして移動、マーカーの追加
- Intentでの簡単な Google Map 地図の表示
- Google Map 移動経路
References:
API キーの取得 | Google Maps Android API | Google Developers
スタートガイド | Google Maps Android API | Google Developers
GitHub の Google マップ リポジトリ
Google Maps Android API – Google Developers
OnMapReadyCallback | Google APIs for Android | Google Developers
![[Android] Google Map タップして移動、マーカーの追加 x13.4 gmap 00 100x100 - [Android] Google Map タップして移動、マーカーの追加](https://akira-watson.com/wp-content/uploads/2015/07/x13.4_gmap_00-100x100.jpg)
