บริการเติมข้อความอัตโนมัติใน Places SDK สําหรับ iOS จะแสดงผลการคาดคะเนสถานที่เพื่อตอบสนองต่อคําค้นหาของผู้ใช้ เมื่อผู้ใช้พิมพ์ บริการเติมข้อความอัตโนมัติจะแสดงคําแนะนําเกี่ยวกับสถานที่อย่างธุรกิจ ที่อยู่ โค้ด Plus และจุดสนใจ
คุณเพิ่มการเติมข้อความอัตโนมัติลงในแอปได้ด้วยวิธีต่อไปนี้
- เพิ่มการควบคุม UI เติมข้อความอัตโนมัติเพื่อประหยัดเวลาในการพัฒนาและสร้างประสบการณ์ของผู้ใช้ที่สอดคล้องกัน
- รับการคาดการณ์เกี่ยวกับสถานที่แบบเป็นโปรแกรมเพื่อสร้างประสบการณ์ที่กําหนดเองของผู้ใช้
การเพิ่มการควบคุม UI ที่เติมข้อความอัตโนมัติ
การควบคุม UI การเติมข้อความอัตโนมัติเป็นกล่องโต้ตอบที่มีฟังก์ชันการเติมข้อความอัตโนมัติในตัว เมื่อผู้ใช้ป้อนข้อความค้นหา ตัวควบคุมจะแสดงรายการสถานที่ที่คาดการณ์ให้เลือก เมื่อผู้ใช้เลือกอินสแตนซ์ ระบบจะส่งคืนอินสแตนซ์ GMSPlace
ซึ่งแอปสามารถใช้เพื่อดูรายละเอียดเกี่ยวกับสถานที่ที่เลือก
คุณเพิ่มการควบคุม UI ที่เติมข้อความอัตโนมัติลงในแอปได้ด้วยวิธีต่อไปนี้
การเพิ่มตัวควบคุมแบบเต็มหน้าจอ
ใช้การควบคุมแบบเต็มหน้าจอเมื่อคุณต้องการใช้บริบทโมดัล ซึ่ง UI เติมข้อความอัตโนมัติจะแทนที่ UI ของแอปชั่วคราวจนกว่าผู้ใช้จะเลือก ฟังก์ชันการทํางานนี้ให้บริการโดยคลาส GMSAutocompleteViewController
เมื่อผู้ใช้เลือกสถานที่ แอปของคุณจะได้รับการเรียกกลับ
วิธีเพิ่มการควบคุมแบบเต็มหน้าจอลงในแอป
- สร้างองค์ประกอบ UI ในแอปหลักเพื่อเปิดการควบคุม UI เติมข้อความอัตโนมัติ เช่น เครื่องจัดการการแตะใน
UIButton
- ใช้โปรโตคอล
GMSAutocompleteViewControllerDelegate
ในตัวควบคุมมุมมองระดับบนสุด - สร้างอินสแตนซ์ของ
GMSAutocompleteViewController
และกําหนดตัวควบคุมมุมมองระดับบนสุดเป็นพร็อพเพอร์ตี้ที่ได้รับมอบสิทธิ์ - สร้าง
GMSPlaceField
เพื่อกําหนดประเภทข้อมูลสถานที่ที่จะแสดง - เพิ่ม
GMSAutocompleteFilter
เพื่อจํากัดการค้นหาไปยังประเภทสถานที่ที่เฉพาะเจาะจง - นําเสนอ
GMSAutocompleteViewController
โดยใช้[self presentViewController...]
- จัดการการเลือกของผู้ใช้ในวิธีการมอบสิทธิ์แบบ
didAutocompleteWithPlace
- ปิดตัวควบคุมในวิธีมอบสิทธิ์
didAutocompleteWithPlace
,didFailAutocompleteWithError
และwasCancelled
ตัวอย่างต่อไปนี้สาธิตวิธีการหนึ่งในการเปิด
GMSAutocompleteViewController
เพื่อตอบสนองต่อการแตะปุ่มของผู้ใช้
Swift
import UIKit import GooglePlaces class ViewController: UIViewController { override func viewDidLoad() { makeButton() } // Present the Autocomplete view controller when the button is pressed. @objc func autocompleteClicked(_ sender: UIButton) { let autocompleteController = GMSAutocompleteViewController() autocompleteController.delegate = self // Specify the place data types to return. let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) | UInt(GMSPlaceField.placeID.rawValue))! autocompleteController.placeFields = fields // Specify a filter. let filter = GMSAutocompleteFilter() filter.types = [.address] autocompleteController.autocompleteFilter = filter // Display the autocomplete view controller. present(autocompleteController, animated: true, completion: nil) } // Add a button to the view. func makeButton() { let btnLaunchAc = UIButton(frame: CGRect(x: 5, y: 150, width: 300, height: 35)) btnLaunchAc.backgroundColor = .blue btnLaunchAc.setTitle("Launch autocomplete", for: .normal) btnLaunchAc.addTarget(self, action: #selector(autocompleteClicked), for: .touchUpInside) self.view.addSubview(btnLaunchAc) } } extension ViewController: GMSAutocompleteViewControllerDelegate { // Handle the user's selection. func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) { print("Place name: \(place.name)") print("Place ID: \(place.placeID)") print("Place attributions: \(place.attributions)") dismiss(animated: true, completion: nil) } func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) { // TODO: handle the error. print("Error: ", error.localizedDescription) } // User canceled the operation. func wasCancelled(_ viewController: GMSAutocompleteViewController) { dismiss(animated: true, completion: nil) } // Turn the network activity indicator on and off again. func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = true } func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = false } }
Objective-C
#import "ViewController.h" @import GooglePlaces; @interface ViewController () <GMSAutocompleteViewControllerDelegate> @end @implementation ViewController { GMSAutocompleteFilter *_filter; } - (void)viewDidLoad { [super viewDidLoad]; [self makeButton]; } // Present the autocomplete view controller when the button is pressed. - (void)autocompleteClicked { GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init]; acController.delegate = self; // Specify the place data types to return. GMSPlaceField fields = (GMSPlaceFieldName | GMSPlaceFieldPlaceID); acController.placeFields = fields; // Specify a filter. _filter = [[GMSAutocompleteFilter alloc] init]; _filter.types = @[ kGMSPlaceTypeBank ]; acController.autocompleteFilter = _filter; // Display the autocomplete view controller. [self presentViewController:acController animated:YES completion:nil]; } // Add a button to the view. - (void)makeButton{ UIButton *btnLaunchAc = [UIButton buttonWithType:UIButtonTypeCustom]; [btnLaunchAc addTarget:self action:NSSelectorFromString(@"autocompleteClicked") forControlEvents:UIControlEventTouchUpInside]; [btnLaunchAc setTitle:@"Launch autocomplete" forState:UIControlStateNormal]; btnLaunchAc.frame = CGRectMake(5.0, 150.0, 300.0, 35.0); btnLaunchAc.backgroundColor = [UIColor blueColor]; [self.view addSubview:btnLaunchAc]; } // Handle the user's selection. - (void)viewController:(GMSAutocompleteViewController *)viewController didAutocompleteWithPlace:(GMSPlace *)place { [self dismissViewControllerAnimated:YES completion:nil]; // Do something with the selected place. NSLog(@"Place name %@", place.name); NSLog(@"Place ID %@", place.placeID); NSLog(@"Place attributions %@", place.attributions.string); } - (void)viewController:(GMSAutocompleteViewController *)viewController didFailAutocompleteWithError:(NSError *)error { [self dismissViewControllerAnimated:YES completion:nil]; // TODO: handle the error. NSLog(@"Error: %@", [error description]); } // User canceled the operation. - (void)wasCancelled:(GMSAutocompleteViewController *)viewController { [self dismissViewControllerAnimated:YES completion:nil]; } // Turn the network activity indicator on and off again. - (void)didRequestAutocompletePredictions:(GMSAutocompleteViewController *)viewController { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; } - (void)didUpdateAutocompletePredictions:(GMSAutocompleteViewController *)viewController { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } @end
การเพิ่มตัวควบคุมผลลัพธ์
ใช้ตัวควบคุมผลลัพธ์เมื่อคุณต้องการควบคุม UI การป้อนข้อความได้มากขึ้น ตัวควบคุมผลลัพธ์จะสลับการแสดงรายการผลการค้นหาแบบไดนามิกโดยอิงตามโฟกัสของ UI การป้อนข้อมูล
วิธีเพิ่มตัวควบคุมผลลัพธ์ลงในแอป
- สร้าง
GMSAutocompleteResultsViewController
- ใช้โปรโตคอล
GMSAutocompleteResultsViewControllerDelegate
ในตัวควบคุมมุมมองระดับบนสุด และกําหนดตัวควบคุมมุมมองระดับบนสุดเป็นพร็อพเพอร์ตี้ผู้รับมอบสิทธิ์ - สร้างออบเจ็กต์
UISearchController
ส่งในGMSAutocompleteResultsViewController
เป็นอาร์กิวเมนต์ตัวควบคุมผลลัพธ์ - ตั้งค่า
GMSAutocompleteResultsViewController
เป็นพร็อพเพอร์ตี้searchResultsUpdater
ของUISearchController
- เพิ่ม
searchBar
สําหรับUISearchController
ลงใน UI ของแอป - จัดการการเลือกของผู้ใช้ในวิธีการมอบสิทธิ์แบบ
didAutocompleteWithPlace
คุณวางแถบค้นหาของ UISearchController
ใน UI ของแอปได้หลายวิธี ดังนี้
การเพิ่มแถบค้นหาลงในแถบนําทาง
ตัวอย่างโค้ดต่อไปนี้สาธิตการเพิ่มตัวควบคุมผลลัพธ์ การเพิ่ม searchBar
ลงในแถบนําทาง และการจัดการการเลือกของผู้ใช้
Swift
class ViewController: UIViewController { var resultsViewController: GMSAutocompleteResultsViewController? var searchController: UISearchController? var resultView: UITextView? override func viewDidLoad() { super.viewDidLoad() resultsViewController = GMSAutocompleteResultsViewController() resultsViewController?.delegate = self searchController = UISearchController(searchResultsController: resultsViewController) searchController?.searchResultsUpdater = resultsViewController // Put the search bar in the navigation bar. searchController?.searchBar.sizeToFit() navigationItem.titleView = searchController?.searchBar // When UISearchController presents the results view, present it in // this view controller, not one further up the chain. definesPresentationContext = true // Prevent the navigation bar from being hidden when searching. searchController?.hidesNavigationBarDuringPresentation = false } } // Handle the user's selection. extension ViewController: GMSAutocompleteResultsViewControllerDelegate { func resultsController(_ resultsController: GMSAutocompleteResultsViewController, didAutocompleteWith place: GMSPlace) { searchController?.isActive = false // Do something with the selected place. print("Place name: \(place.name)") print("Place address: \(place.formattedAddress)") print("Place attributions: \(place.attributions)") } func resultsController(_ resultsController: GMSAutocompleteResultsViewController, didFailAutocompleteWithError error: Error){ // TODO: handle the error. print("Error: ", error.localizedDescription) } // Turn the network activity indicator on and off again. func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = true } func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = false } }
Objective-C
- (void)viewDidLoad { _resultsViewController = [[GMSAutocompleteResultsViewController alloc] init]; _resultsViewController.delegate = self; _searchController = [[UISearchController alloc] initWithSearchResultsController:_resultsViewController]; _searchController.searchResultsUpdater = _resultsViewController; // Put the search bar in the navigation bar. [_searchController.searchBar sizeToFit]; self.navigationItem.titleView = _searchController.searchBar; // When UISearchController presents the results view, present it in // this view controller, not one further up the chain. self.definesPresentationContext = YES; // Prevent the navigation bar from being hidden when searching. _searchController.hidesNavigationBarDuringPresentation = NO; } // Handle the user's selection. - (void)resultsController:(GMSAutocompleteResultsViewController *)resultsController didAutocompleteWithPlace:(GMSPlace *)place { _searchController.active = NO; // Do something with the selected place. NSLog(@"Place name %@", place.name); NSLog(@"Place address %@", place.formattedAddress); NSLog(@"Place attributions %@", place.attributions.string); } - (void)resultsController:(GMSAutocompleteResultsViewController *)resultsController didFailAutocompleteWithError:(NSError *)error { [self dismissViewControllerAnimated:YES completion:nil]; // TODO: handle the error. NSLog(@"Error: %@", [error description]); } // Turn the network activity indicator on and off again. - (void)didRequestAutocompletePredictionsForResultsController: (GMSAutocompleteResultsViewController *)resultsController { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; } - (void)didUpdateAutocompletePredictionsForResultsController: (GMSAutocompleteResultsViewController *)resultsController { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; }
การเพิ่มแถบค้นหาที่ด้านบนของมุมมอง
ตัวอย่างโค้ดต่อไปนี้แสดงการเพิ่ม searchBar
ไว้ด้านบนของมุมมอง
Swift
import UIKit import GooglePlaces class ViewController: UIViewController { var resultsViewController: GMSAutocompleteResultsViewController? var searchController: UISearchController? var resultView: UITextView? override func viewDidLoad() { super.viewDidLoad() resultsViewController = GMSAutocompleteResultsViewController() resultsViewController?.delegate = self searchController = UISearchController(searchResultsController: resultsViewController) searchController?.searchResultsUpdater = resultsViewController let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, height: 45.0)) subView.addSubview((searchController?.searchBar)!) view.addSubview(subView) searchController?.searchBar.sizeToFit() searchController?.hidesNavigationBarDuringPresentation = false // When UISearchController presents the results view, present it in // this view controller, not one further up the chain. definesPresentationContext = true } } // Handle the user's selection. extension ViewController: GMSAutocompleteResultsViewControllerDelegate { func resultsController(_ resultsController: GMSAutocompleteResultsViewController, didAutocompleteWith place: GMSPlace) { searchController?.isActive = false // Do something with the selected place. print("Place name: \(place.name)") print("Place address: \(place.formattedAddress)") print("Place attributions: \(place.attributions)") } func resultsController(_ resultsController: GMSAutocompleteResultsViewController, didFailAutocompleteWithError error: Error){ // TODO: handle the error. print("Error: ", error.localizedDescription) } // Turn the network activity indicator on and off again. func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = true } func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = false } }
Objective-C
- (void)viewDidLoad { [super viewDidLoad]; _resultsViewController = [[GMSAutocompleteResultsViewController alloc] init]; _resultsViewController.delegate = self; _searchController = [[UISearchController alloc] initWithSearchResultsController:_resultsViewController]; _searchController.searchResultsUpdater = _resultsViewController; UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 65.0, 250, 50)]; [subView addSubview:_searchController.searchBar]; [_searchController.searchBar sizeToFit]; [self.view addSubview:subView]; // When UISearchController presents the results view, present it in // this view controller, not one further up the chain. self.definesPresentationContext = YES; } // Handle the user's selection. - (void)resultsController:(GMSAutocompleteResultsViewController *)resultsController didAutocompleteWithPlace:(GMSPlace *)place { [self dismissViewControllerAnimated:YES completion:nil]; // Do something with the selected place. NSLog(@"Place name %@", place.name); NSLog(@"Place address %@", place.formattedAddress); NSLog(@"Place attributions %@", place.attributions.string); } - (void)resultsController:(GMSAutocompleteResultsViewController *)resultsController didFailAutocompleteWithError:(NSError *)error { [self dismissViewControllerAnimated:YES completion:nil]; // TODO: handle the error. NSLog(@"Error: %@", [error description]); } // Turn the network activity indicator on and off again. - (void)didRequestAutocompletePredictionsForResultsController: (GMSAutocompleteResultsViewController *)resultsController { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; } - (void)didUpdateAutocompletePredictionsForResultsController: (GMSAutocompleteResultsViewController *)resultsController { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; }
โดยค่าเริ่มต้น UISearchController
จะซ่อนแถบนําทางเมื่อนําเสนอ (ปิดใช้ได้) ในกรณีที่แถบนําทางมองเห็นได้และทึบแสง UISearchController
จะตั้งค่าตําแหน่งไม่ถูกต้อง
ใช้รหัสต่อไปนี้เพื่อแก้ไขปัญหา
Swift
navigationController?.navigationBar.translucent = false searchController?.hidesNavigationBarDuringPresentation = false // This makes the view area include the nav bar even though it is opaque. // Adjust the view placement down. self.extendedLayoutIncludesOpaqueBars = true self.edgesForExtendedLayout = .top
Objective-C
self.navigationController.navigationBar.translucent = NO; _searchController.hidesNavigationBarDuringPresentation = NO; // This makes the view area include the nav bar even though it is opaque. // Adjust the view placement down. self.extendedLayoutIncludesOpaqueBars = YES; self.edgesForExtendedLayout = UIRectEdgeTop;
การเพิ่มแถบค้นหาโดยใช้ผลการค้นหาป๊อบโอเวอร์
ตัวอย่างโค้ดต่อไปนี้แสดงการวางแถบค้นหาที่ด้านขวาของแถบนําทาง และแสดงผลการค้นหาในป๊อปอัป
Swift
import UIKit import GooglePlaces class ViewController: UIViewController { var resultsViewController: GMSAutocompleteResultsViewController? var searchController: UISearchController? var resultView: UITextView? override func viewDidLoad() { super.viewDidLoad() resultsViewController = GMSAutocompleteResultsViewController() resultsViewController?.delegate = self searchController = UISearchController(searchResultsController: resultsViewController) searchController?.searchResultsUpdater = resultsViewController // Add the search bar to the right of the nav bar, // use a popover to display the results. // Set an explicit size as we don't want to use the entire nav bar. searchController?.searchBar.frame = (CGRect(x: 0, y: 0, width: 250.0, height: 44.0)) navigationItem.rightBarButtonItem = UIBarButtonItem(customView: (searchController?.searchBar)!) // When UISearchController presents the results view, present it in // this view controller, not one further up the chain. definesPresentationContext = true // Keep the navigation bar visible. searchController?.hidesNavigationBarDuringPresentation = false searchController?.modalPresentationStyle = .popover } } // Handle the user's selection. extension ViewController: GMSAutocompleteResultsViewControllerDelegate { func resultsController(_ resultsController: GMSAutocompleteResultsViewController, didAutocompleteWith place: GMSPlace) { searchController?.isActive = false // Do something with the selected place. print("Place name: \(place.name)") print("Place address: \(place.formattedAddress)") print("Place attributions: \(place.attributions)") } func resultsController(_ resultsController: GMSAutocompleteResultsViewController, didFailAutocompleteWithError error: Error){ // TODO: handle the error. print("Error: ", error.localizedDescription) } // Turn the network activity indicator on and off again. func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = true } func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = false } }
Objective-C
- (void)viewDidLoad { [super viewDidLoad]; _resultsViewController = [[GMSAutocompleteResultsViewController alloc] init]; _resultsViewController.delegate = self; _searchController = [[UISearchController alloc] initWithSearchResultsController:_resultsViewController]; _searchController.searchResultsUpdater = _resultsViewController; // Add the search bar to the right of the nav bar, // use a popover to display the results. // Set an explicit size as we don't want to use the entire nav bar. _searchController.searchBar.frame = CGRectMake(0, 0, 250.0f, 44.0f); self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_searchController.searchBar]; // When UISearchController presents the results view, present it in // this view controller, not one further up the chain. self.definesPresentationContext = YES; // Keep the navigation bar visible. _searchController.hidesNavigationBarDuringPresentation = NO; _searchController.modalPresentationStyle = UIModalPresentationPopover; } // Handle the user's selection. - (void)resultsController:(GMSAutocompleteResultsViewController *)resultsController didAutocompleteWithPlace:(GMSPlace *)place { [self dismissViewControllerAnimated:YES completion:nil]; NSLog(@"Place name %@", place.name); NSLog(@"Place address %@", place.formattedAddress); NSLog(@"Place attributions %@", place.attributions.string); } - (void)resultsController:(GMSAutocompleteResultsViewController *)resultsController didFailAutocompleteWithError:(NSError *)error { [self dismissViewControllerAnimated:YES completion:nil]; // TODO: handle the error. NSLog(@"Error: %@", [error description]); } // Turn the network activity indicator on and off again. - (void)didRequestAutocompletePredictionsForResultsController: (GMSAutocompleteResultsViewController *)resultsController { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; } - (void)didUpdateAutocompletePredictionsForResultsController: (GMSAutocompleteResultsViewController *)resultsController { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; }
การใช้แหล่งข้อมูลตาราง
หากแอปมี UI ของข้อความค้นหาที่กําหนดเอง คุณสามารถใช้คลาส GMSAutocompleteTableDataSource
เพื่อเพิ่มมุมมองตารางที่แสดงผลลัพธ์บนตัวควบคุมมุมมอง
วิธีใช้ GMSAutocompleteTableDataSource
เป็นแหล่งข้อมูลและมอบสิทธิ์ของ UITableView
ในตัวควบคุมข้อมูลพร็อพเพอร์ตี้
- นําโปรโตคอล
GMSAutocompleteTableDataSourceDelegate
และUISearchBarDelegate
ไปใช้ในตัวควบคุมการดู - สร้างอินสแตนซ์
GMSAutocompleteTableDataSource
และกําหนดตัวควบคุมข้อมูลพร็อพเพอร์ตี้เป็นพร็อพเพอร์ตี้ที่มอบสิทธิ์ - ตั้งค่า
GMSAutocompleteTableDataSource
เป็นแหล่งข้อมูลและมอบสิทธิ์ของอินสแตนซ์UITableView
บนตัวควบคุมมุมมอง - ในเครื่องจัดการสําหรับการป้อนข้อความค้นหา ให้เรียก
sourceTextHasChanged
ในGMSAutocompleteTableDataSource
- จัดการการเลือกของผู้ใช้ในวิธีการมอบสิทธิ์
didAutocompleteWithPlace
- ปิดตัวควบคุมในเมธอดการมอบสิทธิ์
didAutocompleteWithPlace
,didFailAutocompleteWithError
,wasCancelled
ตัวอย่างโค้ดต่อไปนี้แสดงการใช้คลาส GMSAutocompleteTableDataSource
เพื่อเพิ่มมุมมองตารางของ UIViewController
เมื่อมีการเพิ่ม UISearchBar
แยกต่างหาก
Swift
// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import GooglePlaces import UIKit class PlaceAutocompleteViewController: UIViewController { private var tableView: UITableView! private var tableDataSource: GMSAutocompleteTableDataSource! override func viewDidLoad() { super.viewDidLoad() let searchBar = UISearchBar(frame: CGRect(x: 0, y: 20, width: self.view.frame.size.width, height: 44.0)) searchBar.delegate = self view.addSubview(searchBar) tableDataSource = GMSAutocompleteTableDataSource() tableDataSource.delegate = self tableView = UITableView(frame: CGRect(x: 0, y: 64, width: self.view.frame.size.width, height: self.view.frame.size.height - 44)) tableView.delegate = tableDataSource tableView.dataSource = tableDataSource view.addSubview(tableView) } } extension PlaceAutocompleteViewController: UISearchBarDelegate { func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { // Update the GMSAutocompleteTableDataSource with the search text. tableDataSource.sourceTextHasChanged(searchText) } } extension PlaceAutocompleteViewController: GMSAutocompleteTableDataSourceDelegate { func didUpdateAutocompletePredictions(for tableDataSource: GMSAutocompleteTableDataSource) { // Turn the network activity indicator off. UIApplication.shared.isNetworkActivityIndicatorVisible = false // Reload table data. tableView.reloadData() } func didRequestAutocompletePredictions(for tableDataSource: GMSAutocompleteTableDataSource) { // Turn the network activity indicator on. UIApplication.shared.isNetworkActivityIndicatorVisible = true // Reload table data. tableView.reloadData() } func tableDataSource(_ tableDataSource: GMSAutocompleteTableDataSource, didAutocompleteWith place: GMSPlace) { // Do something with the selected place. print("Place name: \(place.name)") print("Place address: \(place.formattedAddress)") print("Place attributions: \(place.attributions)") } func tableDataSource(_ tableDataSource: GMSAutocompleteTableDataSource, didFailAutocompleteWithError error: Error) { // Handle the error. print("Error: \(error.localizedDescription)") } func tableDataSource(_ tableDataSource: GMSAutocompleteTableDataSource, didSelect prediction: GMSAutocompletePrediction) -> Bool { return true } }
Objective-C
// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #import "PlaceAutocompleteViewController.h" @import GooglePlaces; @import UIKit; @interface PlaceAutocompleteViewController () <GMSAutocompleteTableDataSourceDelegate, UISearchBarDelegate> @end @implementation PlaceAutocompleteViewController { UITableView *tableView; GMSAutocompleteTableDataSource *tableDataSource; } - (void)viewDidLoad { [super viewDidLoad]; UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)]; searchBar.delegate = self; [self.view addSubview:searchBar]; tableDataSource = [[GMSAutocompleteTableDataSource alloc] init]; tableDataSource.delegate = self; tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 44)]; tableView.delegate = tableDataSource; tableView.dataSource = tableDataSource; [self.view addSubview:tableView]; } #pragma mark - GMSAutocompleteTableDataSourceDelegate - (void)didUpdateAutocompletePredictionsForTableDataSource:(GMSAutocompleteTableDataSource *)tableDataSource { // Turn the network activity indicator off. UIApplication.sharedApplication.networkActivityIndicatorVisible = NO; // Reload table data. [tableView reloadData]; } - (void)didRequestAutocompletePredictionsForTableDataSource:(GMSAutocompleteTableDataSource *)tableDataSource { // Turn the network activity indicator on. UIApplication.sharedApplication.networkActivityIndicatorVisible = YES; // Reload table data. [tableView reloadData]; } - (void)tableDataSource:(GMSAutocompleteTableDataSource *)tableDataSource didAutocompleteWithPlace:(GMSPlace *)place { // Do something with the selected place. NSLog(@"Place name: %@", place.name); NSLog(@"Place address: %@", place.formattedAddress); NSLog(@"Place attributions: %@", place.attributions); } - (void)tableDataSource:(GMSAutocompleteTableDataSource *)tableDataSource didFailAutocompleteWithError:(NSError *)error { // Handle the error NSLog(@"Error %@", error.description); } - (BOOL)tableDataSource:(GMSAutocompleteTableDataSource *)tableDataSource didSelectPrediction:(GMSAutocompletePrediction *)prediction { return YES; } #pragma mark - UISearchBarDelegate - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { // Update the GMSAutocompleteTableDataSource with the search text. [tableDataSource sourceTextHasChanged:searchText]; } @end
การปรับแต่งข้อความและสีพื้นหลัง
คุณตั้งค่าสีของข้อความและพื้นหลังทั้งหมดในการควบคุม UI เติมข้อความอัตโนมัติได้ เพื่อให้วิดเจ็ตตรงกับลักษณะที่มองเห็นได้ของแอปมากขึ้น การตั้งค่าสีควบคุม UI ทําได้ 2 วิธี ดังนี้
- โดยใช้ โปรโตคอลลักษณะที่ปรากฏของ UI แบบเนทีฟของ iOS เพื่อจัดรูปแบบการควบคุม UI ทั่วโลกเมื่อเป็นไปได้ การตั้งค่าเหล่านี้มีผลกับองค์ประกอบการควบคุม UI จํานวนมาก แต่ไม่ใช่ทั้งหมด
- การใช้เมธอด SDK ในคลาสวิดเจ็ตเพื่อตั้งค่าพร็อพเพอร์ตี้ที่โปรโตคอล UIลักษณะที่ปรากฏไม่รองรับ
โดยทั่วไปแอปจะใช้โปรโตคอล UIลักษณะที่ปรากฏ และวิธีการ SDK บางอย่างร่วมกัน แผนภาพต่อไปนี้แสดงองค์ประกอบที่จัดรูปแบบได้
ตารางต่อไปนี้แสดงองค์ประกอบ UI ทั้งหมด และระบุการจัดรูปแบบแต่ละรายการ (โปรโตคอล UIลักษณะที่ปรากฏของเมธอดหรือ SDK)
องค์ประกอบ UI | วิธีการ | วิธีการแต่งตัว |
---|---|---|
การปรับสีของแถบนําทาง (พื้นหลัง) | โปรโตคอล UIลักษณะที่ปรากฏ | โทรหา setBarTintColor บนพร็อกซี UINavigationBar |
สีแต้มสีของแถบนําทาง (เคอร์เซอร์ข้อความสําหรับแถบค้นหาและปุ่มยกเลิก) | โปรโตคอล UIลักษณะที่ปรากฏ | โทรหา setTintColor บนพร็อกซี UINavigationBar |
สีข้อความของแถบค้นหา | โปรโตคอล UIลักษณะที่ปรากฏ | ตั้งค่า NSForegroundColorAttributeName ใน searchBarTextAttributes |
สีแต้มสีของแถบค้นหา | ไม่เกี่ยวข้อง | แถบค้นหาเป็นแบบโปร่งแสง และจะแสดงเป็นแถบนําทางในเวอร์ชันสีเทา |
สีข้อความของตัวยึดตําแหน่งแถบค้นหา (ข้อความค้นหาเริ่มต้น) | โปรโตคอล UIลักษณะที่ปรากฏ | ตั้งค่า NSForegroundColorAttributeName ใน placeholderAttributes |
ข้อความหลัก (ใช้ได้กับข้อความแสดงข้อผิดพลาดและข้อความ) | เมธอด SDK | โทรมาที่ primaryTextColor |
ไฮไลต์ข้อความหลัก | เมธอด SDK | โทรมาที่ primaryTextHighlightColor |
ข้อความรอง | เมธอด SDK | โทรมาที่ secondaryTextColor |
ข้อความแสดงข้อผิดพลาดและข้อความ | เมธอด SDK | โทรมาที่ primaryTextColor |
พื้นหลังเซลล์ของตาราง | เมธอด SDK | โทรมาที่ tableCellBackgroundColor |
สีตัวคั่นเซลล์ในตาราง | เมธอด SDK | โทรมาที่ tableCellSeparatorColor |
ปุ่ม "ลองอีกครั้ง" | เมธอด SDK | โทรมาที่ tintColor |
สัญญาณบอกสถานะกิจกรรม (ไอคอนหมุนแสดงความคืบหน้า) | โปรโตคอล UIลักษณะที่ปรากฏ | โทรหา setColor บนพร็อกซี UIActivityIndicatorView |
โลโก้ "ขับเคลื่อนโดย Google" รูปภาพระบบคลาวด์เศร้า | ไม่เกี่ยวข้อง | ระบบจะเลือกเวอร์ชันสีขาวหรือสีเทาโดยอัตโนมัติตามคอนทราสต์ของพื้นหลัง |
แว่นขยายและไอคอนข้อความล้างในช่องข้อความช่องข้อความในแถบค้นหา | ไม่เกี่ยวข้อง | หากต้องการจัดรูปแบบ ให้แทนที่รูปภาพเริ่มต้นด้วยรูปภาพสีที่ต้องการ |
การใช้โปรโตคอล UIลักษณะที่ปรากฏ
คุณใช้โปรโตคอล UIลักษณะที่ปรากฏ เพื่อรับพร็อกซีลักษณะที่ปรากฏสําหรับองค์ประกอบ UI ที่ระบุ ซึ่งคุณจะใช้เพื่อตั้งค่าสีสําหรับองค์ประกอบ UI ได้ เมื่อมีการแก้ไข อินสแตนซ์ทั้งหมดขององค์ประกอบ UI หนึ่งๆ จะได้รับผลกระทบ ตัวอย่างเช่น ตัวอย่างต่อไปนี้จะเปลี่ยนสีของข้อความในคลาส UITextField
เป็นสีเขียวเมื่อมีอยู่ใน UISearchBar
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}];
โปรดดูข้อมูลเพิ่มเติมเกี่ยวกับการระบุค่าสีในข้อมูลอ้างอิงของ UIColor Class
ข้อมูลโค้ดต่อไปนี้แสดงคําสั่งพร็อกซีทั้งหมดที่ต้องใช้ในการจัดรูปแบบทุกอย่างในการควบคุม UI ที่เติมข้อความอัตโนมัติแบบเต็มหน้าจอ เพิ่มโค้ดนี้ลงในเมธอด didFinishLaunchingWithOptions
ใน Appdelegate.m:
// Define some colors. UIColor *darkGray = [UIColor darkGrayColor]; UIColor *lightGray = [UIColor lightGrayColor]; // Navigation bar background. [[UINavigationBar appearance] setBarTintColor:darkGray]; [[UINavigationBar appearance] setTintColor:lightGray]; // Color of typed text in the search bar. NSDictionary *searchBarTextAttributes = @{ NSForegroundColorAttributeName: lightGray, NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]] }; [UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] .defaultTextAttributes = searchBarTextAttributes; // Color of the placeholder text in the search bar prior to text entry. NSDictionary *placeholderAttributes = @{ NSForegroundColorAttributeName: lightGray, NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]] }; // Color of the default search text. // NOTE: In a production scenario, "Search" would be a localized string. NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Search" attributes:placeholderAttributes]; [UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] .attributedPlaceholder = attributedPlaceholder; // Color of the in-progress spinner. [[UIActivityIndicatorView appearance] setColor:lightGray]; // To style the two image icons in the search bar (the magnifying glass // icon and the 'clear text' icon), replace them with different images. [[UISearchBar appearance] setImage:[UIImage imageNamed:@"custom_clear_x_high"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted]; [[UISearchBar appearance] setImage:[UIImage imageNamed:@"custom_clear_x"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal]; [[UISearchBar appearance] setImage:[UIImage imageNamed:@"custom_search"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; // Color of selected table cells. UIView *selectedBackgroundView = [[UIView alloc] init]; selectedBackgroundView.backgroundColor = [UIColor lightGrayColor]; [UITableViewCell appearanceWhenContainedIn:[GMSAutocompleteViewController class], nil] .selectedBackgroundView = selectedBackgroundView;
การตั้งค่าพร็อพเพอร์ตี้รูปแบบการควบคุม UI
องค์ประกอบการควบคุม UI ชุดย่อยมีพร็อพเพอร์ตี้ที่ไม่ได้รับผลกระทบจากโปรโตคอล UIลักษณะที่ปรากฏ และต้องตั้งค่าโดยตรง ตัวอย่างโค้ดต่อไปนี้แสดงการกําหนดสีพื้นหน้าและสีพื้นหลัง แล้วนําไปใช้กับอินสแตนซ์ควบคุม UI ที่ชื่อ acController
เพิ่มโค้ดนี้ลงในเมธอด onLaunchClicked
ใน ViewController.m
UIColor *darkGray = [UIColor darkGrayColor]; UIColor *lightGray = [UIColor lightGrayColor]; acController.secondaryTextColor = [UIColor colorWithWhite:1.0f alpha:0.5f]; acController.primaryTextColor = lightGray; acController.primaryTextHighlightColor = [UIColor grayColor]; acController.tableCellBackgroundColor = darkGray; acController.tableCellSeparatorColor = lightGray; acController.tintColor = lightGray;
การคาดการณ์สถานที่แบบเป็นโปรแกรม
คุณสามารถสร้าง UI การค้นหาที่กําหนดเองเป็นทางเลือกจาก UI ที่ได้รับจากวิดเจ็ตการเติมข้อความอัตโนมัติ แอปต้องใช้การคาดการณ์สถานที่แบบเป็นโปรแกรม แอปจะรับรายการชื่อสถานที่และ/หรือที่อยู่ที่คาดคะเนได้โดยใช้วิธีใดวิธีหนึ่งต่อไปนี้
กำลังโทรหา GMSPlacesClient findAutocompletePredictionsFromQuery:
หากต้องการดูรายชื่อและ/หรือที่อยู่ของสถานที่ที่คาดการณ์ไว้ ก่อนอื่นให้ระบุ GMSPlacesClient จากนั้นเรียกเมธอด GMSPlacesClient findAutocompletePredictionsFromQuery:
ด้วยพารามิเตอร์ต่อไปนี้
- สตริง
autocompleteQuery
ที่มีข้อความที่ผู้ใช้พิมพ์ GMSAutocompleteSessionToken
ซึ่งใช้เพื่อระบุแต่ละเซสชัน แอปของคุณควรส่งโทเค็นเดียวกันสําหรับการเรียกคําขอการเติมข้อความอัตโนมัติแต่ละครั้ง จากนั้นส่งโทเค็นดังกล่าวพร้อมด้วยรหัสสถานที่ในการเรียกfetchPlacefromPlaceID:
ครั้งถัดไปเพื่อเรียกรายละเอียดสถานที่ของสถานที่ที่ผู้ใช้เลือกGMSAutocompleteFilter
เพื่อดําเนินการต่อไปนี้- การให้น้ําหนักหรือจํากัดผลลัพธ์ไปยังภูมิภาคที่ระบุ
- จํากัดผลลัพธ์ไว้ที่ประเภทสถานที่ที่เจาะจง
- ออบเจ็กต์
GMSPlaceLocationBias
/การจํากัดการให้น้ําหนักผลลัพธ์กับพื้นที่ที่ระบุโดยขอบเขตละติจูดและลองจิจูด
- วิธีเรียกกลับสําหรับจัดการการคาดคะเนที่แสดง
ตัวอย่างโค้ดด้านล่างแสดงการเรียกใช้ findAutocompletePredictionsFromQuery:
Swift
/** * Create a new session token. Be sure to use the same token for calling * findAutocompletePredictions, as well as the subsequent place details request. * This ensures that the user's query and selection are billed as a single session. */ let token = GMSAutocompleteSessionToken.init() // Create a type filter. let filter = GMSAutocompleteFilter() filter.types = [.bank] filter.locationBias = GMSPlaceRectangularLocationOption( northEastBounds, southWestBounds); placesClient?.findAutocompletePredictions(fromQuery: "cheesebu", filter: filter, sessionToken: token, callback: { (results, error) in if let error = error { print("Autocomplete error: \(error)") return } if let results = results { for result in results { print("Result \(result.attributedFullText) with placeID \(result.placeID)") } } })
Objective-C
/** * Create a new session token. Be sure to use the same token for calling * findAutocompletePredictionsFromQuery:, as well as the subsequent place details request. * This ensures that the user's query and selection are billed as a single session. */ GMSAutocompleteSessionToken *token = [[GMSAutocompleteSessionToken alloc] init]; // Create a type filter. GMSAutocompleteFilter *_filter = [[GMSAutocompleteFilter alloc] init]; _filter.types = @[ kGMSPlaceTypeBank ]; [_placesClient findAutocompletePredictionsFromQuery:@"cheesebu" filter:_filter sessionToken:token callback:^(NSArray<GMSAutocompletePrediction *> * _Nullable results, NSError * _Nullable error) { if (error != nil) { NSLog(@"An error occurred %@", [error localizedDescription]); return; } if (results != nil) { for (GMSAutocompletePrediction *result in results) { NSLog(@"Result %@ with PlaceID %@", result.attributedFullText, result.placeID); } } }];
API จะเรียกใช้เมธอดเรียกกลับที่ระบุ และส่งผ่านอาร์เรย์ของออบเจ็กต์ GMSAutocompletePrediction
ออบเจ็กต์ GMSAutocompletePrediction
แต่ละรายการจะมีข้อมูลต่อไปนี้
attributedFullText
– ข้อความทั้งหมดของการคาดคะเนในรูปแบบNSAttributedString
เช่น "Sydney Opera House, Sydney, New South Wales, Australia" ช่วงข้อความทุกช่วงที่ตรงกับอินพุตของผู้ใช้จะมีแอตทริบิวต์kGMSAutocompleteMatchAttribute
คุณใช้แอตทริบิวต์นี้เพื่อไฮไลต์ข้อความที่ตรงกันในคําค้นหาของผู้ใช้ได้ เช่น ดังที่แสดงด้านล่างplaceID
– รหัสสถานที่ของสถานที่ที่คาดการณ์ รหัสสถานที่เป็นตัวระบุแบบข้อความที่ระบุสถานที่ได้อย่างแน่ชัด ดูข้อมูลเพิ่มเติมเกี่ยวกับรหัสสถานที่ได้ที่ภาพรวมของรหัสสถานที่distanceMeters
– ระยะทางในบรรทัดจากorigin
ที่ระบุไว้ไปยังจุดหมาย หากไม่ได้ตั้งค่าพร็อพเพอร์ตี้origin
ไว้ ระบบจะไม่แสดงผลค่าระยะทาง
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีไฮไลต์ข้อความตัวหนาส่วนต่างๆ ของผลการค้นหาที่ตรงกับข้อความในการค้นหาของผู้ใช้โดยใช้ enumerateAttribute
Swift
let regularFont = UIFont.systemFont(ofSize: UIFont.labelFontSize) let boldFont = UIFont.boldSystemFont(ofSize: UIFont.labelFontSize) let bolded = prediction.attributedFullText.mutableCopy() as! NSMutableAttributedString bolded.enumerateAttribute(kGMSAutocompleteMatchAttribute, in: NSMakeRange(0, bolded.length), options: []) { (value, range: NSRange, stop: UnsafeMutablePointer<ObjCBool>) -> Void in let font = (value == nil) ? regularFont : boldFont bolded.addAttribute(NSFontAttributeName, value: font, range: range) } label.attributedText = bolded
Objective-C
UIFont *regularFont = [UIFont systemFontOfSize:[UIFont labelFontSize]]; UIFont *boldFont = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; NSMutableAttributedString *bolded = [prediction.attributedFullText mutableCopy]; [bolded enumerateAttribute:kGMSAutocompleteMatchAttribute inRange:NSMakeRange(0, bolded.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) { UIFont *font = (value == nil) ? regularFont : boldFont; [bolded addAttribute:NSFontAttributeName value:font range:range]; }]; label.attributedText = bolded;
กําลังดึงข้อมูล
หากต้องการสร้างการควบคุมการเติมข้อความอัตโนมัติเองตั้งแต่ต้น ก็ใช้ GMSAutocompleteFetcher
ซึ่งรวมเมธอด autocompleteQuery
ไว้ใน GMSPlacesClient
ได้
โปรแกรมควบคุมจะส่งคําขอคําขอ โดยให้แสดงเฉพาะผลการค้นหาสําหรับข้อความที่ป้อนล่าสุดเท่านั้น และไม่มีองค์ประกอบ UI
หากต้องการใช้ GMSAutocompleteFetcher
ให้ทําตามขั้นตอนต่อไปนี้
- ใช้โปรโตคอล
GMSAutocompleteFetcherDelegate
- สร้างออบเจ็กต์
GMSAutocompleteFetcher
- เรียก
sourceTextHasChanged
ในการดึงข้อมูลเป็นประเภทของผู้ใช้ - จัดการการคาดการณ์และข้อผิดพลาดโดยใช้เมธอดโปรโตคอล
didAutcompleteWithPredictions
และdidFailAutocompleteWithError
ตัวอย่างโค้ดต่อไปนี้สาธิตการใช้การดึงข้อมูลเพื่อป้อนข้อมูลของผู้ใช้ และแสดงการจับคู่สถานที่ในมุมมองข้อความ ยังไม่มีฟังก์ชันสําหรับการเลือกสถานที่ FetcherSampleViewController
มาจาก UIViewController
inFetchSampleViewController.h
Swift
import UIKit import GooglePlaces class ViewController: UIViewController { var textField: UITextField? var resultText: UITextView? var fetcher: GMSAutocompleteFetcher? override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white edgesForExtendedLayout = [] // Set bounds to inner-west Sydney Australia. let neBoundsCorner = CLLocationCoordinate2D(latitude: -33.843366, longitude: 151.134002) let swBoundsCorner = CLLocationCoordinate2D(latitude: -33.875725, longitude: 151.200349) // Set up the autocomplete filter. let filter = GMSAutocompleteFilter() filter.locationRestriction = GMSPlaceRectangularLocationOption(neBoundsCorner, swBoundsCorner) // Create a new session token. let token: GMSAutocompleteSessionToken = GMSAutocompleteSessionToken.init() // Create the fetcher. fetcher = GMSAutocompleteFetcher(bounds: nil, filter: filter) fetcher?.delegate = self fetcher?.provide(token) textField = UITextField(frame: CGRect(x: 5.0, y: 10.0, width: view.bounds.size.width - 5.0, height: 64.0)) textField?.autoresizingMask = .flexibleWidth textField?.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged) let placeholder = NSAttributedString(string: "Type a query...") textField?.attributedPlaceholder = placeholder resultText = UITextView(frame: CGRect(x: 0, y: 65.0, width: view.bounds.size.width, height: view.bounds.size.height - 65.0)) resultText?.backgroundColor = UIColor(white: 0.95, alpha: 1.0) resultText?.text = "No Results" resultText?.isEditable = false self.view.addSubview(textField!) self.view.addSubview(resultText!) } @objc func textFieldDidChange(textField: UITextField) { fetcher?.sourceTextHasChanged(textField.text!) } } extension ViewController: GMSAutocompleteFetcherDelegate { func didAutocomplete(with predictions: [GMSAutocompletePrediction]) { let resultsStr = NSMutableString() for prediction in predictions { resultsStr.appendFormat("\n Primary text: %@\n", prediction.attributedPrimaryText) resultsStr.appendFormat("Place ID: %@\n", prediction.placeID) } resultText?.text = resultsStr as String } func didFailAutocompleteWithError(_ error: Error) { resultText?.text = error.localizedDescription } }
Objective-C
#import "FetcherSampleViewController.h" #import <GooglePlaces/GooglePlaces.h> #import <GoogleMapsBase/GoogleMapsBase.h> @interface FetcherSampleViewController () <GMSAutocompleteFetcherDelegate> @end @implementation FetcherSampleViewController { UITextField *_textField; UITextView *_resultText; GMSAutocompleteFetcher* _fetcher; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.edgesForExtendedLayout = UIRectEdgeNone; // Set bounds to inner-west Sydney Australia. CLLocationCoordinate2D neBoundsCorner = CLLocationCoordinate2DMake(-33.843366, 151.134002); CLLocationCoordinate2D swBoundsCorner = CLLocationCoordinate2DMake(-33.875725, 151.200349); GMSAutocompleteFilter *autocompleteFilter = [[GMSAutocompleteFilter alloc] init]; autocompleteFilter.locationRestriction = GMSPlaceRectangularLocationOption(neBoundsCorner, swBoundsCorner); // Create the fetcher. _fetcher = [[GMSAutocompleteFetcher alloc] initWithBounds:nil filter:filter]; _fetcher.delegate = self; // Set up the UITextField and UITextView. _textField = [[UITextField alloc] initWithFrame:CGRectMake(5.0f, 0, self.view.bounds.size.width - 5.0f, 44.0f)]; _textField.autoresizingMask = UIViewAutoresizingFlexibleWidth; [_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; _resultText =[[UITextView alloc] initWithFrame:CGRectMake(0, 45.0f, self.view.bounds.size.width, self.view.bounds.size.height - 45.0f)]; _resultText.backgroundColor = [UIColor colorWithWhite:0.95f alpha:1.0f]; _resultText.text = @"No Results"; _resultText.editable = NO; [self.view addSubview:_textField]; [self.view addSubview:_resultText]; } - (void)textFieldDidChange:(UITextField *)textField { NSLog(@"%@", textField.text); [_fetcher sourceTextHasChanged:textField.text]; } #pragma mark - GMSAutocompleteFetcherDelegate - (void)didAutocompleteWithPredictions:(NSArray *)predictions { NSMutableString *resultsStr = [NSMutableString string]; for (GMSAutocompletePrediction *prediction in predictions) { [resultsStr appendFormat:@"%@\n", [prediction.attributedPrimaryText string]]; } _resultText.text = resultsStr; } - (void)didFailAutocompleteWithError:(NSError *)error { _resultText.text = [NSString stringWithFormat:@"%@", error.localizedDescription]; } @end
โทเค็นเซสชัน
โทเค็นเซสชันจะจัดกลุ่มข้อความค้นหาและระยะการเลือกของผู้ใช้ที่มีการเติมข้อความอัตโนมัติเป็นเซสชันเดียวเพื่อวัตถุประสงค์ในการเรียกเก็บเงิน เซสชันจะเริ่มต้นขึ้นเมื่อผู้ใช้เริ่มพิมพ์คําค้นหา และสรุปเมื่อเลือกสถานที่ แต่ละเซสชันอาจมีการค้นหาได้หลายรายการ ตามด้วยการเลือกสถานที่ 1 แห่ง เมื่อเซสชันเสร็จสิ้น โทเค็นจะใช้งานไม่ได้อีกต่อไป แอปของคุณต้องสร้างโทเค็นใหม่สําหรับแต่ละเซสชัน เราขอแนะนําให้ใช้โทเค็นเซสชันสําหรับเซสชันการเติมข้อความอัตโนมัติแบบเป็นโปรแกรมทั้งหมด (เมื่อคุณใช้ตัวควบคุมแบบเต็มหน้าจอหรือตัวควบคุมผลลัพธ์ API จะดูแลเรื่องนี้โดยอัตโนมัติ)
Places SDK สําหรับ iOS ใช้ GMSAutocompleteSessionToken
เพื่อระบุแต่ละเซสชัน แอปของคุณควรส่งโทเค็นเซสชันใหม่เมื่อเริ่มต้นเซสชันใหม่แต่ละรายการ แล้วส่งโทเค็นเดียวกันนั้นพร้อมกับรหัสสถานที่ในการเรียก fetchPlacefromPlaceID:
ในภายหลังเพื่อดึงรายละเอียดสถานที่ของสถานที่ที่ผู้ใช้เลือก
ดูข้อมูลเพิ่มเติมเกี่ยวกับโทเค็นเซสชัน
ใช้โค้ดต่อไปนี้เพื่อสร้างโทเค็นเซสชันใหม่
let token: GMSAutocompleteSessionToken = GMSAutocompleteSessionToken.init()
ขีดจำกัดการใช้งาน
- การใช้เมธอด
GMSPlacesClient findAutocompletePredictionsFromQuery
จะขึ้นอยู่กับขีดจํากัดการค้นหาแบบเป็นระดับ โปรดดูเอกสารประกอบเกี่ยวกับขีดจํากัดการใช้งาน
การแสดงการระบุแหล่งที่มาในแอป
- หากแอปใช้บริการเติมข้อความอัตโนมัติแบบเป็นโปรแกรม UI ต้องแสดงการระบุแหล่งที่มา "ขับเคลื่อนโดย Google" หรือปรากฏในแผนที่แบรนด์ Google
- หากแอปใช้ตัวควบคุม UI ที่เติมข้อความอัตโนมัติ ก็ไม่ต้องดําเนินการใดๆ เพิ่มเติม (การระบุแหล่งที่มาที่จําเป็นจะแสดงโดยค่าเริ่มต้น)
- หากคุณดึงข้อมูลและแสดงข้อมูลสถานที่เพิ่มเติมหลังจากรับสถานที่ตามรหัส คุณต้องแสดงการระบุแหล่งที่มาของบุคคลที่สามด้วย
ดูรายละเอียดเพิ่มเติมได้ในเอกสารประกอบเกี่ยวกับการระบุแหล่งที่มา
การควบคุมตัวบ่งชี้กิจกรรมเครือข่าย
หากต้องการควบคุมสัญญาณบอกสถานะกิจกรรมในเครือข่ายในแถบสถานะแอปพลิเคชัน คุณต้องใช้วิธีการมอบสิทธิ์แบบไม่บังคับที่เหมาะกับคลาสการเติมข้อความอัตโนมัติที่คุณใช้ ตลอดจนเปิดและปิดสัญญาณบอกสถานะเครือข่ายด้วยตนเอง
- สําหรับ
GMSAutocompleteViewController
คุณต้องใช้วิธีการมอบสิทธิ์didRequestAutocompletePredictions:
และdidUpdateAutocompletePredictions:
- สําหรับ
GMSAutocompleteResultsViewController
คุณต้องใช้วิธีการมอบสิทธิ์didRequestAutocompletePredictionsForResultsController:
และdidUpdateAutocompletePredictionsForResultsController:
- สําหรับ
GMSAutocompleteTableDataSource
คุณต้องใช้วิธีการมอบสิทธิ์didRequestAutocompletePredictionsForTableDataSource:
และdidUpdateAutocompletePredictionsForTableDataSource:
การใช้วิธีการเหล่านี้และตั้งค่า [UIApplication sharedApplication].networkActivityIndicatorVisible
เป็น YES
และ NO
ตามลําดับจะแถบสถานะตรงกับ UI การเติมข้อความอัตโนมัติอย่างถูกต้อง
จํากัดผลลัพธ์ของการเติมข้อความอัตโนมัติ
คุณตั้งค่าการควบคุม UI แบบเติมข้อความอัตโนมัติเพื่อจํากัดผลการค้นหาไปยังพื้นที่ทางภูมิศาสตร์ที่กําหนด และ/หรือกรองผลลัพธ์ตามประเภทสถานที่หรือประเทศหรือประเทศที่เฉพาะเจาะจงได้ หากต้องการจํากัดผลลัพธ์ คุณสามารถดําเนินการดังต่อไปนี้
- หากต้องการต้องการผลลัพธ์ (การให้น้ําหนักพิเศษ) ภายในภูมิภาคที่กําหนด ให้ตั้งค่า
locationBias
บนGMSAutocompleteFilter
(ผลการค้นหาบางรายการจากนอกภูมิภาคที่กําหนดอาจยังแสดงผลอยู่) หากมีการตั้งค่าlocationRestriction
ด้วย ระบบจะไม่สนใจlocationBias
หากต้องการแสดงเฉพาะผลลัพธ์ (จํากัด) ภายในภูมิภาคที่กําหนด ให้ตั้งค่า
locationRestriction
ในGMSAutocompleteFilter
(ระบบจะแสดงเฉพาะผลลัพธ์ภายในภูมิภาคที่กําหนดเท่านั้น)- หมายเหตุ: ข้อจํากัดนี้มีผลกับเส้นทางทั้งหมดเท่านั้น ส่วนผลลัพธ์สังเคราะห์ที่อยู่นอกขอบเขตสี่เหลี่ยมผืนผ้าอาจแสดงผลตามเส้นทางที่ทับซ้อนกับขีดจํากัดตําแหน่ง
หากต้องการส่งเฉพาะผลการค้นหาที่สอดคล้องกับประเภทสถานที่ที่เฉพาะเจาะจง ให้ตั้งค่า
type
ในGMSAutocompleteFilter
(ตัวอย่างเช่น การระบุ Typefilter.ADDRESS จะทําให้วิดเจ็ตส่งผลลัพธ์ที่มีที่อยู่ที่แน่นอนเท่านั้น)หากต้องการแสดงเฉพาะผลการค้นหาในประเทศที่ระบุได้สูงสุด 5 ประเทศ ให้ตั้งค่า
countries
ในGMSAutocompleteFilter
ขยายผลลัพธ์ไปยังภูมิภาคที่เฉพาะเจาะจง
หากต้องการต้องการผลลัพธ์ (การให้น้ําหนักพิเศษ) ภายในภูมิภาคที่กําหนด ให้ตั้งค่า locationBias
ใน GMSAutocompleteFilter
ตามที่แสดงไว้ที่นี่:
northEast = CLLocationCoordinate2DMake(39.0, -95.0);
southWest = CLLocationCoordinate2DMake(37.5, -100.0);
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.locationBias = GMSPlaceRectangularLocationOption(northEast, southWest);
จํากัดผลการค้นหาเฉพาะภูมิภาค
หากต้องการแสดงเฉพาะผลลัพธ์ (จํากัด) ภายในภูมิภาคที่กําหนด ให้ตั้งค่า locationRestriction
ใน GMSAutocompleteFilter
ตามที่แสดงไว้ที่นี่
northEast = CLLocationCoordinate2DMake(39.0, -95.0);
southWest = CLLocationCoordinate2DMake(37.5, -100.0);
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.locationRestriction = GMSPlaceRectangularLocationOption(northEast, southWest);
กรองผลการค้นหาตามประเทศ
หากต้องการกรองผลการค้นหาภายในไม่เกิน 5 ประเทศ ให้ตั้งค่า countries
ใน GMSAutocompleteFilter
ตามที่แสดงไว้ที่นี่
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.countries = @[ @"au", @"nz" ];
กรองผลลัพธ์ตามประเภทสถานที่หรือประเภทการรวบรวม
จํากัดผลการค้นหาให้เป็นประเภทหรือคอลเล็กชันบางประเภทโดยการตั้งค่าพร็อพเพอร์ตี้ types
ของ GMSAutoCompleteFilter
ใช้พร็อพเพอร์ตี้นี้เพื่อระบุตัวกรองที่แสดงในตาราง 1, 2 และ 3 ในประเภทสถานที่ หากไม่ได้ระบุ ระบบจะแสดงผลทุกประเภท
วิธีระบุประเภทตัวกรองหรือประเภทคอลเล็กชัน
ใช้พร็อพเพอร์ตี้
types
เพื่อระบุประเภทค่าได้สูงสุด 5 ค่าจากตารางที่ 1 และตารางที่ 2 ที่แสดงในประเภทสถานที่ ค่าประเภทจะกําหนดโดยค่าคงที่ในGMSPlaceType
ใช้พร็อพเพอร์ตี้
types
เพื่อระบุการรวบรวมประเภทจากตาราง 3 ที่แสดงในประเภทสถานที่ ค่าการรวบรวมประเภทจะกําหนดโดยค่าคงที่ในGMSPlaceType
คําขอมีเพียงประเภทเดียวจากตาราง 3 ในคําขอ หากคุณระบุค่าจากตาราง 3 คุณจะระบุค่าจากตาราง 1 หรือตาราง 2 ไม่ได้ หากคุณพบข้อผิดพลาด
เช่น หากต้องการให้แสดงเฉพาะผลลัพธ์ที่สอดคล้องกับประเภทสถานที่หนึ่งๆ ให้ตั้งค่า types
ใน GMSAutocompleteFilter
ตัวอย่างต่อไปนี้แสดงการตั้งค่าตัวกรองให้แสดงเฉพาะผลลัพธ์ที่มีที่อยู่ที่แน่นอน
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.types = @[ kGMSPlaceTypeAirport, kGMSPlaceTypeAmusementPark ];
การเพิ่มประสิทธิภาพการเติมข้อความอัตโนมัติของสถานที่
ส่วนนี้จะอธิบายแนวทางปฏิบัติแนะนําในการใช้ประโยชน์สูงสุดจากการเติมข้อความอัตโนมัติของสถานที่
หลักเกณฑ์ทั่วไปมีดังนี้
- วิธีที่รวดเร็วที่สุดในการพัฒนาอินเทอร์เฟซผู้ใช้ที่ใช้งานได้คือ ใช้วิดเจ็ตการเติมข้อความอัตโนมัติของ JavaScript JavaScript API, Places สําหรับการเติมข้อความอัตโนมัติหรือ SDK ของ Places สําหรับ iOS การควบคุม UI เติมข้อความอัตโนมัติ
- พัฒนาความเข้าใจในช่องข้อมูลตําแหน่งอัตโนมัติใน Place ที่สําคัญตั้งแต่ต้น
- ช่องการให้น้ําหนักตําแหน่งและข้อจํากัดด้านสถานที่ตั้งเป็นตัวเลือกที่ไม่บังคับ แต่อาจมีผลกระทบอย่างมากต่อประสิทธิภาพของการเติมข้อความอัตโนมัติ
- ใช้การจัดการข้อผิดพลาดเพื่อให้แน่ใจว่าแอปจะลดประสิทธิภาพลงหาก API แสดงข้อผิดพลาด
- ตรวจสอบว่าแอปของคุณไม่มีตัวเลือกและมีวิธีให้ผู้ใช้ดําเนินการต่อได้
แนวทางปฏิบัติแนะนําสําหรับการเพิ่มประสิทธิภาพต้นทุน
การเพิ่มประสิทธิภาพต้นทุนพื้นฐาน
หากต้องการเพิ่มประสิทธิภาพค่าใช้จ่ายในการใช้บริการการเติมข้อความอัตโนมัติของสถานที่ ให้ใช้มาสก์ช่องในรายละเอียดสถานที่และวิดเจ็ตการเติมข้อความอัตโนมัติเกี่ยวกับสถานที่เพื่อแสดงเฉพาะช่องข้อมูลสถานที่ที่คุณต้องการ
การใช้ต้นทุนขั้นสูง
ลองพิจารณาการใช้การเติมข้อความอัตโนมัติเกี่ยวกับสถานที่เพื่อเข้าถึงการกําหนดราคาต่อคําขอ และขอผลลัพธ์ของ Geocode API เกี่ยวกับสถานที่ที่เลือกแทนรายละเอียดสถานที่ การกําหนดราคาต่อคําขอที่จับคู่กับ Geocode API จะคุ้มค่ากว่าการกําหนดราคาต่อเซสชัน (ตามเซสชัน) หากเป็นไปตามเงื่อนไขทั้งสองต่อไปนี้
- หากคุณต้องการแค่ละติจูด/ลองจิจูดหรือที่อยู่ของสถานที่ที่ผู้ใช้เลือกเท่านั้น Geocode API จะให้ข้อมูลนี้สําหรับการโทรน้อยกว่ารายละเอียดสถานที่
- หากผู้ใช้เลือกการคาดคะเนการเติมข้อความอัตโนมัติโดยเฉลี่ยจากคําขอการคาดคะเนการเติมข้อความอัตโนมัติโดยเฉลี่ยไม่เกิน 4 รายการ การกําหนดราคาต่อคําขออาจประหยัดกว่าการกําหนดราคาต่อเซสชัน
แอปพลิเคชันของคุณต้องการข้อมูลอื่นนอกเหนือจากที่อยู่และละติจูด/ลองจิจูดของการคาดคะเนที่เลือกหรือไม่
ใช่ ต้องการรายละเอียดเพิ่มเติม
ใช้การเติมข้อมูลอัตโนมัติเกี่ยวกับสถานที่ตามเซสชันพร้อมรายละเอียดสถานที่
เนื่องจากแอปพลิเคชันของคุณต้องใช้รายละเอียดสถานที่ เช่น ชื่อสถานที่ สถานะธุรกิจ หรือเวลาทําการ การใช้งานการเติมข้อความอัตโนมัติควรใช้โทเค็นเซสชัน (แบบเป็นโปรแกรม หรือสร้างขึ้นในวิดเจ็ต JavaScript, Android หรือ iOS) โดยมีต้นทุนรวมเท่ากับ $0.017 ต่อเซสชัน บวก SKU ของข้อมูลสถานที่ ขึ้นอยู่กับช่องข้อมูลสถานที่1 }
การนําวิดเจ็ตไปใช้
การจัดการเซสชันจะอยู่ในวิดเจ็ต JavaScript, Android หรือ iOS โดยอัตโนมัติ โดยจะรวมทั้งคําขอการเติมข้อความอัตโนมัติเกี่ยวกับสถานที่และคําขอรายละเอียดสถานที่ในการคาดคะเนที่เลือก อย่าลืมระบุพารามิเตอร์ fields
เพื่อให้แน่ใจว่าคุณขอช่องข้อมูลสถานที่ที่ต้องการเท่านั้น
การใช้งานแบบเป็นโปรแกรม
ใช้โทเค็นเซสชันกับคําขอการเติมข้อความอัตโนมัติเกี่ยวกับสถานที่ เมื่อขอรายละเอียดสถานที่เกี่ยวกับการคาดคะเนที่เลือก ให้ใส่พารามิเตอร์ต่อไปนี้
- รหัสสถานที่จากการตอบกลับการเติมข้อความอัตโนมัติของสถานที่
- โทเค็นเซสชันที่ใช้ในคําขอการเติมข้อความอัตโนมัติเกี่ยวกับสถานที่
- พารามิเตอร์
fields
ที่ระบุช่องข้อมูลสถานที่ที่คุณต้องการ
ไม่ ต้องการเฉพาะที่อยู่และสถานที่
Geoภูมิศาสตร์ API อาจเป็นตัวเลือกที่คุ้มค่ากว่ารายละเอียดสถานที่สําหรับแอปพลิเคชันของคุณ โดยขึ้นอยู่กับประสิทธิภาพการใช้การเติมข้อความอัตโนมัติของสถานที่ ประสิทธิภาพของการเติมข้อความอัตโนมัติของแอปพลิเคชันทั้งหมดจะแตกต่างกันไป ขึ้นอยู่กับสิ่งที่ผู้ใช้ป้อน ตําแหน่งที่มีการใช้งานแอปพลิเคชัน และการใช้งานแนวทางปฏิบัติแนะนําสําหรับการเพิ่มประสิทธิภาพ
ในการตอบคําถามต่อไปนี้ ให้วิเคราะห์จํานวนอักขระของผู้ใช้โดยเฉลี่ยก่อนเลือกการคาดคะเนการเติมข้อความอัตโนมัติของสถานที่ในแอปพลิเคชันของคุณ
โดยเฉลี่ยแล้วผู้ใช้เลือกการคาดคะเนการเติมข้อความอัตโนมัติเกี่ยวกับสถานที่ไว้ในคําขอไม่เกิน 4 รายการใช่ไหม
ได้
ใช้ฟีเจอร์เติมข้อความอัตโนมัติโดยใช้โปรแกรมโดยไม่มีโทเค็นเซสชันและเรียกใช้ Geocode API ในการคาดการณ์สถานที่ที่เลือก
Geoโค้ด API จะส่งที่อยู่และพิกัดละติจูดและลองจิจูดให้ในราคา $0.005 ต่อคําขอ การทําคําขอเติมข้อความอัตโนมัติ - คําขอ 4 ครั้งมีค่าใช้จ่าย $0.01132 ดังนั้นค่าใช้จ่ายรวมของคําขอ 4 รายการบวกกับการเรียก Geocoding API เกี่ยวกับการคาดการณ์สถานที่ที่เลือกจะเท่ากับ 0.30 บาทสําหรับ 300 ต่อเซสชัน
ลองใช้แนวทางปฏิบัติแนะนําด้านประสิทธิภาพเพื่อช่วยให้ผู้ใช้ได้รับการคาดคะเนที่ต้องการได้ง่ายขึ้น
ไม่ได้
ใช้การเติมข้อมูลอัตโนมัติเกี่ยวกับสถานที่ตามเซสชันพร้อมรายละเอียดสถานที่
เนื่องจากจํานวนคําขอเฉลี่ยที่คุณคาดว่าจะเกิดขึ้นก่อนที่ผู้ใช้จะเลือกการคาดคะเนการเติมข้อความอัตโนมัติของสถานที่เกินต้นทุนต่อการกําหนดราคาเซสชัน การใช้งานการเติมข้อความอัตโนมัติของสถานที่จึงควรใช้โทเค็นเซสชันสําหรับทั้งคําขอการเติมข้อความอัตโนมัติของสถานที่และคําขอรายละเอียดสถานที่ที่เกี่ยวข้องรวมเป็นมูลค่า $0.017 ต่อเซสชัน1
การนําวิดเจ็ตไปใช้
การจัดการเซสชันจะอยู่ในวิดเจ็ต JavaScript, Android หรือ iOS โดยอัตโนมัติ โดยจะรวมทั้งคําขอการเติมข้อความอัตโนมัติเกี่ยวกับสถานที่และคําขอรายละเอียดสถานที่ในการคาดคะเนที่เลือก โปรดระบุพารามิเตอร์ fields
เพื่อให้แน่ใจว่าคุณขอเฉพาะช่องข้อมูลพื้นฐาน
การใช้งานแบบเป็นโปรแกรม
ใช้โทเค็นเซสชันกับคําขอการเติมข้อความอัตโนมัติเกี่ยวกับสถานที่ เมื่อขอรายละเอียดสถานที่เกี่ยวกับการคาดคะเนที่เลือก ให้ใส่พารามิเตอร์ต่อไปนี้
- รหัสสถานที่จากการตอบกลับการเติมข้อความอัตโนมัติของสถานที่
- โทเค็นเซสชันที่ใช้ในคําขอการเติมข้อความอัตโนมัติเกี่ยวกับสถานที่
- พารามิเตอร์
fields
ที่ระบุช่องข้อมูลพื้นฐาน เช่น ที่อยู่และเรขาคณิต
พิจารณาความล่าช้าของคําขอเติมข้อความอัตโนมัติของสถานที่
คุณใช้กลยุทธ์ต่างๆ ได้ เช่น เลื่อนคําขอเติมข้อความอัตโนมัติของสถานที่จนกว่าผู้ใช้จะพิมพ์อักขระ 3 หรือ 4 ตัวแรกเพื่อให้แอปพลิเคชันส่งคําขอน้อยลง เช่น การส่งคําขอการเติมข้อความอัตโนมัติของสถานที่สําหรับอักขระแต่ละรายการหลังจากที่ผู้ใช้พิมพ์อักขระตัวที่ 3 หมายถึงหากผู้ใช้พิมพ์อักขระ 7 ตัวเพื่อเลือกการคาดคะเนว่าคุณสร้างคําขอ Geoภูมิศาสตร์ API คําขอ 1 รายการ ค่าใช้จ่ายรวมจะเป็น 0.01632 (4 * 0.00283 การเติมข้อความอัตโนมัติต่อคําขอ + การเข้ารหัสภูมิศาสตร์ 10.00 บาท)1
หากคําขอที่ล่าช้าอาจทําให้คําขอแบบเป็นโปรแกรมโดยเฉลี่ยของคุณต่ํากว่า 4 รายการ โปรดทําตามคําแนะนําสําหรับการติดตั้งใช้งาน การเติมข้อความอัตโนมัติของสถานที่ด้วย Geocode API โปรดทราบว่าคําขอที่ล่าช้าอาจทําให้ผู้ใช้อาจคาดหวังว่าจะเห็นการคาดคะเนด้วยการกดแป้นพิมพ์ใหม่ทุกครั้ง
ลองใช้แนวทางปฏิบัติแนะนําด้านประสิทธิภาพเพื่อช่วยให้ผู้ใช้ได้รับการคาดคะเนที่ต้องการได้ง่ายขึ้น
-
ค่าใช้จ่ายที่แสดงที่นี่เป็นสกุลเงิน USD โปรดดูข้อมูลทั้งหมดในหน้าการเรียกเก็บเงินของ Google Maps Platform
แนวทางปฏิบัติแนะนําด้านประสิทธิภาพ
หลักเกณฑ์ต่อไปนี้อธิบายวิธีเพิ่มประสิทธิภาพการเติมข้อความอัตโนมัติของสถานที่
- เพิ่มข้อจํากัดในแต่ละประเทศ การให้น้ําหนักตําแหน่ง และ (สําหรับการติดตั้งใช้งานแบบเป็นโปรแกรม) ลงในการติดตั้งใช้งานการเติมข้อความอัตโนมัติเกี่ยวกับสถานที่ ค่ากําหนดภาษาไม่จําเป็นสําหรับวิดเจ็ตเนื่องจากเลือกค่ากําหนดภาษาจากเบราว์เซอร์หรืออุปกรณ์เคลื่อนที่ของผู้ใช้
- หากการเติมข้อความอัตโนมัติของสถานที่มาพร้อมกับแผนที่ คุณสามารถจัดตําแหน่งการให้น้ําหนักพิเศษตามวิวพอร์ตในแผนที่ได้
- ในสถานการณ์ที่ผู้ใช้ไม่เลือกการคาดคะเนการเติมข้อความอัตโนมัติแบบใดแบบหนึ่ง โดยทั่วไปเนื่องจากไม่มีการคาดคะเนที่เป็นที่อยู่ผลลัพธ์ที่ต้องการ คุณอาจใช้อินพุตของผู้ใช้เดิมซ้ําเพื่อแสดงผลลัพธ์ที่เกี่ยวข้องมากขึ้นได้
- หากคุณคาดหวังให้ผู้ใช้ป้อนเฉพาะข้อมูลที่อยู่ ให้ใช้อินพุตเดิมของผู้ใช้อีกครั้งในการเรียก Geocoding API
- หากคุณคาดว่าผู้ใช้จะป้อนการค้นหาสําหรับสถานที่บางแห่งตามชื่อหรือที่อยู่ ให้ใช้ค้นหาสถานที่ หากคาดว่าจะพบผลลัพธ์ในบางภูมิภาคเท่านั้น ให้ใช้การให้น้ําหนักตําแหน่ง
- ผู้ใช้ที่ป้อนที่อยู่ย่อยในประเทศที่การรองรับการเติมข้อความอัตโนมัติใน Place นั้นไม่สมบูรณ์ เช่น เช็กเกีย เอสโตเนีย และลิทัวเนีย เช่น ที่อยู่ภาษาเช็ก "Stroupežnického 3191/17, Praha" ให้การคาดคะเนบางส่วนในการเติมข้อความอัตโนมัติของสถานที่
- ผู้ใช้ป้อนที่อยู่ที่มีคํานําหน้าส่วนของถนน เช่น "23-30 29th St, Queens" ในนิวยอร์กซิตี้ หรือ "47-380 Kamehameha Hwy, Kaneohe" บนเกาะคาไวในฮาวาอิ
การแก้ปัญหา
แม้ว่าข้อผิดพลาดที่หลากหลายอาจเกิดขึ้นได้ แต่ข้อผิดพลาดส่วนใหญ่ที่แอปมักพบเกิดจากข้อผิดพลาดในการกําหนดค่า (เช่น มีการใช้คีย์ API ที่ไม่ถูกต้อง หรือกําหนดค่าคีย์ API ไม่ถูกต้อง) หรือข้อผิดพลาดเกี่ยวกับโควต้า (แอปเกินโควต้า) ดูข้อมูลเพิ่มเติมเกี่ยวกับโควต้าได้ที่ขีดจํากัดการใช้งาน
ข้อผิดพลาดที่เกิดขึ้นโดยใช้การควบคุมการเติมข้อความอัตโนมัติจะปรากฏในเมธอด didFailAutocompleteWithError()
ของโปรโตคอลที่ได้รับมอบอํานาจที่หลากหลาย พร็อพเพอร์ตี้ code
ของออบเจ็กต์ NSError
ที่ระบุได้รับการตั้งค่าให้เป็นค่าการแจกแจง GMSPlacesErrorCode