รับตำแหน่งที่รู้จักล่าสุด
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
แอปของคุณสามารถขอตำแหน่งล่าสุดที่ทราบของอุปกรณ์ของผู้ใช้ได้โดยใช้ Location API ของบริการ Google Play โดยส่วนใหญ่ คุณจะสนใจตำแหน่งปัจจุบันของผู้ใช้ ซึ่งมักจะเทียบเท่ากับตำแหน่งที่ทราบล่าสุดของอุปกรณ์
โดยเฉพาะอย่างยิ่ง ให้ใช้ผู้ให้บริการตำแหน่งที่ผสานรวมเพื่อดึงข้อมูลตำแหน่งที่ทราบล่าสุดของอุปกรณ์ ผู้ให้บริการตำแหน่งที่ผสานรวมเป็นหนึ่งใน API ตำแหน่งในบริการ Google Play โดยจะ
จัดการเทคโนโลยีตำแหน่งพื้นฐานและมี API ที่ใช้งานง่ายเพื่อให้
คุณระบุข้อกำหนดในระดับสูงได้ เช่น ความแม่นยำสูงหรือประหยัดพลังงาน
นอกจากนี้ ยังเพิ่มประสิทธิภาพการใช้พลังงานแบตเตอรี่ของอุปกรณ์ด้วย
หมายเหตุ: เมื่อแอปทำงานในเบื้องหลัง
สิทธิ์เข้าถึงตำแหน่งควร
มีความสำคัญต่อฟังก์ชันหลักของแอปและมาพร้อมกับการ
เปิดเผยข้อมูลที่เหมาะสมต่อผู้ใช้
บทเรียนนี้จะแสดงวิธีส่งคำขอเดียวสำหรับตำแหน่งของอุปกรณ์
โดยใช้เมธอด
getLastLocation()
ในผู้ให้บริการตำแหน่งที่ผสานรวม
ตั้งค่าบริการ Google Play
หากต้องการเข้าถึงผู้ให้บริการตำแหน่งที่ผสานรวม โปรเจ็กต์การพัฒนาแอปของคุณต้องมี Google Play Services
ดาวน์โหลดและติดตั้งคอมโพเนนต์บริการ Google Play
ผ่าน SDK
Manager แล้วเพิ่มไลบรารีลงในโปรเจ็กต์ ดูรายละเอียดได้ที่คำแนะนำเกี่ยวกับ
การตั้งค่าบริการของ Google Play
ระบุสิทธิ์ของแอป
แอปที่มีฟีเจอร์ที่ใช้บริการตำแหน่งต้องขอสิทธิ์เข้าถึงตำแหน่ง
ตามกรณีการใช้งานของฟีเจอร์เหล่านั้น
สร้างไคลเอ็นต์บริการตำแหน่ง
ในonCreate()
เมธอด
ของกิจกรรม ให้สร้างอินสแตนซ์ของไคลเอ็นต์ Fused Location Provider ดังที่ข้อมูลโค้ดต่อไปนี้แสดง
Kotlin
private lateinit var fusedLocationClient: FusedLocationProviderClient
override fun onCreate(savedInstanceState: Bundle?) {
// ...
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
}
Java
private FusedLocationProviderClient fusedLocationClient;
// ..
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}
รับตำแหน่งที่รู้จักล่าสุด
เมื่อสร้างไคลเอ็นต์บริการตำแหน่งแล้ว
คุณจะรับตำแหน่งที่ทราบล่าสุดของอุปกรณ์ของผู้ใช้ได้ เมื่อแอปเชื่อมต่อกับอุปกรณ์เหล่านี้ คุณจะใช้วิธีการ getLastLocation()
ของผู้ให้บริการตำแหน่งที่ผสานรวมเพื่อดึงข้อมูลตำแหน่งของอุปกรณ์ได้ ความแม่นยำของตำแหน่งที่การเรียกนี้ส่งคืน
จะกำหนดโดยการตั้งค่าสิทธิ์ที่คุณใส่ไว้ในไฟล์ Manifest ของแอป
ตามที่อธิบายไว้ในคำแนะนำเกี่ยวกับวิธีขอสิทธิ์เข้าถึงตำแหน่ง
หากต้องการขอตำแหน่งล่าสุดที่ทราบ ให้เรียกใช้เมธอด
getLastLocation()
ข้อมูลโค้ดต่อไปนี้แสดงคำขอและการจัดการการตอบกลับอย่างง่าย
Kotlin
fusedLocationClient.lastLocation
.addOnSuccessListener { location : Location? ->
// Got last known location. In some rare situations this can be null.
}
Java
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
}
}
});
เมธอด
getLastLocation()
จะแสดงผล Task
ที่คุณใช้เพื่อรับออบเจ็กต์
Location
ที่มีพิกัดละติจูดและลองจิจูดของ
ตำแหน่งทางภูมิศาสตร์ได้ ออบเจ็กต์สถานที่ตั้งอาจเป็น null
ในสถานการณ์ต่อไปนี้
- ตำแหน่งปิดอยู่ในการตั้งค่าอุปกรณ์ ผลลัพธ์อาจเป็น
null
แม้ว่าจะดึงข้อมูลตำแหน่งล่าสุดมาก่อนหน้านี้แล้วก็ตาม เนื่องจาก
การปิดใช้ตำแหน่งจะล้างแคชด้วย
- อุปกรณ์ไม่เคยบันทึกตำแหน่ง ซึ่งอาจเป็นกรณีของอุปกรณ์ใหม่
หรืออุปกรณ์ที่กู้คืนเป็นการตั้งค่าเริ่มต้น
- บริการ Google Play ในอุปกรณ์รีสตาร์ทแล้ว และไม่มีไคลเอ็นต์ Fused Location Provider ที่ใช้งานอยู่ซึ่งขอตำแหน่งหลังจากที่บริการรีสตาร์ท หากต้องการหลีกเลี่ยงสถานการณ์นี้ คุณสามารถสร้างไคลเอ็นต์ใหม่และ
ขออัปเดตตำแหน่งด้วยตนเอง ดูข้อมูลเพิ่มเติมได้ที่ขออัปเดตตำแหน่ง
เลือกการประมาณตำแหน่งที่ดีที่สุด
FusedLocationProviderClient
มีหลายวิธีในการดึงข้อมูลตำแหน่งของอุปกรณ์
เลือกตัวเลือกใดตัวเลือกหนึ่งต่อไปนี้ ทั้งนี้ขึ้นอยู่กับกรณีการใช้งานของแอป
getLastLocation()
รับการประมาณตำแหน่งได้เร็วขึ้นและลดการใช้แบตเตอรี่ที่อาจ
เกิดจากแอปของคุณ อย่างไรก็ตาม
ข้อมูลตำแหน่งอาจล้าสมัย หากไม่มีไคลเอ็นต์อื่นที่ใช้ตำแหน่งอย่างต่อเนื่อง
เมื่อเร็วๆ นี้
getCurrentLocation()
รับตำแหน่งที่อัปเดตล่าสุดและแม่นยำยิ่งขึ้นอย่างสม่ำเสมอ อย่างไรก็ตาม วิธีนี้
อาจทำให้เกิดการคำนวณตำแหน่งที่ใช้งานอยู่บนอุปกรณ์
นี่เป็นวิธีที่แนะนำในการรับตำแหน่งล่าสุดทุกครั้งที่ทำได้ และ
ปลอดภัยกว่าทางเลือกอื่นๆ เช่น การเริ่มต้นและจัดการการอัปเดตตำแหน่ง
ด้วยตนเองโดยใช้ requestLocationUpdates()
หากแอปของคุณเรียกใช้ requestLocationUpdates()
บางครั้งแอปอาจใช้พลังงานจำนวนมากหากไม่มีตำแหน่ง หรือหากไม่ได้หยุดคำขออย่างถูกต้อง
หลังจากได้รับตำแหน่งใหม่
แหล่งข้อมูลเพิ่มเติม
ดูข้อมูลเพิ่มเติมเกี่ยวกับการดึงข้อมูลตำแหน่งปัจจุบันใน Android ได้จากสื่อต่อไปนี้
ตัวอย่าง
- แอปตัวอย่างเพื่อแสดงแนวทางปฏิบัติแนะนำขณะดึงข้อมูล
ตำแหน่งปัจจุบัน
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-21 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-08-21 UTC"],[],[],null,["Using the Google Play services location APIs, your app can request the last\nknown location of the user's device. In most cases, you are interested in the\nuser's current location, which is usually equivalent to the last known\nlocation of the device.\n\nSpecifically, use the\n[fused\nlocation provider](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient.html) to retrieve the device's last known location. The fused\nlocation provider is one of the location APIs in Google Play services. It\nmanages the underlying location technology and provides a simple API so that\nyou can specify requirements at a high level, like high accuracy or low power.\nIt also optimizes the device's use of battery power. \n**Note:** When your app is running in the background,\n[access to location](/training/location/background) should be\ncritical to the core functionality of the app and is accompanied with proper\ndisclosure to users.\n\nThis lesson shows you how to make a single request for the location of a\ndevice using the\n[`getLastLocation()`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient.html#getLastLocation())\nmethod in the fused location provider.\n\nSet up Google Play services\n\nTo access the fused location provider, your app's development project must\ninclude Google Play services. Download and install the Google Play services\ncomponent via the [SDK\nManager](/tools/help/sdk-manager) and add the library to your project. For details, see the guide to\n[Setting Up Google Play\nServices](/google/play-services/setup).\n\nSpecify app permissions\n\nApps whose features use location services must\n[request location permissions](/training/location/permissions),\ndepending on the use cases of those features.\n\nCreate location services client\n\nIn your activity's [onCreate()](/reference/android/app/Activity#onCreate(android.os.Bundle)) method,\ncreate an instance of the Fused Location Provider Client as the following code snippet shows. \n\nKotlin \n\n```kotlin\nprivate lateinit var fusedLocationClient: FusedLocationProviderClient\n\noverride fun onCreate(savedInstanceState: Bundle?) {\n // ...\n\n fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)\n}\n```\n\nJava \n\n```java\nprivate FusedLocationProviderClient fusedLocationClient;\n\n// ..\n\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n // ...\n\n fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);\n}\n```\n\nGet the last known location\n\nOnce you have created the Location Services client\nyou can get the last known location of a user's device. When your app is\nconnected to these you can use the fused location provider's\n[`getLastLocation()`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient.html#getLastLocation())\nmethod to retrieve the device location. The precision of the location returned\nby this call is determined by the permission setting you put in your app\nmanifest, as described in the guide on how to\n[request location permissions](/training/location/permissions).\n\nTo request the last known location, call the\n[`getLastLocation()`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient.html#getLastLocation())\nmethod. The following code snippet illustrates the request and a simple handling of the\nresponse: \n\nKotlin \n\n```kotlin\nfusedLocationClient.lastLocation\n .addOnSuccessListener { location : Location? -\u003e\n // Got last known location. In some rare situations this can be null.\n }\n```\n\nJava \n\n```java\nfusedLocationClient.getLastLocation()\n .addOnSuccessListener(this, new OnSuccessListener\u003cLocation\u003e() {\n @Override\n public void onSuccess(Location location) {\n // Got last known location. In some rare situations this can be null.\n if (location != null) {\n // Logic to handle location object\n }\n }\n });\n```\n\nThe\n[`getLastLocation()`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient.html#getLastLocation())\nmethod returns a [`Task`](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task)\nthat you can use to get a\n[`Location`](/reference/android/location/Location)\nobject with the latitude and longitude coordinates of a\ngeographic location. The location object may be `null` in the\nfollowing situations:\n\n- Location is turned off in the device settings. The result could be `null` even if the last location was previously retrieved because disabling location also clears the cache.\n- The device never recorded its location, which could be the case of a new device or a device that has been restored to factory settings.\n- Google Play services on the device has restarted, and there is no active Fused Location Provider client that has requested location after the services restarted. To avoid this situation you can create a new client and request location updates yourself. For more information, see [Request location\n updates](/training/location/receive-location-updates).\n\nChoose the best location estimate\n\nThe `FusedLocationProviderClient` provides several methods to retrieve device\nlocation information. Choose from one of the following, depending on your app's\nuse case:\n\n- [`getLastLocation()`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient#getLastLocation()) gets a location estimate more quickly and minimizes battery usage that can be attributed to your app. However, the location information might be out of date, if no other clients have actively used location recently.\n- [`getCurrentLocation()`](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient#getCurrentLocation(int,%20com.google.android.gms.tasks.CancellationToken))\n gets a fresher, more accurate location more consistently. However, this method\n can cause active location computation to occur on the device\n\n This is the recommended way to get a fresh location, whenever possible, and\n is safer than alternatives like starting and managing location updates\n yourself using `requestLocationUpdates()`. If your app calls\n `requestLocationUpdates()`, your app can sometimes consume large amounts of\n power if location isn't available, or if the request isn't stopped correctly\n after obtaining a fresh location.\n\nAdditional resources\n\nFor more information about fetching current location in Android, view the\nfollowing materials:\n\nSamples\n\n- [Sample app](https://github.com/android/platform-samples/tree/main/samples/location/src/main/java/com/example/platform/location/currentLocation) to demonstrate best practices while fetching current location."]]