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

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

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

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