2012-10-22 3 views
0

iOS 프로그래밍을 처음 사용합니다. 내 요구 사항은 Google 지역 정보 자동 완성 요청을 UITextField으로 설정하는 것입니다. 내가iOS의 UITextField에 Google 지역 정보 자동 완성 요청을 설정하는 방법

NSString *urlPath = [NSString stringWithFormat:@"/maps/api/place/autocomplete/json?input=%@&types=geocode&language=en-EN&sensor=false",string]; 
NSURL *url = [[NSURL alloc]initWithScheme:@"http" host:@"maps.googleapis.com" path:urlPath]; 
NSLog(@"url is:::%@",url); 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; 
[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 

NSURLResponse *response ; 
NSError *error; 
NSData *data; 
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSLog(@"data is:::%@",data); 

NSMutableDictionary *jsonDict= (NSMutableDictionary*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
NSLog(@"jsonDict is:%@",jsonDict); 

아래와 같은 코드를 작성하지만이 비어 있습니다. 내가 잘못한 곳을 말해 줄 수 있니?

답변

0

을 제어 스위프트 코드 아래를 시도 할 것이다 : -

import UIKit 
import GooglePlaces 

class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 



@IBOutlet weak var txtField: UITextField! 
@IBOutlet weak var tableView: UITableView! 

var tableData = [GMSAutocompletePrediction]() 

var fetcher: GMSAutocompleteFetcher? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.view.backgroundColor = UIColor.white 
    self.edgesForExtendedLayout = [] 

    let nsBoundsCorner = CLLocationCoordinate2D(latitude: 20.5937, longitude: 78.9629) 

    let bounds = GMSCoordinateBounds(coordinate: nsBoundsCorner, coordinate: nsBoundsCorner) 

    let filter = GMSAutocompleteFilter() 
    filter.type = .establishment 

    fetcher = GMSAutocompleteFetcher(bounds: bounds, filter: filter) 
    fetcher?.delegate = self 

    txtField?.addTarget(self, action: #selector(textFieldDidChange(textField:)),for: .editingChanged) 

    tableView.delegate = self 
    tableView.dataSource = self 

    self.tableView.reloadData() 


    // Do any additional setup after loading the view. 
} 

@objc func textFieldDidChange(textField: UITextField) { 
     fetcher?.sourceTextHasChanged(txtField.text!) 
    } 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return tableData.count 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    var section = indexPath.section 
    var row = indexPath.row 

    let cell1 : UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell1") 

    cell1.selectionStyle = UITableViewCellSelectionStyle.none 
    cell1.backgroundColor = UIColor.clear 
    cell1.contentView.backgroundColor = UIColor.clear 
    cell1.textLabel?.textAlignment = NSTextAlignment.left 
    cell1.textLabel?.textColor = UIColor.black 
    cell1.textLabel?.font = UIFont.systemFont(ofSize: 14.0) 

    cell1.textLabel?.text = tableData[indexPath.row].attributedFullText.string 
    return cell1 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    txtField.text = tableData[indexPath.row].attributedFullText.string 
    getLatLongFromAutocompletePrediction(prediction:tableData[indexPath.row]) 
} 

func getLatLongFromAutocompletePrediction(prediction:GMSAutocompletePrediction){ 

    let placeClient = GMSPlacesClient() 

    placeClient.lookUpPlaceID(prediction.placeID!) { (place, error) -> Void in 
     if let error = error { 
      //show error 
      return 
     } 

     if let place = place { 
      place.coordinate.longitude //longitude 
      place.coordinate.latitude //latitude 
     } else { 
     //show error 
     } 
    } 
} 
} 

extension TableViewController: GMSAutocompleteFetcherDelegate { 


func didAutocomplete(with predictions: [GMSAutocompletePrediction]) { 

    tableData.removeAll() 

    for prediction in predictions{ 

     tableData.append(prediction) 

    } 
    tableView.reloadData() 
} 

func didFailAutocompleteWithError(_ error: Error) { 
    print(error.localizedDescription) 
    } 
} 
관련 문제