在UIView中显示谷歌地图

时间:2017-06-29 23:57:09

标签: swift3 xcode8

我在iOS上使用谷歌地图。

这是我的代码:

class ViewController: UIViewController {
    @IBOutlet weak var myMapView: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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
    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: +31.75097946, longitude: +35.23694368)
    marker.title = "my location"
    marker.map = mapView
}

如何将地图附加到myMapView UIView? 有没有办法让标记标题始终出现?

感谢

2 个答案:

答案 0 :(得分:2)

要在UIView中放置一个谷歌地图,你需要将UIView对象拖到画布上并将其子类化为GMSMapView,然后在代码中创建一个插座 - 然后你可以使用以下代码初始化它: / p>

   @IBOutlet weak var miniview: GMSMapView!

   override func viewDidLoad() {
        super.viewDidLoad()
        let camera = GMSCameraPosition.camera(withLatitude: +31.75097946, longitude: +35.23694368, zoom: 6.0)
        miniview.camera = camera
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: +31.75097946, longitude: +35.23694368)
        marker.title = "my location"
        marker.map = miniview
    }

答案 1 :(得分:1)

您只需使用自己创建的插座即可。

class ViewController: UIViewController {
    @IBOutlet weak var myMapView: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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

        // CHANGE THIS
        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 = "my location"
        marker.map = mapView
    }