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

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

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

    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 ได้แล้วตอนนี้ จึงไม่จำเป็นต้องส่งคำขอแยกต่างหากอีก รูปภาพมีความกว้างหรือความสูงได้สูงสุด 1600 พิกเซล fetchPhoto() ทำงานคล้ายกับ getPhoto()

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

  1. ตั้งค่าการโทรไปยัง fetchPlace() อย่าลืมระบุช่อง PHOTO_METADATAS ในคำขอ

    List<Place.Field> fields = Arrays.asList(Place.Field.PHOTO_METADATAS);
    
  2. รับออบเจ็กต์ Place (ตัวอย่างนี้ใช้ 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 เพื่อรับสถานที่ที่เลือก

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

    /**
     * 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 ใหม่ไม่สนับสนุนเครื่องมือเลือกสถานที่