เมื่อใช้ 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
ช่องต่างๆ สอดคล้องกับผลการค้นหาของ Place Search และแบ่งออกเป็น 3 หมวดหมู่การเรียกเก็บเงิน ได้แก่ ข้อมูลพื้นฐาน ข้อมูลติดต่อ และบรรยากาศ ระบบจะเรียกเก็บเงินสำหรับช่องพื้นฐานในราคาฐานและไม่มีค่าใช้จ่ายเพิ่มเติม ระบบจะเรียกเก็บเงินสำหรับฟิลด์รายชื่อติดต่อและบรรยากาศในอัตราที่สูงขึ้น ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีเรียกเก็บเงินคำขอข้อมูลสถานที่ได้ที่การใช้งานและการเรียกเก็บเงิน
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()
แอปจะต้องแสดงการระบุแหล่งที่มาด้วย ดูเอกสารประกอบเกี่ยวกับการระบุแหล่งที่มา