Computer >> Máy Tính >  >> Lập trình >> Android

Làm thế nào để phát hiện cảm ứng và vị trí của nó trên bản đồ Google trong Android?

Ví dụ này trình bày cách tôi phát hiện cảm ứng và vị trí của nó trên bản đồ Google trong Android.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Google Maps mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://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" />

Bước 3 - Thêm mã sau vào 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();
   }
}

Bước 4 - Thêm khóa ma vào google_maps_api.xml

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

Bước 5 - Thêm mã sau vào androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://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>

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ studio android, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Run từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm thế nào để phát hiện cảm ứng và vị trí của nó trên bản đồ Google trong Android?