การย้ายข้อมูลไปยังไคลเอ็นต์ New Places SDK

นักพัฒนาแอปในเขตเศรษฐกิจยุโรป (EEA)

คู่มือนี้อธิบายการเปลี่ยนแปลงระหว่างไลบรารีความเข้ากันได้ของ Places กับ Places SDK สำหรับ Android เวอร์ชันใหม่แบบสแตนด์อโลน หากคุณใช้ไลบรารีความเข้ากันได้ของ Places แทนการย้ายข้อมูลไปยัง Places SDK สำหรับ Android เวอร์ชันใหม่แบบสแตนด์อโลน คู่มือนี้จะแสดงวิธีอัปเดตโปรเจ็กต์เพื่อใช้ Places SDK สำหรับ Android เวอร์ชันใหม่

วิธีเดียวในการเข้าถึงฟีเจอร์และการแก้ไขข้อบกพร่องใน Places SDK สำหรับ Android เวอร์ชัน 2.6.0 ขึ้นไปคือการใช้ Places SDK สำหรับ Android Google ขอแนะนำให้อัปเดตจากไลบรารีความเข้ากันได้เป็น Places SDK สำหรับ Android เวอร์ชันใหม่โดยเร็วที่สุด

ข้อผิดพลาด: 9005 PLACES_API_RATE_LIMIT_EXCEEDED

มีอะไรเปลี่ยนแปลงบ้าง

การเปลี่ยนแปลงหลักๆ มีดังนี้

  • Places SDK สำหรับ Android เวอร์ชันใหม่จะเผยแพร่ เป็นไลบรารีไคลเอ็นต์แบบคงที่ ก่อนเดือนมกราคม 2019 เราได้เปิดตัว Places SDK สำหรับ Android ผ่านบริการ Google Play ตั้งแต่นั้นมา เราได้จัดเตรียมไลบรารีความเข้ากันได้ของ Places เพื่อให้การเปลี่ยนไปใช้ Places SDK ใหม่สำหรับ Android เป็นไปอย่างราบรื่น
  • มีวิธีใหม่ทั้งหมด
  • ตอนนี้ระบบรองรับมาสก์ฟิลด์สำหรับเมธอดที่แสดงรายละเอียดสถานที่แล้ว คุณใช้มาสก์ฟิลด์เพื่อระบุประเภทข้อมูลสถานที่ที่จะ แสดงได้
  • เราได้ปรับปรุงรหัสสถานะที่ใช้ในการรายงานข้อผิดพลาดแล้ว
  • ตอนนี้การเติมข้อความอัตโนมัติรองรับโทเค็นเซสชันแล้ว
  • เครื่องมือเลือกสถานที่ไม่มีให้บริการแล้ว

เกี่ยวกับคลังความเข้ากันได้ของ Places

ในเดือนมกราคม 2019 เมื่อเปิดตัว Places SDK สำหรับ Android เวอร์ชัน 1.0 แบบสแตนด์อโลน Google ได้จัดเตรียมไลบรารีความเข้ากันได้เพื่อช่วยในการย้ายข้อมูล จาก Places SDK สำหรับ Android เวอร์ชันบริการ Google Play ที่เลิกใช้งานแล้ว (com.google.android.gms:play-services-places)

เราได้จัดเตรียมไลบรารีความเข้ากันได้นี้ไว้ชั่วคราวเพื่อเปลี่ยนเส้นทางและแปลการเรียก API ที่มุ่งเป้าไปที่ Google Play Services เวอร์ชันหนึ่งๆ เป็นเวอร์ชันใหม่แบบสแตนด์อโลน จนกว่านักพัฒนาแอปจะย้ายข้อมูลโค้ดไปใช้ชื่อใหม่ใน SDK แบบสแตนด์อโลนได้ สำหรับ Places SDK สำหรับ Android แต่ละเวอร์ชันที่ เผยแพร่ตั้งแต่เวอร์ชัน 1.0 ถึงเวอร์ชัน 2.6.0 เราได้เผยแพร่เวอร์ชันที่เกี่ยวข้อง ของไลบรารีความเข้ากันได้ของ Places เพื่อให้ฟังก์ชันการทำงานที่เทียบเท่ากัน

การหยุดให้บริการและการเลิกใช้งานไลบรารีความเข้ากันได้ของ Places

เราเลิกใช้งานไลบรารีความเข้ากันได้ทุกเวอร์ชันสำหรับ Places SDK สำหรับ Android ตั้งแต่วันที่ 31 มีนาคม 2022 เวอร์ชัน 2.6.0 เป็นเวอร์ชันสุดท้ายของ ไลบรารีความเข้ากันได้ของ Places วิธีเดียวในการเข้าถึงฟีเจอร์และการแก้ไขข้อบกพร่องใน Places SDK สำหรับ Android เวอร์ชัน 2.6.0 ขึ้นไปคือการใช้ Places SDK สำหรับ Android

Google ขอแนะนำให้คุณย้ายข้อมูลไปยัง Places SDK สำหรับ Android เพื่อเข้าถึงฟีเจอร์ใหม่และการแก้ไขข้อบกพร่องที่สำคัญสำหรับการเปิดตัวที่สูงกว่าเวอร์ชัน 2.6.0 หากปัจจุบันคุณใช้ไลบรารีความเข้ากันได้ ให้ทำตามขั้นตอนด้านล่างในส่วนติดตั้ง Places SDK สำหรับ Android เพื่อย้ายข้อมูล ไปยัง Places SDK สำหรับ Android

ติดตั้งไลบรารีของไคลเอ็นต์

Places SDK สำหรับ Android เวอร์ชันใหม่จะเผยแพร่เป็น ไลบรารีไคลเอ็นต์แบบคงที่

ใช้ Maven เพื่อเพิ่ม Places SDK สำหรับ Android ลงในโปรเจ็กต์ Android Studio โดยทำดังนี้

  1. หากคุณใช้ไลบรารีความเข้ากันได้ของ Places อยู่ ให้ทำดังนี้

    1. แทนที่บรรทัดต่อไปนี้ในส่วน dependencies

          implementation 'com.google.android.libraries.places:places-compat:X.Y.Z'

      โดยใช้บรรทัดนี้เพื่อเปลี่ยนไปใช้ Places SDK สำหรับ Android

          implementation("com.google.android.libraries.places:places:4.3.1")

  2. หากคุณกำลังใช้ Places SDK สำหรับ Android เวอร์ชันบริการ Play

    1. แทนที่บรรทัดต่อไปนี้ในส่วน dependencies

          implementation 'com.google.android.gms:play-services-places:X.Y.Z'

      โดยใช้บรรทัดนี้เพื่อเปลี่ยนไปใช้ Places SDK สำหรับ Android

          implementation("com.google.android.libraries.places:places:4.3.1")

  3. ซิงค์โปรเจ็กต์ Gradle

  4. ตั้งค่า minSdkVersion สำหรับโปรเจ็กต์แอปพลิเคชันเป็น 23 ขึ้นไป

  5. อัปเดตชิ้นงาน "ขับเคลื่อนโดย Google"

    @drawable/powered_by_google_light // OLD
    @drawable/places_powered_by_google_light // NEW
    @drawable/powered_by_google_dark // OLD
    @drawable/places_powered_by_google_dark // NEW
  6. สร้างแอป หากเห็นข้อผิดพลาดในการสร้างเนื่องจากการแปลงเป็น Places SDK สำหรับ Android โปรดดูข้อมูลเกี่ยวกับ การแก้ไขข้อผิดพลาดเหล่านี้ในส่วนด้านล่าง

เริ่มต้นไคลเอ็นต์ Places SDK ใหม่

เริ่มต้นไคลเอ็นต์ Places SDK ใหม่ตามที่แสดงในตัวอย่างต่อไปนี้

// Add an import statement for the client library.
import com.google.android.libraries.places.api.Places;

...

// Initialize Places.
Places.initialize(getApplicationContext(), apiKey);

// Create a new Places client instance.
PlacesClient placesClient = Places.createClient(this);

รหัสสถานะ

รหัสสถานะสำหรับข้อผิดพลาดเกี่ยวกับขีดจำกัด QPS มีการเปลี่ยนแปลง ตอนนี้ระบบจะแสดงข้อผิดพลาดเกี่ยวกับขีดจำกัด QPS ผ่าน PlaceStatusCodes.OVER_QUERY_LIMIT ไม่มีการจำกัด QPD อีกต่อไป

เพิ่มรหัสสถานะต่อไปนี้แล้ว

  • REQUEST_DENIED — คำขอถูกปฏิเสธ เหตุผลที่เป็นไปได้สำหรับความแตกต่างดังกล่าวมีดังต่อไปนี้

    • ไม่ได้ระบุคีย์ API
    • ระบุคีย์ API ไม่ถูกต้อง
    • ไม่ได้เปิดใช้ Places API ใน Cloud Console
    • มีการระบุคีย์ API ที่มีข้อจำกัดของคีย์ไม่ถูกต้อง
  • INVALID_REQUEST — คำขอไม่ถูกต้องเนื่องจากอาร์กิวเมนต์ขาดหายไปหรือไม่ถูกต้อง

  • NOT_FOUND — ไม่พบผลลัพธ์สำหรับคำขอที่ระบุ

วิธีการใหม่

Places SDK สำหรับ Android เวอร์ชันใหม่มีเมธอดใหม่ทั้งหมด ซึ่งออกแบบมาให้สอดคล้องกัน วิธีการใหม่ทั้งหมด เป็นไปตามข้อกำหนดต่อไปนี้

  • ปลายทางจะไม่ใช้กริยา get อีกต่อไป
  • ออบเจ็กต์คำขอและคำตอบใช้ชื่อเดียวกันกับเมธอดไคลเอ็นต์ที่เกี่ยวข้อง
  • ตอนนี้ออบเจ็กต์คำขอมีบิลเดอร์แล้ว โดยจะส่งพารามิเตอร์ที่จำเป็นเป็นพารามิเตอร์บิลเดอร์คำขอ
  • ระบบจะไม่ใช้บัฟเฟอร์อีกต่อไป

ส่วนนี้จะแนะนำวิธีการใหม่ๆ และแสดงวิธีการทำงาน

ดึงข้อมูลสถานที่ตามรหัส

ใช้ fetchPlace() เพื่อดูรายละเอียดเกี่ยวกับสถานที่หนึ่งๆ fetchPlace() ทำงานคล้ายกับ getPlaceById()

ทำตามขั้นตอนต่อไปนี้เพื่อดึงข้อมูลสถานที่

  1. เรียกใช้ fetchPlace() โดยส่งออบเจ็กต์ FetchPlaceRequest ที่ระบุรหัสสถานที่และรายการฟิลด์ที่ระบุข้อมูลสถานที่ที่จะแสดงผล

    // Define a Place ID.
    String placeId = "INSERT_PLACE_ID_HERE";
    
    // Specify the fields to return.
    List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.DISPLAY_NAME);
    
    // Construct a request object, passing the place ID and fields array.
    FetchPlaceRequest request = FetchPlaceRequest.builder(placeId, placeFields)
            .build();
    
    
  2. โทรหา addOnSuccessListener() เพื่อจัดการกับ FetchPlaceResponse ระบบจะแสดงผลลัพธ์เดียว Place

    // Add a listener to handle the response.
    placesClient.fetchPlace(request).addOnSuccessListener((response) -> {
      Place place = response.getPlace();
      Log.i(TAG, "Place found: " + place.getName());
    }).addOnFailureListener((exception) -> {
        if (exception instanceof ApiException) {
            ApiException apiException = (ApiException) exception;
            int statusCode = apiException.getStatusCode();
            // Handle error with given status code.
            Log.e(TAG, "Place not found: " + exception.getMessage());
        }
    });
    

ดึงข้อมูลรูปภาพสถานที่

ใช้ fetchPhoto() เพื่อรับรูปภาพสถานที่ fetchPhoto() แสดงรูปภาพของสถานที่ เราได้ปรับรูปแบบ การขอรูปภาพให้ง่ายขึ้น ตอนนี้คุณขอ PhotoMetadata ได้โดยตรงจากออบเจ็กต์ Place แล้ว ไม่จำเป็นต้องส่งคำขอแยกต่างหากอีกต่อไป รูปภาพมีความกว้างหรือสูงได้สูงสุด 1,600 พิกเซล fetchPhoto() ทำงาน คล้ายกับ getPhoto()

ทำตามขั้นตอนต่อไปนี้เพื่อดึงรูปภาพของสถานที่

  1. ตั้งค่าการโทรหา fetchPlace() อย่าลืมใส่ฟิลด์ PHOTO_METADATAS ในคำขอ

    List<Place.Field> fields = Arrays.asList(Place.Field.PHOTO_METADATAS);
    
  2. รับออบเจ็กต์สถานที่ (ตัวอย่างนี้ใช้ fetchPlace() แต่คุณใช้ findCurrentPlace() ได้ด้วย)

    FetchPlaceRequest placeRequest = FetchPlaceRequest.builder(placeId, fields).build();
    
  3. เพิ่ม OnSuccessListener เพื่อรับข้อมูลเมตาของรูปภาพจาก Place ที่ได้ใน FetchPlaceResponse จากนั้นใช้ข้อมูลเมตาของรูปภาพที่ได้เพื่อรับบิตแมปและข้อความระบุแหล่งที่มา

    placesClient.fetchPlace(placeRequest).addOnSuccessListener((response) -> {
        Place place = response.getPlace();
    
        // Get the photo metadata.
        PhotoMetadata photoMetadata = place.getPhotoMetadatas().get(0);
    
        // Get the attribution text.
        String attributions = photoMetadata.getAttributions();
    
        // Create a FetchPhotoRequest.
        FetchPhotoRequest photoRequest = FetchPhotoRequest.builder(photoMetadata)
                .setMaxWidth(500) // Optional.
                .setMaxHeight(300) // Optional.
                .build();
        placesClient.fetchPhoto(photoRequest).addOnSuccessListener((fetchPhotoResponse) -> {
            Bitmap bitmap = fetchPhotoResponse.getBitmap();
            imageView.setImageBitmap(bitmap);
        }).addOnFailureListener((exception) -> {
            if (exception instanceof ApiException) {
                ApiException apiException = (ApiException) exception;
                int statusCode = apiException.getStatusCode();
                // Handle error with given status code.
                Log.e(TAG, "Place not found: " + exception.getMessage());
            }
        });
    });
    

ค้นหาสถานที่จากตำแหน่งของผู้ใช้

ใช้ findCurrentPlace() เพื่อค้นหาตำแหน่งปัจจุบันของอุปกรณ์ของผู้ใช้ findCurrentPlace() จะแสดงรายการ PlaceLikelihoods ที่ระบุสถานที่ซึ่งอุปกรณ์ของผู้ใช้ มีแนวโน้มที่จะอยู่มากที่สุด findCurrentPlace() ทำงานคล้ายกับ getCurrentPlace()

ทำตามขั้นตอนต่อไปนี้เพื่อดูตำแหน่งปัจจุบันของอุปกรณ์ของผู้ใช้

  1. ตรวจสอบว่าแอปขอสิทธิ์ ACCESS_FINE_LOCATION และ ACCESS_WIFI_STATE ผู้ใช้ต้องให้สิทธิ์เข้าถึง ตำแหน่งปัจจุบันของอุปกรณ์ ดูรายละเอียดได้ที่ขอสิทธิ์ของแอป

  2. สร้าง FindCurrentPlaceRequest รวมถึงรายการประเภทข้อมูลสถานที่ที่จะ แสดง

      // Use fields to define the data types to return.
      List<Place.Field> placeFields = Arrays.asList(Place.Field.DISPLAY_NAME);
    
      // Use the builder to create a FindCurrentPlaceRequest.
      FindCurrentPlaceRequest request =
              FindCurrentPlaceRequest.builder(placeFields).build();
    
  3. เรียกใช้ findCurrentPlace และจัดการการตอบกลับ โดยตรวจสอบก่อนเพื่อยืนยันว่า ผู้ใช้ได้ให้สิทธิ์ในการใช้ตำแหน่งของอุปกรณ์แล้ว

      // Call findCurrentPlace and handle the response (first check that the user has granted permission).
      if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
          placesClient.findCurrentPlace(request).addOnSuccessListener(((response) -> {
              for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
                  Log.i(TAG, String.format("Place '%s' has likelihood: %f",
                          placeLikelihood.getPlace().getName(),
                          placeLikelihood.getLikelihood()));
                  textView.append(String.format("Place '%s' has likelihood: %f\n",
                          placeLikelihood.getPlace().getName(),
                          placeLikelihood.getLikelihood()));
              }
          })).addOnFailureListener((exception) -> {
              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();
      }
    

ค้นหาการคาดคะเนการเติมข้อความอัตโนมัติ

ใช้ findAutocompletePredictions() เพื่อแสดงการคาดคะเนสถานที่เพื่อตอบสนองต่อคำค้นหาของผู้ใช้ findAutocompletePredictions() ทำงานคล้ายกับ getAutocompletePredictions()

ตัวอย่างต่อไปนี้แสดงการเรียกใช้ findAutocompletePredictions()

// Create a new token for the autocomplete session. Pass this to FindAutocompletePredictionsRequest,
// and once again when the user makes a selection (for example when calling fetchPlace()).
AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
// Create a RectangularBounds object.
RectangularBounds bounds = RectangularBounds.newInstance(
  new LatLng(-33.880490, 151.184363),
  new LatLng(-33.858754, 151.229596));
// Use the builder to create a FindAutocompletePredictionsRequest.
FindAutocompletePredictionsRequest request = FindAutocompletePredictionsRequest.builder()
// Call either setLocationBias() OR setLocationRestriction().
   .setLocationBias(bounds)
   //.setLocationRestriction(bounds)
   .setCountry("au")
   .setTypesFilter(Arrays.asList(PlaceTypes.ADDRESS))
   .setSessionToken(token)
   .setQuery(query)
   .build();

placesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {
   for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
       Log.i(TAG, prediction.getPlaceId());
       Log.i(TAG, prediction.getPrimaryText(null).toString());
   }
}).addOnFailureListener((exception) -> {
   if (exception instanceof ApiException) {
       ApiException apiException = (ApiException) exception;
       Log.e(TAG, "Place not found: " + apiException.getStatusCode());
   }
});

โทเค็นของเซสชัน

โทเค็นเซสชันจะจัดกลุ่มระยะการค้นหาและการเลือกของผู้ใช้ไว้ในเซสชันที่แยกต่างหากเพื่อวัตถุประสงค์ในการเรียกเก็บเงิน เราขอแนะนำให้ใช้โทเค็นเซสชันสำหรับ เซสชันการเติมข้อความอัตโนมัติทั้งหมด เซสชันจะเริ่มต้นเมื่อผู้ใช้เริ่มพิมพ์คำค้นหา และสิ้นสุดเมื่อผู้ใช้เลือกสถานที่ แต่ละเซสชันมีคำค้นหาได้หลายรายการ ตามด้วยการเลือกสถานที่ 1 รายการ เมื่อเซสชันสิ้นสุดลง โทเค็นจะใช้ไม่ได้อีกต่อไป แอปของคุณต้องสร้างโทเค็นใหม่สำหรับแต่ละเซสชัน

ฟิลด์มาสก์

ในเมธอดที่แสดงรายละเอียดสถานที่ คุณต้องระบุประเภทข้อมูลสถานที่ ที่จะแสดงพร้อมกับคำขอแต่ละรายการ ซึ่งจะช่วยให้คุณขอ (และชำระเงิน) เฉพาะข้อมูลที่คุณจะใช้จริงเท่านั้น

หากต้องการระบุประเภทข้อมูลที่จะแสดงผล ให้ส่งอาร์เรย์ของ Place.Field ใน FetchPlaceRequest ดังที่แสดงในตัวอย่างต่อไปนี้

// Include address, ID, and phone number.
List<Place.Field> placeFields = Arrays.asList(Place.Field.FORMATTED_ADDRESS,
                                              Place.Field.ID,
                                              Place.Field.INTERNATIONAL_PHONE_NUMBER);

ดูรายการช่องที่คุณใช้ในมาสก์ของช่องได้ที่วางช่องข้อมูล (ใหม่)

อ่านเพิ่มเติมเกี่ยวกับ SKU ของข้อมูลสถานที่

การอัปเดตเครื่องมือเลือกสถานที่และ Autocomplete

ส่วนนี้จะอธิบายการเปลี่ยนแปลงวิดเจ็ต Places (เครื่องมือเลือกสถานที่และ การเติมข้อความอัตโนมัติ)

การเติมข้อความอัตโนมัติแบบเป็นโปรแกรม

เราได้ทำการเปลี่ยนแปลงต่อไปนี้กับการเติมข้อความอัตโนมัติ

  • เปลี่ยนชื่อ PlaceAutocomplete เป็น Autocomplete
    • เปลี่ยนชื่อ PlaceAutocomplete.getPlace เป็น Autocomplete.getPlaceFromIntent
    • เปลี่ยนชื่อ PlaceAutocomplete.getStatus เป็น Autocomplete.getStatusFromIntent
  • เปลี่ยนชื่อ PlaceAutocomplete.RESULT_ERROR เป็น AutocompleteActivity.RESULT_ERROR (การจัดการข้อผิดพลาดสำหรับส่วนย่อยการเติมข้อความอัตโนมัติยังคงเหมือนเดิม)

เครื่องมือเลือกสถานที่

เราเลิกใช้งานเครื่องมือเลือกสถานที่เมื่อวันที่ 29 มกราคม 2019 เราได้ปิดฟีเจอร์นี้ไปเมื่อวันที่ 29 กรกฎาคม 2019 และไม่มีให้บริการอีกต่อไป การใช้งานต่อไปจะทำให้คุณได้รับข้อความแสดงข้อผิดพลาด SDK ใหม่ไม่รองรับเครื่องมือเลือกสถานที่

วิดเจ็ตเติมข้อความอัตโนมัติ

เราได้อัปเดตวิดเจ็ตการเติมข้อความอัตโนมัติแล้ว ดังนี้

  • ระบบได้นำคำนำหน้า Place ออกจากชั้นเรียนทั้งหมดแล้ว
  • เพิ่มการรองรับโทเค็นเซสชัน วิดเจ็ตจะจัดการโทเค็นให้คุณ โดยอัตโนมัติในเบื้องหลัง
  • เพิ่มการรองรับมาสก์ฟิลด์ ซึ่งช่วยให้คุณเลือกประเภทข้อมูลสถานที่ที่จะแสดงหลังจากที่ผู้ใช้เลือก

ส่วนต่อไปนี้แสดงวิธีเพิ่มวิดเจ็ตการเติมข้อความอัตโนมัติลงในโปรเจ็กต์

ฝัง AutocompleteFragment

หากต้องการเพิ่มข้อความที่เติมข้อความอัตโนมัติ ให้ทำตามขั้นตอนต่อไปนี้

  1. เพิ่ม Fragment ลงในเลย์เอาต์ XML ของกิจกรรม ดังที่แสดงในตัวอย่างต่อไปนี้

    <fragment
      android:id="@+id/autocomplete_fragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:name=
    "com.google.android.libraries.places.widget.AutocompleteSupportFragment"
      />
    
  2. หากต้องการเพิ่มวิดเจ็ตการเติมข้อความอัตโนมัติลงในกิจกรรม ให้ทำตามขั้นตอนต่อไปนี้

    • เริ่มต้น Places โดยส่งบริบทของแอปพลิเคชันและคีย์ API
    • เริ่มต้น AutocompleteSupportFragment
    • เรียกใช้ setPlaceFields() เพื่อระบุประเภทข้อมูลสถานที่ที่คุณต้องการ รับ
    • เพิ่ม PlaceSelectionListener เพื่อดำเนินการกับผลลัพธ์ รวมถึง จัดการข้อผิดพลาดที่อาจเกิดขึ้น

    ตัวอย่างต่อไปนี้แสดงการเพิ่มวิดเจ็ตการเติมข้อความอัตโนมัติลงในกิจกรรม

    /**
     * Initialize Places. For simplicity, the API key is hard-coded. In a production
     * environment we recommend using a secure mechanism to manage API keys.
     */
    if (!Places.isInitialized()) {
        Places.initialize(getApplicationContext(), "YOUR_API_KEY");
    }
    
    // Initialize the AutocompleteSupportFragment.
    AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
            getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
    
    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.DISPLAY_NAME));
    
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
        }
    
        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i(TAG, "An error occurred: " + status);
        }
    });
    

ใช้ Intent เพื่อเปิดกิจกรรมการเติมข้อความอัตโนมัติ

  1. เริ่มต้น Places โดยส่งบริบทของแอปและคีย์ API
  2. ใช้ Autocomplete.IntentBuilder เพื่อสร้าง Intent โดยส่งPlaceAutocompleteโหมดที่ต้องการ (เต็มหน้าจอหรือซ้อนทับ) Intent ต้องเรียกใช้ startActivityForResult โดยส่งรหัสคำขอที่ระบุ Intent
  3. แทนที่onActivityResultการเรียกกลับเพื่อรับสถานที่ที่เลือก

ตัวอย่างต่อไปนี้แสดงวิธีใช้ Intent เพื่อเปิดใช้การเติมข้อความอัตโนมัติ แล้วจัดการผลลัพธ์

    /**
     * Initialize Places. For simplicity, the API key is hard-coded. In a production
     * environment we recommend using a secure mechanism to manage API keys.
     */
    if (!Places.isInitialized()) {
        Places.initialize(getApplicationContext(), "YOUR_API_KEY");
    }

    ...

    // Set the fields to specify which types of place data to return.
    List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.DISPLAY_NAME);

    // Start the autocomplete intent.
    Intent intent = new Autocomplete.IntentBuilder(
            AutocompleteActivityMode.FULLSCREEN, fields)
            .build(this);
    startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

    ...

    /**
     * Override the activity's onActivityResult(), check the request code, and
     * do something with the returned place data (in this example its place name and place ID).
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Place place = Autocomplete.getPlaceFromIntent(data);
                Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
            } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                // TODO: Handle the error.
                Status status = Autocomplete.getStatusFromIntent(data);
                Log.i(TAG, status.getStatusMessage());
            } else if (resultCode == RESULT_CANCELED) {
                // The user canceled the operation.
            }
        }
    }

เครื่องมือเลือกสถานที่ไม่พร้อมใช้งานอีกต่อไป

เราเลิกใช้งานเครื่องมือเลือกสถานที่เมื่อวันที่ 29 มกราคม 2019 เราได้ปิดฟีเจอร์นี้ไปเมื่อวันที่ 29 กรกฎาคม 2019 และไม่มีให้บริการอีกต่อไป การใช้งานต่อไปจะทำให้คุณได้รับข้อความแสดงข้อผิดพลาด SDK ใหม่ไม่รองรับเครื่องมือเลือกสถานที่