สถานที่ปัจจุบัน

เลือกแพลตฟอร์ม: Android iOS

เมื่อใช้ 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 เพื่อเริ่มต้นใช้งาน

รับตำแหน่งปัจจุบัน

หากต้องการค้นหาธุรกิจท้องถิ่นหรือสถานที่อื่นๆ ที่อุปกรณ์ตั้งอยู่ ให้ทำตามขั้นตอนต่อไปนี้

  1. เรียก ContextCompat.checkSelfPermission เพื่อตรวจสอบว่าผู้ใช้ได้ให้สิทธิ์เข้าถึงตำแหน่งอุปกรณ์ของตนหรือไม่ แอปของคุณต้องมีโค้ดเพื่อแจ้งให้ผู้ใช้ให้สิทธิ์และจัดการกับผลลัพธ์ด้วย ดูรายละเอียดได้ที่ขอสิทธิ์ของแอป
  2. สร้าง FindCurrentPlaceRequest โดยส่ง List จาก Place.Field รายการ โดยระบุประเภทข้อมูลสถานที่ที่แอปควรขอ
  3. เรียก PlacesClient.findCurrentPlace() โดยผ่าน FindCurrentPlaceRequest ที่คุณสร้างไว้ในขั้นตอนก่อนหน้า
  4. ดูรายการของ 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: 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() แอปจะต้องแสดงการระบุแหล่งที่มาด้วย ดูเอกสารประกอบเกี่ยวกับการระบุแหล่งที่มา