2016-06-14 1 views
4

IBOutlet으로 연결된 하위보기 인 경우 Google지도 자체를 중앙에 놓고 마커를 추가하는 데 문제가 있습니다. 나는 그것이 20 가지의 다른 방법들처럼 느껴지도록 노력했지만, 붙어있어서 어떤 도움이나지도라도 감사하게 느낄 것이다. 다음과 같이Google지도 SDK로 중심점 추가 및 마커 추가 Subview (Swift)

는 코드 설치는 매우 간단합니다 :

//Map view outlet 
@IBOutlet weak var googleMapView: GMSMapView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

// Google Map View Setup 
    let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6) 
    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera) 
    mapView.myLocationEnabled = true 

    self.googleMapView = mapView 

    let marker = GMSMarker() 
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20) 
    marker.title = "Sydney" 
    marker.snippet = "Australia" 
    marker.map = mapView 

내가보기 self.view하는 self.googleMapView을 변경하여 전체보기 컨트롤러 인 경우이 작업을 얻을 수 있습니다,하지만 난 알아낼 수 없습니다 googleMapView 콘센트에 제대로 연결하는 방법을 설명합니다. 감사!

similar question에 대한 도움말을 사용해 보았습니다.하지만 콘센트와 변수가 모두 mapView이고 제대로 작동하지 않아 명확하게 답변되지 않았습니다.

답변

8

물론 내가 여기 모든 사람에게 질문을 한 후에 나는 주현절을 가지고 그것을 알아 냈습니다. Autolayout으로 이미 완료되었으므로 새 GMSMapView를 만들 필요가 없습니다. 위치를 설정하기 위해 GMSMapView Outlet의 카메라 변수에 액세스해야했습니다. 아래

코드 :

// Google Map View Setup 
    let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6) 
    self.googleMapView.myLocationEnabled = true 

    self.googleMapView.camera = camera 

    let marker = GMSMarker() 
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20) 
    marker.title = "Sydney" 
    marker.snippet = "Australia" 
    marker.map = googleMapView 
+0

나는 똑같은 문제를 겪고 있었고, 빛을 보았습니다. +1 –

1

나는 당신의지도보기 경계가있는 viewDidLoad에서보기 콘센트() 함수를 제대로 설정하지 않는 생각합니다. 그래서 mapView 경계를 func viewDidAppear (animated : Bool)에서 설정할 수 있습니다. Google지도가 self.view를 적절하게 중심에두고 사용자 정의보기 경계를 중심으로하지 않는 동일한 문제에 직면했습니다. func viewDidLoad에서 그 이유를 발견했습니다.(), 우리는 뷰의 사각형 경계를 올바르게 얻을 수 없습니다.

override func viewDidAppear(animated: Bool) { 
     super.viewDidAppear(animated) 
     googleMapsView = GMSMapView() 
     googleMapsView?.frame = mapView.bounds 
     googleMapsView?.myLocationEnabled = true 
     if let googleMapView = googleMapsView { 
      mapView.addSubview(googleMapView) 
     } 
    }