คู่มือนี้จะอธิบายการเปลี่ยนแปลงระหว่างไลบรารีความเข้ากันได้ของ Places กับ Places SDK สำหรับ Android เวอร์ชันใหม่แบบสแตนด์อโลน หากคุณใช้ไลบรารีความเข้ากันได้ของ Places แทนการเปลี่ยนไปใช้ Places SDK เวอร์ชันสแตนด์อโลนเวอร์ชันใหม่ คู่มือนี้จะแสดงวิธีอัปเดตโปรเจ็กต์ของคุณให้ใช้ 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 เพื่อเพิ่ม SDK ของ Places สำหรับ Android ลงในโปรเจ็กต์ Android Studio โดยทำดังนี้
หากคุณกำลังใช้ไลบรารีความเข้ากันได้ของ Places:
แทนที่บรรทัดต่อไปนี้ในส่วน
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'
หากคุณกำลังใช้ Places SDK เวอร์ชันบริการ Google Play สำหรับ Android อยู่ ให้ทำดังนี้
แทนที่บรรทัดต่อไปนี้ในส่วน
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'
ซิงค์โปรเจ็กต์ Gradle
ตั้งค่า
minSdkVersion
สำหรับโปรเจ็กต์แอปพลิเคชันเป็น 16 ขึ้นไปอัปเดตชิ้นงาน "ขับเคลื่อนโดย 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
สร้างแอปของคุณ หากคุณเห็นข้อผิดพลาดในการสร้างที่เกิดจากการแปลงไปเป็น 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()
ทำตามขั้นตอนเหล่านี้เพื่อดึงข้อมูลสถานที่
เรียก
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();
โปรดโทรไปที่
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()
ทำตามขั้นตอนเหล่านี้เพื่อดึงรูปภาพสถานที่:
นัดหมายการโทรไปยัง
fetchPlace()
อย่าลืมใส่ช่องPHOTO_METADATAS
ในคำขอList<Place.Field> fields = Arrays.asList(Place.Field.PHOTO_METADATAS);
รับออบเจ็กต์ Place (ตัวอย่างนี้ใช้
fetchPlace()
แต่คุณยังสามารถใช้findCurrentPlace()
) ได้ด้วย)FetchPlaceRequest placeRequest = FetchPlaceRequest.builder(placeId, fields).build();
เพิ่ม
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()
ทำตามขั้นตอนต่อไปนี้เพื่อรับตำแหน่งปัจจุบันของอุปกรณ์ของผู้ใช้
ตรวจสอบว่าแอปขอสิทธิ์
ACCESS_FINE_LOCATION
และACCESS_WIFI_STATE
ผู้ใช้ต้องให้สิทธิ์เข้าถึงตำแหน่งปัจจุบันของอุปกรณ์ ดูรายละเอียดในขอสิทธิ์ของแอปสร้าง
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();
เรียก 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
หากต้องการเพิ่มส่วนย่อยการเติมข้อความอัตโนมัติ ให้ทำตามขั้นตอนต่อไปนี้
เพิ่ม 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" />
หากต้องการเพิ่มวิดเจ็ตการเติมข้อความอัตโนมัติไปยังกิจกรรม ให้ทำตามขั้นตอนต่อไปนี้
- เริ่มต้น
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 เพื่อเปิดกิจกรรมการเติมข้อความอัตโนมัติ
- เริ่มต้น
Places
, ส่งบริบทของแอปและคีย์ API - ใช้
Autocomplete.IntentBuilder
เพื่อสร้าง Intent ผ่านโหมดPlaceAutocomplete
ที่ต้องการ (เต็มหน้าจอหรือซ้อนทับ) Intent ต้องเรียกใช้startActivityForResult
โดยส่งผ่านรหัสคำขอที่ระบุความตั้งใจของคุณ - ลบล้างโค้ดเรียกกลับของ
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 ใหม่ไม่รองรับเครื่องมือเลือกสถานที่