2016-10-04 11 views
0

一部のオンラインヘルプで、私はiOSアプリケーションでいくつかのGoogleマップ機能を正常に実装しました。Swift:Google Map APIカメラと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

I "はmapView.camera = GMSCameraPosition(ターゲット:place.coordinate、ズーム:15、ベアリング:0、視野角: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