เมื่อใช้ 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>
อ่านเพิ่มเติมเกี่ยวกับ สิทธิ์และพิจารณาใช้ EasyPermissions เพื่อเริ่มต้นใช้งาน
รับตำแหน่งปัจจุบัน
หากต้องการค้นหาธุรกิจในพื้นที่หรือสถานที่อื่นๆ ที่อุปกรณ์อยู่ ให้ทำตามขั้นตอนต่อไปนี้
- เรียกใช้
ContextCompat.checkSelfPermission
เพื่อตรวจสอบว่าผู้ใช้ได้ให้สิทธิ์เข้าถึงตำแหน่งของอุปกรณ์หรือไม่ แอปของคุณต้องมีโค้ดเพื่อแจ้งให้ผู้ใช้ขอสิทธิ์ และเพื่อจัดการผลลัพธ์ด้วย ดูรายละเอียดได้ที่ขอสิทธิ์ของแอป - สร้าง
FindCurrentPlaceRequest
โดยส่งList
ของPlace.Field
ระบุประเภทข้อมูลสถานที่ที่แอปควรร้องขอ - เรียกใช้
PlacesClient.findCurrentPlace()
โดยส่งFindCurrentPlaceRequest
ที่คุณสร้างไว้ในขั้นตอนก่อนหน้า - ดูรายการ
PlaceLikelihood
จากFindCurrentPlaceResponse
ฟิลด์จะสอดคล้องกับผลการค้นหาสถานที่และแบ่งออกเป็น 3 หมวดหมู่การเรียกเก็บเงิน ได้แก่ พื้นฐาน ข้อมูลติดต่อ และบรรยากาศ ระบบจะเรียกเก็บเงินสำหรับฟิลด์พื้นฐานตามอัตราฐาน และจะไม่มีการเรียกเก็บเงินเพิ่มเติม ระบบจะเรียกเก็บเงินสำหรับฟิลด์ Contact และ Atmosphere ในอัตราที่สูงขึ้น ดูข้อมูลเพิ่มเติม เกี่ยวกับวิธีเรียกเก็บเงินสำหรับคำขอข้อมูลสถานที่ได้ที่ การใช้งานและการเรียกเก็บเงิน
API จะแสดง
FindCurrentPlaceResponse
ใน
Task
FindCurrentPlaceResponse
มีรายการ
PlaceLikelihood
ออบเจ็กต์ที่แสดงสถานที่ซึ่งมีแนวโน้มว่าอุปกรณ์จะอยู่ สำหรับ
สถานที่แต่ละแห่ง ผลลัพธ์จะระบุความเป็นไปได้ที่
สถานที่นั้นเป็นสถานที่ที่ถูกต้อง รายการอาจว่างเปล่าหากไม่มีสถานที่ที่รู้จักซึ่งสอดคล้องกับตำแหน่งของอุปกรณ์ที่ระบุ
คุณสามารถเรียกใช้
PlaceLikelihood.getPlace()
เพื่อดึงข้อมูลออบเจ็กต์
Place
และ
PlaceLikelihood.getLikelihood()
เพื่อรับคะแนนความน่าจะเป็นของสถานที่ ค่าที่สูงขึ้นหมายถึงความเป็นไปได้ที่สถานที่นั้นๆ จะเป็นสถานที่ที่ตรงกันมากที่สุด
ตัวอย่างโค้ดต่อไปนี้จะดึงรายการสถานที่ที่อุปกรณ์ มีแนวโน้มที่จะอยู่มากที่สุด และบันทึกชื่อและความน่าจะเป็นของแต่ละสถานที่
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() }
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(); }
หมายเหตุเกี่ยวกับค่าความน่าจะเป็น
- ความน่าจะเป็นจะให้ความน่าจะเป็นสัมพัทธ์ ของสถานที่ที่ตรงกันมากที่สุดในรายการสถานที่ที่แสดงผลสำหรับคำขอเดียว คุณไม่สามารถเปรียบเทียบความเป็นไปได้ในคำขอต่างๆ
- ค่าของความเป็นไปได้จะอยู่ระหว่าง 0.0 ถึง 1.0
ตัวอย่างเช่น หากต้องการแสดงความน่าจะเป็น 55% ที่สถานที่ที่ถูกต้องคือสถานที่ ก และความน่าจะเป็น 35% ที่สถานที่ที่ถูกต้องคือสถานที่ ข การตอบกลับจะมีสมาชิก 2 รายการ ได้แก่ สถานที่ ก ที่มีความน่าจะเป็น 0.55 และสถานที่ ข ที่มีความน่าจะเป็น 0.35
แสดงการระบุแหล่งที่มาในแอป
เมื่อแอปแสดงข้อมูลที่ได้จาก
PlacesClient.findCurrentPlace()
แอปต้องแสดงการระบุแหล่งที่มาด้วย ดูเอกสารประกอบเกี่ยวกับการระบุแหล่งที่มา