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

เลือกแพลตฟอร์ม: 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>

อ่านเพิ่มเติมเกี่ยวกับ สิทธิ์ และพิจารณาใช้ EasyEasy เพื่อเริ่มต้นใช้งาน

ดูตําแหน่งปัจจุบัน

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

  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() เพื่อรับคะแนนความเป็นไปได้ของสถานที่ได้ ค่าที่สูงขึ้นหมายถึงความน่าจะเป็นที่มากขึ้นว่าสถานที่นั้นๆ ตรงที่สุด

ตัวอย่างโค้ดต่อไปนี้จะดึงข้อมูลรายการสถานที่ที่อุปกรณ์น่าจะอาศัยอยู่มากที่สุด รวมถึงบันทึกชื่อและแนวโน้มของแต่ละสถานที่

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();
}

      

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()
}

      

หมายเหตุเกี่ยวกับค่าความน่าจะเป็น

  • มีความเป็นไปได้ว่าความน่าจะเป็นของสถานที่ที่ตรงที่สุดจะได้ภายในรายการสถานที่ที่ส่งคืนสําหรับคําขอ 1 รายการ คุณเปรียบเทียบแนวโน้มในคําขอต่างๆ ไม่ได้
  • ค่าแนวโน้มจะอยู่ระหว่าง 0.0 ถึง 1.0

ตัวอย่างเช่น หากต้องการแสดงผลความน่าจะเป็น 55% ว่าสถานที่ที่ถูกต้องคือสถานที่ ก และมีความเป็นไปได้ 35% สําหรับสถานที่ ข คําตอบมีสมาชิก 2 คน สถานที่ ก มีแนวโน้ม 0.55 และสถานที่ ข.มีแนวโน้ม 0.35

แสดงการระบุแหล่งที่มาในแอป

เมื่อแอปแสดงข้อมูลที่ได้รับจาก PlacesClient.findCurrentPlace() แอปต้องแสดงการระบุแหล่งที่มาด้วย ดูเอกสารประกอบเกี่ยวกับการระบุแหล่งที่มา