คุณสามารถใช้ 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 หมวดหมู่สำหรับการเรียกเก็บเงิน ได้แก่ พื้นฐาน รายชื่อติดต่อ และบรรยากาศ ระบบจะเรียกเก็บเงินช่องพื้นฐานในอัตราฐานและไม่มีค่าใช้จ่ายเพิ่มเติม ระบบจะเรียกเก็บเงินฟิลด์รายชื่อติดต่อและบรรยากาศในอัตราที่สูงกว่า สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีเรียกเก็บเงินคำขอข้อมูล Place โปรดดูการใช้งานและการเรียกเก็บเงิน
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% ที่สถานที่ที่ถูกต้องคือสถานที่ A และ 35% ที่สถานที่ดังกล่าวจะเป็นสถานที่ B คำตอบดังกล่าวมีสมาชิก 2 ราย และสถานที่ A ซึ่งมีความน่าจะเป็นที่ 0.55 และสถานที่ B โดยมีความน่าจะเป็นที่ 0.35
แสดงการระบุแหล่งที่มาในแอปของคุณ
เมื่อแอปแสดงข้อมูลที่ได้รับจาก PlacesClient.findCurrentPlace()
แอปต้องแสดงการระบุแหล่งที่มาด้วย ดูเอกสารประกอบเกี่ยวกับการระบุแหล่งที่มา