คุณสามารถสํารวจสถานที่ ณ ตําแหน่งที่อุปกรณ์ของตนรายงานไว้ใน Places SDK สําหรับ Android ได้ ตัวอย่างของสถานที่ ได้แก่ ธุรกิจในพื้นที่ จุดสนใจ และสถานที่ตั้งทางภูมิศาสตร์
สิทธิ์
หากต้องการใช้ไลบรารี คุณไม่จําเป็นต้องประกาศสิทธิ์เพิ่มเติมในไฟล์ Manifest ของแอป เนื่องจากไลบรารีจะประกาศสิทธิ์ทั้งหมดที่ใช้ในไฟล์ Manifest อย่างไรก็ตาม หากแอปใช้
PlacesClient.findCurrentPlace()
คุณต้องขอสิทธิ์เข้าถึงตําแหน่งขณะรันไทม์
หากแอปไม่ได้ใช้ PlacesClient.findCurrentPlace()
ให้นําสิทธิ์ของ ACCESS_FINE_LOCATION
และ ACCESS_COARSE_LOCATION
ที่ไลบรารีแนะนําอย่างชัดเจนโดยการเพิ่มข้อมูลต่อไปนี้ไปยังไฟล์ Manifest
<manifest ... xmlns:tools="http://schemas.android.com/tools"> ... <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:node="remove"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove"/> ... </manifest>
อ่านเพิ่มเติมเกี่ยวกับสิทธิ์ และลองใช้ EasyEasy เพื่อเริ่มต้นใช้งาน
ดูตําแหน่งปัจจุบัน
หากต้องการค้นหาธุรกิจในพื้นที่หรือสถานที่อื่นๆ ที่อุปกรณ์ตั้งอยู่อยู่ในปัจจุบัน ให้ทําตามขั้นตอนต่อไปนี้
- โทรหา
ContextCompat.checkSelfPermission
เพื่อตรวจสอบว่าผู้ใช้ให้สิทธิ์เข้าถึงตําแหน่งอุปกรณ์ของตนหรือไม่ แอปของคุณยังต้องใส่รหัสเพื่อแจ้งให้ผู้ใช้อนุญาตและช่วยจัดการกับผลการค้นหา โปรดดูรายละเอียดที่ขอสิทธิ์ของแอป - สร้าง
FindCurrentPlaceRequest
โดยส่งList
ของPlace.Field
เพื่อระบุประเภทข้อมูลสถานที่ซึ่งแอปควรขอ - โทรหา
PlacesClient.findCurrentPlace()
โดยส่งFindCurrentPlaceRequest
ที่คุณสร้างในขั้นตอนก่อนหน้า - ดูรายการ
PlaceLikelihood
จากFindCurrentPlaceResponse
ช่องจะสอดคล้องกับผลการค้นหาสถานที่ และแบ่งออกเป็น 3 หมวดหมู่การเรียกเก็บเงิน ได้แก่ พื้นฐาน ข้อมูลติดต่อ และบรรยากาศ ระบบจะเรียกเก็บเงินในช่องพื้นฐานตามราคาฐาน และจะไม่มีค่าใช้จ่ายเพิ่มเติม ระบบจะเรียกเก็บเงินสําหรับช่องรายชื่อติดต่อและบรรยากาศในอัตราที่สูงกว่า ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีการเรียกเก็บเงินสําหรับคําขอข้อมูลสถานที่ได้ที่การใช้งานและการเรียกเก็บเงิน
API แสดงผล FindCurrentPlaceResponse
ใน Task
FindCurrentPlaceResponse
มีรายการออบเจ็กต์ PlaceLikelihood
ที่เป็นตัวแทนตําแหน่งที่อุปกรณ์มีแนวโน้มจะอยู่ ผลลัพธ์จะแสดงตําแหน่งแต่ละแห่งของสถานที่แต่ละแห่ง ซึ่งมักเป็นตําแหน่งที่ถูกต้อง รายการอาจว่างเปล่าหากไม่มีสถานที่ที่รู้จักซึ่งตรงกับตําแหน่งอุปกรณ์ที่ระบุ
คุณสามารถเรียก PlaceLikelihood.getPlace()
เพื่อเรียกข้อมูลออบเจ็กต์ Place
และ PlaceLikelihood.getLikelihood()
เพื่อรับคะแนนความเป็นไปได้ของสถานที่ ค่าที่สูงขึ้นหมายถึงความเป็นไปได้ที่สถานที่นี้จะตรงที่สุด
ตัวอย่างโค้ดต่อไปนี้จะดึงข้อมูลรายการตําแหน่งที่อุปกรณ์มักพบมากที่สุด แล้วบันทึกชื่อและแนวโน้มของสถานที่แต่ละแห่ง
Java
// Use fields to define the data types to return. List<Place.Field> placeFields = Collections.singletonList(Place.Field.NAME); // Use the builder to create a FindCurrentPlaceRequest. FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields); // Call findCurrentPlace and handle the response (first check that the user has granted permission). if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request); placeResponse.addOnCompleteListener(task -> { if (task.isSuccessful()){ FindCurrentPlaceResponse response = task.getResult(); for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) { Log.i(TAG, String.format("Place '%s' has likelihood: %f", placeLikelihood.getPlace().getName(), placeLikelihood.getLikelihood())); } } else { Exception exception = task.getException(); if (exception instanceof ApiException) { ApiException apiException = (ApiException) exception; Log.e(TAG, "Place not found: " + apiException.getStatusCode()); } } }); } else { // A local method to request required permissions; // See https://developer.android.com/training/permissions/requesting getLocationPermission(); }
Kotlin
// Use fields to define the data types to return. val placeFields: List<Place.Field> = listOf(Place.Field.NAME) // Use the builder to create a FindCurrentPlaceRequest. val request: FindCurrentPlaceRequest = FindCurrentPlaceRequest.newInstance(placeFields) // Call findCurrentPlace and handle the response (first check that the user has granted permission). if (ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { val placeResponse = placesClient.findCurrentPlace(request) placeResponse.addOnCompleteListener { task -> if (task.isSuccessful) { val response = task.result for (placeLikelihood: PlaceLikelihood in response?.placeLikelihoods ?: emptyList()) { Log.i( TAG, "Place '${placeLikelihood.place.name}' has likelihood: ${placeLikelihood.likelihood}" ) } } else { val exception = task.exception if (exception is ApiException) { Log.e(TAG, "Place not found: ${exception.statusCode}") } } } } else { // A local method to request required permissions; // See https://developer.android.com/training/permissions/requesting getLocationPermission() }
หมายเหตุเกี่ยวกับค่าความน่าจะเป็น
- ความน่าจะเป็นให้ความน่าจะเป็นสัมพัทธ์ของสถานที่ซึ่งตรงกับคําแนะนํามากที่สุดในรายการสถานที่ที่ส่งคืนสําหรับคําขอ 1 รายการ คุณจะเปรียบเทียบความเป็นไปได้ในคําขอต่างๆ ไม่ได้
- ค่าแนวโน้มจะอยู่ระหว่าง 0.0 ถึง 1.0
ตัวอย่างเช่น เพื่อแสดงความน่าจะเป็น 55% ว่าสถานที่ที่ถูกต้องคือสถานที่ ก และ 35% ของสถานที่ที่ ข คําตอบ 2 มีสมาชิก 2 คน โดยสถานที่ ก มีโอกาส 0.55 และมีโอกาส ข.ที่มีความเป็นไปได้ 0.35
แสดงการระบุแหล่งที่มาในแอป
เมื่อแอปแสดงข้อมูลที่ได้มาจาก PlacesClient.findCurrentPlace()
แอปต้องแสดงการระบุแหล่งที่มาด้วย ดูเอกสารประกอบเกี่ยวกับการระบุแหล่งที่มา