เมื่อใช้ 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 หมวดหมู่การเรียกเก็บเงิน ได้แก่ พื้นฐาน ข้อมูลติดต่อ และบรรยากาศ ระบบจะเรียกเก็บเงินสำหรับช่องพื้นฐานในอัตราฐาน และไม่มีค่าใช้จ่ายเพิ่มเติม ระบบจะเรียกเก็บเงินสำหรับฟิลด์ข้อมูลติดต่อและบรรยากาศในอัตราที่สูงขึ้น ดูข้อมูลเพิ่มเติมเกี่ยวกับการเรียกเก็บเงินสำหรับคำขอข้อมูลสถานที่ได้ที่การใช้งานและการเรียกเก็บเงิน
API จะแสดงผล FindCurrentPlaceResponse ใน Task
FindCurrentPlaceResponse มีรายการออบเจ็กต์ PlaceLikelihood ที่แสดงสถานที่ซึ่งอุปกรณ์น่าจะอยู่ สำหรับสถานที่แต่ละแห่ง ผลลัพธ์จะรวมการระบุความเป็นไปได้ที่สถานที่นั้นจะเป็นสถานที่ที่ถูกต้อง รายการอาจว่างเปล่า หากไม่มีสถานที่ที่รู้จักซึ่งสอดคล้องกับตำแหน่งของอุปกรณ์ที่ระบุ
คุณสามารถเรียกใช้
PlaceLikelihood.getPlace()
เพื่อดึงข้อมูลออบเจ็กต์
Place
และเรียกใช้
PlaceLikelihood.getLikelihood()
เพื่อดูคะแนนความเป็นไปได้ของสถานที่ ค่าที่สูงขึ้นหมายความว่ามีโอกาสมากขึ้นที่สถานที่นั้นจะตรงกันมากที่สุด
ตัวอย่างโค้ดต่อไปนี้จะดึงข้อมูลรายการสถานที่ที่อุปกรณ์มีแนวโน้มที่จะอยู่มากที่สุด และบันทึกชื่อและความเป็นไปได้ของสถานที่แต่ละแห่ง
Kotlin
// Use fields to define the data types to return. val placeFields = listOf(Place.Field.DISPLAY_NAME, Place.Field.FORMATTED_ADDRESS, Place.Field.LOCATION) // Define the search area around the current location (500m radius). val currentLocation = LatLng(37.4220, -122.0841) val circle = CircularBounds.newInstance(currentLocation, 500.0) val request = SearchNearbyRequest.builder(circle, placeFields) .setMaxResultCount(20) .build() // Call searchNearby and handle the response (first check that the user has granted permission). if (ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ) { placesClient.searchNearby(request).addOnSuccessListener { response -> binding.progressBar.visibility = View.GONE adapter.setPlaces(response.places) }.addOnFailureListener { exception -> binding.progressBar.visibility = View.GONE 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 = Arrays.asList( Place.Field.DISPLAY_NAME, Place.Field.FORMATTED_ADDRESS, Place.Field.LOCATION); // Define the search area around the current location. LatLng currentLocation = new LatLng(37.4220, -122.0841); LocationRestriction circle = CircularBounds.newInstance(currentLocation, /* radius= */ 500.0); // Use the builder to create a SearchNearbyRequest. SearchNearbyRequest request = SearchNearbyRequest.builder(circle, placeFields).setMaxResultCount(20).build(); // Call searchNearby and handle the response (first check that the user has granted // permission). if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { Task<SearchNearbyResponse> placeResponse = placesClient.searchNearby(request); placeResponse.addOnCompleteListener( task -> { binding.progressBar.setVisibility(View.GONE); if (task.isSuccessful()) { SearchNearbyResponse response = task.getResult(); adapter.setPlaces(response.getPlaces()); } else { Exception exception = task.getException(); if (exception instanceof ApiException apiException) { Log.e( "CurrentPlace", "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() แอปจะต้องแสดงการระบุแหล่งที่มาด้วย ดูเอกสารประกอบเกี่ยวกับการระบุแหล่งที่มา