iOS GMSMapView更改初始加载背景颜色

时间:2018-03-26 21:47:29

标签: ios swift google-maps swift4 google-maps-sdk-ios

我发现这个question可能是完全相同的问题,但接受的答案对我不起作用。

我们目前正在使用Google Maps API加载地图以覆盖整个视图。最初的GMSMapView背景颜色类似于棕褐色,RGB(237,234,226),至少在xcode的视图层次结构中,但我们的应用程序在其他任何地方都有黑色背景,所以我们在谷歌地图加载我们的自定义黑暗之前得到一个棕褐色的闪烁样式。

有谁知道如何更改初始棕褐色背景颜色?

我们在viewDidLoad(下面的代码)中加载自定义样式,但我们假设初始背景不会受此影响。

do {
  if let styleURL = Bundle.main.url(forResource: "GoogleMapsStyle", withExtension: "json") {
    mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
  } else {
    NSLog("Unable to find style.json")
  }
} catch {
  NSLog("One or more of the map styles failed to load. \(error)")
}

*可能有用的其他信息:

整页地图视图是Pulley视图的主要部分。

通过点击表格视图单元格的一部分来触发整页地图视图,该表格单元格加载PulleyView以及其中的两个容器视图(主视图和抽屉),主视图或完整mapview和抽屉/“滑轮”其中包含搜索栏和结果。

在此点按功能期间,它会从表格视图中访问用户的地址,并在主滑轮容器视图中设置地图的相机并填充抽屉滑轮容器视图的搜索栏。

guard let pulleyVC = UIStoryboard(name: "EditAddress", bundle: nil).instantiateViewController(withIdentifier: "scheduleAddress") as? PulleyViewController else {
    log.error(formatLog("profile", "EditAddress failed to instantiate from storyboard"))
    return
}
pulleyVC.loadViewIfNeeded()
pulleyVC.addNavBar()
pulleyVC.setNavBarTitle(title: NSLocalizedString("editAddress", comment: ""))

guard let primaryContentVC = pulleyVC.primaryContentViewController as? PrimaryContentViewController else {
    log.error(formatLog("profile", "EditAddress failed to instantiate from storyboard"))
    return
}
if let placemark = userSelectedPlacemark {
    primaryContentVC.setMapLocation(delegate: self, placemark)
}
primaryContentViewController = primaryContentVC

guard let drawerContentVC = pulleyVC.drawerContentViewController as? DrawerContentViewController else {
    log.error(formatLog("profile", "EditAddress failed to instantiate from storyboard"))
    return
}
if let address = userSelectedPlacemark?.name {
    drawerContentVC.setMapPostalAddress(delegate: self, address, usersLocation: locationManager.location)
}
navController.pushViewController(pulleyVC, animated: true)

自定义json样式在视图控制器中的viewDidLoad函数期间加载,用于PulleyView控制器的主视图控制器。

主要内容视图控制器的setMapLocation函数是我们分配mapview的地方。大多数此功能实际上是设置标记。 mapview是此控制器@IBOutlet var mapView: GMSMapView!中的全局变量。

func setMapLocation(delegate: PrimaryContentViewControllerDelegate?, _ placemark: CLPlacemark?) {
    if let delegate = delegate {
        self.delegate = delegate
    }

    guard let coordinate = placemark?.location?.coordinate else {
        log.warning(formatLog("map", "location not available"))
        return
    }
    savedPlacemark = placemark
    mapView.clear()

    let camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude: coordinate.longitude, zoom: 15.0)
    mapView.animate(to: camera)

    // REFACTOR THIS (ALSO IN MAPTABBLEVIEWCELL)
    let marker = GMSMarker(position: camera.target)
    let markerImage = UIImage(named: "marker")
    let markerView = UIImageView(image: markerImage)
    marker.iconView = markerView
    marker.map = mapView
    marker.groundAnchor = CGPoint(x: 0.5, y: 0.5)

    // TODO: get this working and reusable (i.e. an extension?)
    let pulseMarker = GMSMarker(position: camera.target)

    let pulseRingImg = UIImageView(image: UIImage(named: "pulse"))
    pulseRingImg.isUserInteractionEnabled = false
    CATransaction.begin()
    CATransaction.setAnimationDuration(3.5)

    //transform scale animation
    var theAnimation: CABasicAnimation?
    theAnimation = CABasicAnimation(keyPath: "transform.scale.xy")
    theAnimation?.repeatCount = Float.infinity
    theAnimation?.autoreverses = false
    theAnimation?.fromValue = Float(0.5)
    theAnimation?.toValue = Float(1.0)
    theAnimation?.isRemovedOnCompletion = false

    pulseRingImg.layer.add(theAnimation!, forKey: "pulse")
    pulseRingImg.isUserInteractionEnabled = false
    CATransaction.setCompletionBlock {() -> Void in
        //alpha Animation for the image
        let animation = CAKeyframeAnimation(keyPath: "opacity")
        animation.duration = 3.5
        animation.repeatCount = Float.infinity
        animation.values = [Float(2.0), Float(0.5)]
        pulseMarker.iconView?.layer.add(animation, forKey: "opacity")
    }
    CATransaction.commit()
    pulseMarker.iconView = pulseRingImg
    pulseMarker.layer.addSublayer(pulseRingImg.layer)
    pulseMarker.map = mapView
    pulseMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
}

0 个答案:

没有答案