簡體   English   中英

如何為谷歌地圖中心按鈕分配明暗模式?

[英]How to assign light and dark mode to Google maps recenter button?

我正在使用 Google Maps SDK 在我的應用程序中嵌入地圖。 我還使用以下方法啟用了地圖中心圖標:

extension DashboardVC: GMSMapViewDelegate {

func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
    if !change{
        googleMapView.settings.myLocationButton = true
        ColorLocationButton()
    }
        change = false
}
func didTapMyLocationButton(for mapView: GMSMapView) -> Bool {
    googleMapView.settings.myLocationButton = false
    change = true
    return false
}
func mapView(_ mapView: GMSMapView, didTapMyLocation location: CLLocationCoordinate2D) {
    googleMapView.settings.myLocationButton = false
 }
}

如何從谷歌地圖本身為這個按鈕渲染添加明暗模式?

首先添加一個視圖並在視圖中添加圖像

然后在您的控制器上制作插座

@IBOutlet weak var recenterView: UIView!
@IBOutlet weak var hamburgerImage: UIImageView!

如果您在 Google 地圖視圖上的視圖必須在您的視圖中添加這些代碼行將出現

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    googleMapView.settings.myLocationButton = false
    self.googleMapView.bringSubviewToFront(self.recenterView)
    recenterView.isUserInteractionEnabled = true
    let tapOnRecenter = UITapGestureRecognizer(target: self, action: #selector(recenterTheMap(gesture:)))
    tapOnRecenter.delegate = self
    recenterView.addGestureRecognizer(tapOnRecenter)
    // Setup The Recenter View For Corner Radius and Shadow
     recenterView.clipsToBounds = true
    recenterView.layer.cornerRadius = recenterView.frame.size.width / 2
    recenterView.layer.shadowColor  = UIColor(ciColor: .gray).cgColor
    recenterView.layer.shadowRadius = 12 
     }

您的中心按鈕代碼

 @objc func recenterTheMap(gesture: UITapGestureRecognizer){
   
  googleMapView.camera = GMSCameraPosition.camera(withTarget: marker.position, zoom: 15)
     
  }

不要忘記添加手勢代表

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

通過此委托方法檢測 Light 或 DarkMode

   override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)
    
    if #available(iOS 13.0, *) {
        if self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
            if traitCollection.userInterfaceStyle == .light {
              
            recenterView.tintColor = .white
            
            }
            else {
                
                recenterView.tintColor = .black
            }
        }
    } else {
        // Fallback on earlier versions
    }
}

這是您當前位置的代碼

   func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
   location: CLLocation = locations.last!
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM