2016-10-04 26 views
0

藉助一些在線幫助,我成功地在iOS應用中實現了一些Google地圖功能。Swift:Google Map API Camera和MapView

extension GoogleMapViewController: GMSAutocompleteResultsViewControllerDelegate { 
    func resultsController(resultsController: GMSAutocompleteResultsViewController, 
         didAutocompleteWithPlace place: GMSPlace) { 
     searchController?.active = false 
     // Do something with the selected place. 
     print("Place name: ", place.name) 
     print("Place address: ", place.formattedAddress) 
     print("Place attributions: ", place.attributions) 

     // Create a GMSCameraPosition that tells the map to display the 
     // coordinate -33.86,151.20 at zoom level 6. 
     let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6.0) 
     let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera) 
     mapView.myLocationEnabled = true 
     view = mapView 

     // Creates a marker in the center of the map. 
     let marker = GMSMarker() 
     marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20) 
     marker.title = place.formattedAddress 
     marker.map = mapView 
    } 
} 

我相信我已經得到了搜索欄的place.formattedAddress但如何取回其座標,這樣我可以設置相機和標記,以表明搜索到的地方嗎?

回答

1

根據他們的documentation,你可以使用place.coordinate

let currentPlace: CLLocationCoordinate2D = place.coordinate 

然後你就可以得到它的緯度和經度currentPlace.latitudecurrentPlace.longitude分別

這裏該地點的documentation

+0

我試圖 「mapView.camera = GMSCameraPosition(目標:place.coordinate,縮放:15,軸承:0,viewingAngle:0)」,但它不工作。 – user6702783

+0

'place.coordinate'返回一個'CLLocationCoordinate2D'。我已更新我的答案以反映 – eshirima

+0

非常感謝。它現在工作! – user6702783

0

我解決了這條路。

func resultsController(resultsController: GMSAutocompleteResultsViewController, 
         didAutocompleteWithPlace place: GMSPlace) { 
    searchController?.active = false 
    // Do something with the selected place. 
    print("Place name: ", place.name) 
    print("Place address: ", place.formattedAddress) 
    print("Place attributions: ", place.attributions) 

    // Create a GMSCameraPosition that tells the map to display the 

    let camera = GMSCameraPosition.cameraWithTarget(place.coordinate, zoom: 15, bearing: 0, viewingAngle: 0) 
    let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera) 
    mapView.myLocationEnabled = true 
    view = mapView 

    // Creates a marker in the center of the map. 
    let marker = GMSMarker() 
    marker.position = place.coordinate 
    marker.title = place.formattedAddress 
    marker.map = mapView 
} 
1
To set the camera position at particular place , first we have to declare a object of CLLocationCoordinate2D and then get the latitude and longitude of that particular place . 

var currentPlace:CLLocationCoordinate2D = place.coordinate 

Now you are able to get the latitude and longitude by writing currentPlace.latitude and currentPlace.longitude . 

Since you have lat/lon , you can easily set the camera position on that place . 

let camera = GMSCameraPosition.cameraWithLatitude(currentPlace.latitude, longitude: currentPlace.longitude, zoom: 6.0) 

let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera) 
    mapView.myLocationEnabled = true 
    view = mapView