2017-08-24 42 views

回答

3

第一。然後你參考下面的代碼。

斯威夫特

在AppDelegate中

import UIKit 
import CoreData 
import GoogleMaps 
import GooglePlacePicker 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 

func application(_ application: UIApplication, didFinishLaunchingWithOptions 
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    func setUpGoogleMaps() 
    return true 
} 

func setUpGoogleMaps() { 
    let googleMapsApiKey = "AIzaSyA-kiOBvrR9CNztqutwmKaSLyXIid93K0E" 
    GMSServices.provideAPIKey(googleMapsApiKey) 
    GMSPlacesClient.provideAPIKey("AIzaSyA-kiOBvrR9CNztqutwmKaSLyXIid93K0E") 
} 

在你的ViewController這是具有文本框

import UIKit 
import GooglePlaces 
import GoogleMaps 
import GooglePlacePicker 

class HotelVC: UIViewController, GMSMapViewDelegate, UITextFieldDelegate { 

@IBOutlet weak var YourTextField: UITextField! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.YourTextField.delegate = self 

} 

func textFieldDidBeginEditing(_ textField: UITextField) { 
    let acController = GMSAutocompleteViewController() 
    acController.delegate = self 
    self.present(acController, animated: true, completion: nil) 
    } 
} 


extension viewController: GMSAutocompleteViewControllerDelegate { 

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) { 

    print("Place name: \(place.name)") 
    print("Place address: \(place.formattedAddress ?? "null")") 
    self.YourTextField.text = place.formattedAddress 
    print("Place attributions: \(String(describing: place.attributions))") 

    self.dismiss(animated: true, completion: nil) 
} 
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) { 
    // TODO: handle the error. 
    //  print("Error: \(error.description)") 
    self.dismiss(animated: true, completion: nil) 
} 

// User canceled the operation. 
func wasCancelled(_ viewController: GMSAutocompleteViewController) { 
    print("Autocomplete was cancelled.") 
    self.dismiss(animated: true, completion: nil) 
    } 
} 
+0

感謝兄弟爲我工作忘了投票@格雷戈裏威爾遜Pullyattu – anuj

0

您可以使用Google Places API。它可以幫助您輕鬆查找地點,也可以通過您的textField激活它。

以下是你需要在你的項目安裝GooglePlaces SDK鏈接https://developers.google.com/places/ios-api/autocomplete

+0

ü可以給我提供@kkakkurt – anuj

+0

樣本還我想,他們的是搜索結果的極限@kkakkurt – anuj

+0

幫我解決這個@kkakkurt – anuj