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

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

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

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

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

  • Places SDK สำหรับ Android เวอร์ชันใหม่จะเผยแพร่ เป็นไลบรารีของไคลเอ็นต์แบบคงที่ Places SDK สำหรับ Android ได้เปิดตัวผ่านบริการ Google Play ก่อนเดือนมกราคม 2019 นับจากนั้นมา เราก็ได้จัดเตรียมไลบรารีความเข้ากันได้ของ 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 เป็นเวอร์ชันสแตนด์อโลนใหม่เป็นการชั่วคราวจนกว่านักพัฒนาแอปจะย้ายข้อมูลโค้ดไปใช้ชื่อใหม่ใน 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:3.3.0'

  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:3.3.0'

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

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

  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.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() จะแสดงรายการ PlaceLikelihood ซึ่งระบุสถานที่ที่อุปกรณ์ของผู้ใช้น่าจะอยู่มากที่สุด 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.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.ADDRESS,
                                              Place.Field.ID,
                                              Place.Field.PHONE_NUMBER);

คุณสามารถใช้ช่องต่อไปนี้อย่างน้อย 1 ช่อง

  • Place.Field.ADDRESS
  • Place.Field.ID
  • Place.Field.LAT_LNG
  • Place.Field.NAME
  • Place.Field.OPENING_HOURS
  • Place.Field.PHONE_NUMBER
  • Place.Field.PHOTO_METADATAS
  • Place.Field.PLUS_CODE
  • Place.Field.PRICE_LEVEL
  • Place.Field.RATING
  • Place.Field.TYPES
  • Place.Field.USER_RATINGS_TOTAL
  • Place.Field.VIEWPORT
  • Place.Field.WEBSITE_URI

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

การอัปเดตเครื่องมือเลือกสถานที่และการเติมข้อความอัตโนมัติ

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

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

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

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

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

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

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

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

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

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

ฝัง AutocompleteFragment

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

  1. เพิ่มส่วนย่อยในเลย์เอาต์ 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.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 โดยส่งผ่านรหัสคำขอที่ระบุความตั้งใจของคุณ
  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.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 ใหม่ไม่รองรับเครื่องมือเลือกสถานที่