2017-06-30 160 views
0

我在iOS上使用谷歌地圖。在UIView內正確顯示谷歌地圖

這是我的代碼:

import UIKit 
import GoogleMaps 

ViewController: UIViewController { 

@IBOutlet weak var myMapView: GMSMapView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 


override func viewDidAppear(_ animated: Bool) { 
    // Create a GMSCameraPosition that tells the map to display the 
    // coordinate -33.86,151.20 at zoom level 6. 
    let camera = GMSCameraPosition.camera(withLatitude: +31.75097946, longitude: +35.23694368, zoom: 17.0) 
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) 
    //mapView.isMyLocationEnabled = true 
    mapView.mapType = .terrain 



    self.view = mapView 
    //self.myMapView = mapView 



    // Creates a marker in the center of the map. 
    let marker = GMSMarker() 
    marker.position = CLLocationCoordinate2D(latitude: +31.75097946, longitude: +35.23694368) 
    marker.title = "Eden VidanPeled" 

    //marker.snippet = "Australia" 
    marker.map = mapView 
    marker.opacity = 1.0 
    } 

在Interface Builder中我有類GMSMapView中一個UIView。當我將地圖附加到UIViewController時,我得到了我的經緯度座標。當我嘗試將地圖加入UIView時,我會看到不同的地圖,縮放級別等,如下圖所示。

the maps

如何獲得UIView的地圖正確描繪? p.s.我同時嘗試了viewDidLoad和viewDidAppear。 感謝

+0

像使用addsubview函數一樣將mapview添加到您的視圖中 –

回答

1

您已經創建了一個IBOutletGMSMapView

這將創建一個地圖實例爲您服務。所以不需要再創建一個GMSMapView的實例並將其分配給類變量。

override func viewDidAppear(_ animated: Bool) { 
    // Create a GMSCameraPosition that tells the map to display the 
    // coordinate -33.86,151.20 at zoom level 6. 

    let camera = GMSCameraPosition.camera(withLatitude: +31.75097946, longitude: +35.23694368, zoom: 17.0) 

    self.myMapView.mapType = .terrain 

    self.myMapView.camera = camera 


    // Creates a marker in the center of the map. 
    let marker = GMSMarker() 
    marker.position = CLLocationCoordinate2D(latitude: +31.75097946, longitude: +35.23694368) 
    marker.title = "Eden VidanPeled" 

    //marker.snippet = "Australia" 
    marker.map = self.myMapView 
    marker.opacity = 1.0 
}