首页 > 解决方案 > Google Maps Mapview 不以标记为中心

问题描述

我在 ios 中使用带有 swift 4 的谷歌地图,我必须在屏幕上显示一个带有标记的地图视图,问题是每当我显示地图视图时,我都会得到这个结果:

在此处输入图像描述

我想要得到的结果是这样的:

在此处输入图像描述

这是我正在使用的代码:

 func loadMap(location:CLLocation){
    self.mapView.isMyLocationEnabled = true
    self.mapView.isUserInteractionEnabled = true
    self.view.layoutIfNeeded()
    self.addLocationMarker()
}

func addLocationMarker(){
    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    print(self.startLocation)
    marker.position = CLLocationCoordinate2D(latitude: self.startLocation.coordinate.latitude, longitude: self.startLocation.coordinate.longitude)
    marker.map = self.mapView

    let camera = GMSCameraPosition.camera(withLatitude: self.startLocation.coordinate.latitude, longitude: self.startLocation.coordinate.longitude, zoom: 12.0)
    self.mapView.camera = camera
}

mapview 是情节提要中视图的出口

标签: iosswift4google-maps-sdk-ios

解决方案


这段代码对我来说很好。试试这个(Swift 4)

func setupMapView() {

    DispatchQueue.main.async {
      let position = CLLocationCoordinate2D(latitude: 32.9483364, longitude: -96.8241543)
      let marker = GMSMarker(position: position)
      self.mapView.camera = GMSCameraPosition(target: position, zoom: 15, bearing: 0, viewingAngle: 0)
      marker.title = "Enter marker title here"
      marker.map = self.mapView
    }  
}

注意:我使用了样本经度和纬度。你应该插入你的。


推荐阅读