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

Cách sử dụng API vị trí trong Android để theo dõi vị trí hiện tại của bạn?

Ví dụ này giải thích cách tôi sử dụng API vị trí trong android để theo dõi vị trí hiện tại của bạn.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án 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.

Thêm phần phụ thuộc sau vào build.gradle (Module:app) -

implementation 'com.google.android.gms:play-services-maps:17.0.0'

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 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 mMap;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
      mapFragment.getMapAsync(this);
   }
   @Override
   public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;
      LatLng myCurrentLocation = new LatLng(13.0827, 80.2707);
      mMap.addMarker(new MarkerOptions().position(myCurrentLocation).title("This is My current Location"));
      mMap.moveCamera(CameraUpdateFactory.newLatLng(myCurrentLocation));
   }
}

Bước 4 - Mở res / string.xml và thêm mã sau -

<resources>
   <string name="app_name">Sample</string>
   <string name="title_activity_maps">Map</string>
   <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyC19gZFIlF5OVySzG9iMAeDFzOmUuCHZ5Q</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.sample">
   <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 -

Cách sử dụng API vị trí trong Android để theo dõi vị trí hiện tại của bạn?