如何在Android中檢測觸控及其在Google地圖上的位置?


此示例演示瞭如何在Android中檢測觸控及其在Google地圖上的位置。

步驟1 - 在Android Studio中建立一個新專案,轉到檔案⇒新建Google地圖專案,並填寫所有必需的詳細資訊以建立新專案。

步驟2 - 將以下程式碼新增到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" />

步驟3 - 將以下程式碼新增到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();
   }
}

步驟4 - 在google_maps_api.xml中新增ma金鑰

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

步驟5 - 將以下程式碼新增到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>

讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟您的專案之一的活動檔案,然後單擊執行 工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

點選這裡下載專案程式碼。

更新於: 2019年11月22日

687 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.