คุณสมบัติเด่นอย่างหนึ่งของแอปพลิเคชันบนอุปกรณ์เคลื่อนที่คือการรับรู้สถานที่ตั้ง ผู้ใช้อุปกรณ์เคลื่อนที่นําอุปกรณ์ของตนติดตัวไปด้วยทุกที่ การเพิ่มการรับรู้ตําแหน่งลงในแอปจะช่วยให้ผู้ใช้ได้รับประสบการณ์ตามบริบทมากขึ้น
ตัวอย่างโค้ด
ที่เก็บ ApiDemos ใน GitHub มีตัวอย่างที่แสดงการใช้ตําแหน่งในแผนที่
Java
- MyLocationDemoActivity: ใช้เลเยอร์ในตําแหน่งของฉัน รวมถึงสิทธิ์รันไทม์
- LocationSourceDemoActivity: ใช้ที่กําหนดเอง
LocationSource
- CurrentPlaceDetailsOnMaps: การค้นหาตําแหน่งปัจจุบันของอุปกรณ์ Android และแสดงรายละเอียดของสถานที่ (ธุรกิจหรือจุดสนใจอื่นๆ) ในสถานที่นั้น ดูบทแนะนําเกี่ยวกับการแสดงรายละเอียดสถานที่ปัจจุบันบนแผนที่
โคตลิน
- MyLocationDemoActivity: ใช้เลเยอร์ในตําแหน่งของฉัน รวมถึงสิทธิ์รันไทม์
- LocationSourceDemoActivity: ใช้ที่กําหนดเอง
LocationSource
- CurrentPlaceDetailsOnMaps: การค้นหาตําแหน่งปัจจุบันของอุปกรณ์ Android และแสดงรายละเอียดของสถานที่ (ธุรกิจหรือจุดสนใจอื่นๆ) ในสถานที่นั้น ดูบทแนะนําเกี่ยวกับการแสดงรายละเอียดสถานที่ปัจจุบันบนแผนที่
การทํางานกับข้อมูลตําแหน่ง
ข้อมูลตําแหน่งที่มีอยู่ในอุปกรณ์ Android จะรวมถึงตําแหน่งปัจจุบันของอุปกรณ์ โดยใช้เทคโนโลยีหลายอย่างร่วมกัน เช่น ทิศทางและวิธีการเคลื่อนที่ และระบุว่าอุปกรณ์ได้มีการเคลื่อนที่ข้ามขอบเขตทางภูมิศาสตร์ที่กําหนดไว้ หรือเขตพื้นที่เสมือนหรือไม่ คุณสามารถเลือกทํางานกับข้อมูลตําแหน่งได้หลายวิธีระหว่าง ความต้องการของแอปพลิเคชันของคุณ:
- เลเยอร์ตําแหน่งของฉันเป็นวิธีง่ายๆ ในการแสดงตําแหน่งของอุปกรณ์บนแผนที่ ไม่ให้ข้อมูล
- แนะนําให้ใช้ Location API ของบริการ Google Play สําหรับคําขอแบบเป็นโปรแกรมทั้งหมดสําหรับข้อมูลตําแหน่ง
- อินเทอร์เฟซ
LocationSource
ช่วยให้คุณระบุผู้ให้บริการตําแหน่งที่กําหนดเองได้
สิทธิ์เข้าถึงตำแหน่ง
หากแอปจําเป็นต้องเข้าถึงตําแหน่งของผู้ใช้ คุณต้องขอสิทธิ์โดยเพิ่มสิทธิ์เข้าถึงตําแหน่ง Android ที่เกี่ยวข้องลงในแอป
Android มีสิทธิ์เข้าถึงตําแหน่ง 2 รายการ ได้แก่ ACCESS_COARSE_LOCATION
และ
ACCESS_FINE_LOCATION
สิทธิ์ที่คุณเลือกจะเป็นตัวกําหนดความแม่นยําของตําแหน่งที่ API แสดงผล
android.permission.ACCESS_COARSE_LOCATION
– อนุญาตให้ API แสดงผลตําแหน่งโดยประมาณของอุปกรณ์ สิทธิ์นี้จะแสดงค่าประมาณตําแหน่งของอุปกรณ์จากบริการตําแหน่ง ตามที่อธิบายไว้ในเอกสารประกอบเกี่ยวกับความแม่นยําของตําแหน่งโดยประมาณandroid.permission.ACCESS_FINE_LOCATION
– อนุญาตให้ API ระบุตําแหน่งที่แน่นอนที่สุดเท่าที่จะเป็นไปได้จากผู้ให้บริการตําแหน่งที่มีอยู่ รวมถึงระบบการกําหนดตําแหน่งทั่วโลก (GPS) และข้อมูล Wi-Fi และอินเทอร์เน็ตมือถือ
เพิ่มสิทธิ์ในไฟล์ Manifest ของแอป
หากจําเป็นต้องใช้ตําแหน่งโดยประมาณเพื่อให้แอปทํางานเท่านั้น ให้เพิ่มสิทธิ์ ACCESS_COARSE_LOCATION
ในไฟล์ Manifest ของแอป โดยทําดังนี้
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp" > ... <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> ... </manifest>
อย่างไรก็ตาม หากต้องการใช้ตําแหน่งที่แน่นอน ให้เพิ่มสิทธิ์ ACCESS_COARSE_LOCATION
และ ACCESS_FINE_LOCATION
ลงในไฟล์ Manifest ของแอป
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp" > ... <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> ... </manifest>
ขอสิทธิ์รันไทม์
Android 6.0 (Marshmallow) นําเสนอรูปแบบใหม่สําหรับการจัดการสิทธิ์ ซึ่งจะช่วยลดความซับซ้อนของผู้ใช้เมื่อติดตั้งและอัปเกรดแอป หากแอปกําหนดเป้าหมาย API ระดับ 23 ขึ้นไป คุณจะใช้โมเดลสิทธิ์ใหม่ได้
หากแอปรองรับโมเดลสิทธิ์ใหม่และอุปกรณ์ใช้ Android 6.0 (Marshmallow) ขึ้นไป ผู้ใช้ไม่จําเป็นต้องให้สิทธิ์ใดๆ เมื่อติดตั้งหรืออัปเกรดแอป แอปจะต้องตรวจสอบว่ามีสิทธิ์ที่จําเป็นขณะรันไทม์หรือไม่ และขอสิทธิ์หากไม่มีสิทธิ์ โดยระบบจะแสดงกล่องโต้ตอบแก่ผู้ใช้เพื่อขอสิทธิ์
เพื่อให้ผู้ใช้ได้รับประสบการณ์ที่ดีที่สุด คุณควรขอสิทธิ์ในบริบท หากตําแหน่งจําเป็นต่อฟังก์ชันการทํางานของแอป คุณควรขอสิทธิ์ตําแหน่งเมื่อเริ่มต้นใช้งานแอป วิธีที่ดีที่สุดคือการดูหน้าจอต้อนรับหรือวิซาร์ดที่ให้ความรู้แก่ผู้ใช้เกี่ยวกับเหตุผลที่จําเป็นต้องใช้สิทธิ์
หากแอปต้องใช้สิทธิ์สําหรับฟังก์ชันการทํางานเพียงบางส่วนเท่านั้น คุณควรขอสิทธิ์ตําแหน่ง ณ เวลาที่แอปดําเนินการที่ต้องใช้สิทธิ์
แอปต้องจัดการเคสที่ผู้ใช้ไม่ได้ให้สิทธิ์ เช่น หากต้องได้รับสิทธิ์สําหรับฟีเจอร์ที่เฉพาะเจาะจง แอปจะปิดใช้ฟีเจอร์นั้นได้ หากแอปจําเป็นต้องใช้สิทธิ์เพื่อทํางาน แอปสามารถปิดใช้ฟังก์ชันการทํางานทั้งหมดและแจ้งให้ผู้ใช้ทราบว่าจําเป็นต้องให้สิทธิ์
ตัวอย่างโค้ดต่อไปนี้จะตรวจสอบสิทธิ์โดยใช้ไลบรารี AndroidX ก่อนที่จะเปิดใช้เลเยอร์ "ตําแหน่งของฉัน" จากนั้นจะจัดการผลลัพธ์ของคําขอสิทธิ์โดยติดตั้งใช้งาน ActivityCompat.OnRequestPermissionsResultCallback
จากไลบรารีการสนับสนุน
Java
// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.example.mapdemo; import android.Manifest.permission; import android.annotation.SuppressLint; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener; import com.google.android.gms.maps.GoogleMap.OnMyLocationClickListener; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import android.Manifest; import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.widget.Toast; /** * This demo shows how GMS Location can be used to check for changes to the users location. The "My * Location" button uses GMS Location to set the blue dot representing the users location. * Permission for {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and {@link * android.Manifest.permission#ACCESS_COARSE_LOCATION} are requested at run time. If either * permission is not granted, the Activity is finished with an error message. */ public class MyLocationDemoActivity extends AppCompatActivity implements OnMyLocationButtonClickListener, OnMyLocationClickListener, OnMapReadyCallback, ActivityCompat.OnRequestPermissionsResultCallback { /** * Request code for location permission request. * * @see #onRequestPermissionsResult(int, String[], int[]) */ private static final int LOCATION_PERMISSION_REQUEST_CODE = 1; /** * Flag indicating whether a requested permission has been denied after returning in {@link * #onRequestPermissionsResult(int, String[], int[])}. */ private boolean permissionDenied = false; private GoogleMap map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_location_demo); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(@NonNull GoogleMap googleMap) { map = googleMap; map.setOnMyLocationButtonClickListener(this); map.setOnMyLocationClickListener(this); enableMyLocation(); } /** * Enables the My Location layer if the fine location permission has been granted. */ @SuppressLint("MissingPermission") private void enableMyLocation() { // 1. Check if permissions are granted, if so, enable the my location layer if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { map.setMyLocationEnabled(true); return; } // 2. Otherwise, request location permissions from the user. PermissionUtils.requestLocationPermissions(this, LOCATION_PERMISSION_REQUEST_CODE, true); } @Override public boolean onMyLocationButtonClick() { Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show(); // Return false so that we don't consume the event and the default behavior still occurs // (the camera animates to the user's current position). return false; } @Override public void onMyLocationClick(@NonNull Location location) { Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG).show(); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); return; } if (PermissionUtils.isPermissionGranted(permissions, grantResults, Manifest.permission.ACCESS_FINE_LOCATION) || PermissionUtils .isPermissionGranted(permissions, grantResults, Manifest.permission.ACCESS_COARSE_LOCATION)) { // Enable the my location layer if the permission has been granted. enableMyLocation(); } else { // Permission was denied. Display an error message // Display the missing permission error dialog when the fragments resume. permissionDenied = true; } } @Override protected void onResumeFragments() { super.onResumeFragments(); if (permissionDenied) { // Permission was not granted, display error dialog. showMissingPermissionError(); permissionDenied = false; } } /** * Displays a dialog with error message explaining that the location permission is missing. */ private void showMissingPermissionError() { PermissionUtils.PermissionDeniedDialog .newInstance(true).show(getSupportFragmentManager(), "dialog"); } }
Kotlin
// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.example.kotlindemos import android.Manifest import android.annotation.SuppressLint import android.content.pm.PackageManager import android.location.Location import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat.OnRequestPermissionsResultCallback import androidx.core.content.ContextCompat import com.example.kotlindemos.PermissionUtils.PermissionDeniedDialog.Companion.newInstance import com.example.kotlindemos.PermissionUtils.isPermissionGranted import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener import com.google.android.gms.maps.GoogleMap.OnMyLocationClickListener import com.google.android.gms.maps.OnMapReadyCallback import com.google.android.gms.maps.SupportMapFragment /** * This demo shows how GMS Location can be used to check for changes to the users location. The * "My Location" button uses GMS Location to set the blue dot representing the users location. * Permission for [Manifest.permission.ACCESS_FINE_LOCATION] and [Manifest.permission.ACCESS_COARSE_LOCATION] * are requested at run time. If either permission is not granted, the Activity is finished with an error message. */ class MyLocationDemoActivity : AppCompatActivity(), OnMyLocationButtonClickListener, OnMyLocationClickListener, OnMapReadyCallback, OnRequestPermissionsResultCallback { /** * Flag indicating whether a requested permission has been denied after returning in * [.onRequestPermissionsResult]. */ private var permissionDenied = false private lateinit var map: GoogleMap override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.my_location_demo) val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment? mapFragment?.getMapAsync(this) } override fun onMapReady(googleMap: GoogleMap) { map = googleMap googleMap.setOnMyLocationButtonClickListener(this) googleMap.setOnMyLocationClickListener(this) enableMyLocation() } /** * Enables the My Location layer if the fine location permission has been granted. */ @SuppressLint("MissingPermission") private fun enableMyLocation() { // 1. Check if permissions are granted, if so, enable the my location layer if (ContextCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission( this, Manifest.permission.ACCESS_COARSE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) { map.isMyLocationEnabled = true return } // 2. If if a permission rationale dialog should be shown if (ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.ACCESS_FINE_LOCATION ) || ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.ACCESS_COARSE_LOCATION ) ) { PermissionUtils.RationaleDialog.newInstance( LOCATION_PERMISSION_REQUEST_CODE, true ).show(supportFragmentManager, "dialog") return } // 3. Otherwise, request permission ActivityCompat.requestPermissions( this, arrayOf( Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION ), LOCATION_PERMISSION_REQUEST_CODE ) } override fun onMyLocationButtonClick(): Boolean { Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT) .show() // Return false so that we don't consume the event and the default behavior still occurs // (the camera animates to the user's current position). return false } override fun onMyLocationClick(location: Location) { Toast.makeText(this, "Current location:\n$location", Toast.LENGTH_LONG) .show() } override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<String>, grantResults: IntArray ) { if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) { super.onRequestPermissionsResult( requestCode, permissions, grantResults ) return } if (isPermissionGranted( permissions, grantResults, Manifest.permission.ACCESS_FINE_LOCATION ) || isPermissionGranted( permissions, grantResults, Manifest.permission.ACCESS_COARSE_LOCATION ) ) { // Enable the my location layer if the permission has been granted. enableMyLocation() } else { // Permission was denied. Display an error message // Display the missing permission error dialog when the fragments resume. permissionDenied = true } } override fun onResumeFragments() { super.onResumeFragments() if (permissionDenied) { // Permission was not granted, display error dialog. showMissingPermissionError() permissionDenied = false } } /** * Displays a dialog with error message explaining that the location permission is missing. */ private fun showMissingPermissionError() { newInstance(true).show(supportFragmentManager, "dialog") } companion object { /** * Request code for location permission request. * * @see .onRequestPermissionsResult */ private const val LOCATION_PERMISSION_REQUEST_CODE = 1 } }
เลเยอร์ตําแหน่งของฉัน
คุณสามารถใช้เลเยอร์ "ตําแหน่งของฉัน" และปุ่ม "ตําแหน่งของฉัน" เพื่อแสดงตําแหน่งปัจจุบันของผู้ใช้บนแผนที่ เรียกใช้ mMap.setMyLocationEnabled()
เพื่อเปิดใช้เลเยอร์ตําแหน่งของฉันบนแผนที่
ตัวอย่างต่อไปนี้แสดงการใช้งานเลเยอร์เลเยอร์แบบง่าย
Java
// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.maps.example; import android.annotation.SuppressLint; import android.location.Location; import android.os.Bundle; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; class MyLocationLayerActivity extends AppCompatActivity implements GoogleMap.OnMyLocationButtonClickListener, GoogleMap.OnMyLocationClickListener, OnMapReadyCallback { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_location); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @SuppressLint("MissingPermission") @Override public void onMapReady(GoogleMap map) { // TODO: Before enabling the My Location layer, you must request // location permission from the user. This sample does not include // a request for location permission. map.setMyLocationEnabled(true); map.setOnMyLocationButtonClickListener(this); map.setOnMyLocationClickListener(this); } @Override public void onMyLocationClick(@NonNull Location location) { Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG) .show(); } @Override public boolean onMyLocationButtonClick() { Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT) .show(); // Return false so that we don't consume the event and the default behavior still occurs // (the camera animates to the user's current position). return false; } }
Kotlin
// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.maps.example.kotlin import android.annotation.SuppressLint import android.location.Location import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener import com.google.android.gms.maps.GoogleMap.OnMyLocationClickListener import com.google.android.gms.maps.OnMapReadyCallback import com.google.android.gms.maps.SupportMapFragment import com.google.maps.example.R internal class MyLocationLayerActivity : AppCompatActivity(), OnMyLocationButtonClickListener, OnMyLocationClickListener, OnMapReadyCallback { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_my_location) val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment mapFragment.getMapAsync(this) } @SuppressLint("MissingPermission") override fun onMapReady(map: GoogleMap) { // TODO: Before enabling the My Location layer, you must request // location permission from the user. This sample does not include // a request for location permission. map.isMyLocationEnabled = true map.setOnMyLocationButtonClickListener(this) map.setOnMyLocationClickListener(this) } override fun onMyLocationClick(location: Location) { Toast.makeText(this, "Current location:\n$location", Toast.LENGTH_LONG) .show() } override fun onMyLocationButtonClick(): Boolean { Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT) .show() // Return false so that we don't consume the event and the default behavior still occurs // (the camera animates to the user's current position). return false } }
เมื่อเปิดใช้เลเยอร์ "ตําแหน่งของฉัน" ปุ่ม "ตําแหน่งของฉัน" จะปรากฏที่มุมขวาบนของแผนที่ เมื่อผู้ใช้คลิกปุ่ม กล้องจะตั้งศูนย์กลางของตําแหน่งปัจจุบันในตําแหน่งของอุปกรณ์ (หากมี) ตําแหน่งนี้จะระบุไว้บนแผนที่ด้วยจุดสีน้ําเงินขนาดเล็กหากอุปกรณ์หยุดนิ่ง หรือแสดงเป็นเครื่องหมายบั้งได้หากอุปกรณ์เคลื่อนที่
ภาพหน้าจอต่อไปนี้แสดงปุ่มตําแหน่งของฉันที่ด้านขวาบนและ จุดสีน้ําเงินของตําแหน่งของฉันที่กึ่งกลางแผนที่:
คุณป้องกันไม่ให้ปุ่มตําแหน่งของฉันปรากฏขึ้นได้โดยเรียกใช้ UiSettings.setMyLocationButtonEnabled(false)
แอปของคุณสามารถตอบสนองต่อเหตุการณ์ต่อไปนี้
- หากผู้ใช้คลิกปุ่ม "ตําแหน่งของฉัน" แอปของคุณจะได้รับการเรียกกลับ
onMyLocationButtonClick()
จากGoogleMap.OnMyLocationButtonClickListener
- หากผู้ใช้คลิกจุดสีน้ําเงินในตําแหน่งของฉัน แอปของคุณจะได้รับการเรียกกลับ
onMyLocationClick()
จากGoogleMap.OnMyLocationClickListener
API สถานที่ตั้งของบริการ Google Play
Location API ของบริการ Google Play เป็นเมธอดที่แนะนําสําหรับการเพิ่มการรับรู้ตําแหน่งลงในแอปพลิเคชัน Android ซึ่งรวมถึงฟังก์ชันที่ช่วยให้คุณทําสิ่งต่อไปนี้ได้
- ระบุตําแหน่งของอุปกรณ์
- ฟังการเปลี่ยนแปลงตําแหน่ง
- กําหนดรูปแบบการเดินทาง หากอุปกรณ์เคลื่อนที่
- สร้างและตรวจสอบพื้นที่ทางภูมิศาสตร์ที่กําหนดไว้ล่วงหน้า หรือที่เรียกว่าเขตพื้นที่เสมือน
API สถานที่ตั้งช่วยให้คุณสร้างแอปพลิเคชันเพื่อการรับรู้ที่ประหยัดพลังงานและใช้งานง่ายได้อย่างง่ายดาย เช่นเดียวกับ SDK ของ Maps สําหรับ Android ระบบจะเผยแพร่ Location API เป็นส่วนหนึ่งของ SDK บริการ Google Play ดูข้อมูลเพิ่มเติมเกี่ยวกับ Location API ได้ที่ชั้นเรียนการฝึกอบรม Android การทําให้แอปทราบถึงตําแหน่งแอปหรือเอกสารอ้างอิง API ของตําแหน่ง ตัวอย่างโค้ดรวมอยู่ใน SDK ของบริการ Google Play