为什么标记未显示在GMSMapView中?

时间:2019-03-25 12:05:43

标签: ios swift google-maps gmsmapview

这是我的代码:

let camera = GMSCameraPosition.camera(withLatitude: 51.5, longitude: -0.127, zoom: 12.0)

var mainView:UIView = GMSMapView.init(frame: CGRect(x:50,y:150,width:250,height:300))
self.view.addSubview(mainView)
let mapView = GMSMapView.map(withFrame: mainView.frame, camera: camera)
mainView = mapView

//Marker Position
let position = CLLocationCoordinate2D(latitude: 51.5, longitude: -0.127)
let london = GMSMarker(position: position)
london.title = "London"
london.icon = UIImage(named: "house")
london.map = mapView

1 个答案:

答案 0 :(得分:0)

替换

var mainView:UIView = GMSMapView.init(frame: CGRect(x:50,y:150,width:250,height:300))
 self.view.addSubview(mainView)
let mapView = GMSMapView.map(withFrame: mainView.frame, camera: camera)
mainView = mapView

使用

var mainView:GMSMapView! // instance var

 // in viewDidLoad
 let camera = GMSCameraPosition.camera(withLatitude: 51.5, longitude: -0.127, zoom: 12.0)
 mainView = GMSMapView.map(withFrame:CGRect(x:50,y:150,width:250,height:300), camera: camera) 
 mapView.delegate = self
 self.view.addSubview(mainView)