【问题标题】:setup google maps as view将谷歌地图设置为视图
【发布时间】:2020-09-02 19:11:12
【问题描述】:

完成设置谷歌地图后,我添加了三个子视图 UIButton 它是可点击的'不输入任何不可编辑的内容 所以当我评论谷歌地图代码时,一切正常,但我在 UIView 中的 textField 将允许编辑工作正常

import UIKit
import GoogleMaps

class GoogleMap: UIViewController, UITextFieldDelegate {

    let locationBtn: UIButton = {
        let button = UIButton(type: .system)
        button.addTarget(self, action: #selector(orderBtnAction), for: UIControl.Event.touchUpInside)
        button.setTitle("إستخدام هذا الموقع", for: UIControl.State.normal)
        button.titleLabel?.font =  UIFont.systemFont(ofSize: 15)
        button.tintColor = UIColor.white
        button.layer.cornerRadius = 10
        button.backgroundColor = UIColor.delevareColor
        button.translatesAutoresizingMaskIntoConstraints = false
       return button
    }()

    let locationView: UIView = {
        let view = UIView()
        view.layer.cornerRadius = 15
        view.backgroundColor = UIColor.white
        view.layer.shadowPath = UIBezierPath(rect: view.bounds).cgPath
        view.layer.shadowRadius = 5
        view.layer.shadowOffset = .zero
        view.layer.shadowOpacity = 1
        view.clipsToBounds = true
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    let currentLocTextField: UITextField = {
        let textField = UITextField()
        textField.text = "جاري تحميل موقعك..."
        textField.font = UIFont.boldSystemFont(ofSize: 19)
        textField.textColor = UIColor.black
        textField.borderStyle = .roundedRect
        textField.isUserInteractionEnabled = true
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.textAlignment = .center
        return textField
    }()

 var locationManager = CLLocationManager()
    var mapView: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        currentLocTextField.delegate = self
        locationView.addSubview(currentLocTextField)
        setUpNavigationController()
        setUpGoogleMap()
        setUpLayout()

    }

func setUpLayout() {

        locationBtn.heightAnchor.constraint(equalToConstant: 50).isActive = true
        locationBtn.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        locationBtn.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -25).isActive = true
        locationBtn.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10).isActive = true
        locationBtn.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true

        locationView.heightAnchor.constraint(equalToConstant: 120).isActive = true
         locationView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        locationView.bottomAnchor.constraint(equalTo: locationBtn.topAnchor, constant: -10).isActive = true
        locationView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10).isActive = true
        locationView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true

        currentLocTextField.heightAnchor.constraint(equalToConstant: 30).isActive = true
        currentLocTextField.centerXAnchor.constraint(equalTo: locationView.centerXAnchor).isActive = true
        currentLocTextField.topAnchor.constraint(equalTo: locationView.topAnchor, constant: 20).isActive = true
        currentLocTextField.leftAnchor.constraint(equalTo: locationView.leftAnchor, constant: 20).isActive = true
        currentLocTextField.rightAnchor.constraint(equalTo: locationView.rightAnchor, constant: -50).isActive = true

        }

    func setUpGoogleMap(){

        let camera = GMSCameraPosition.camera(withLatitude: 15.592778, longitude: 32.552278, zoom: 12)
        mapView = GMSMapView.map(withFrame: .zero, camera: camera)
        view = mapView
        mapView.animate(to: camera)
        mapView.isMyLocationEnabled = true
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        mapView.delegate = self
        mapView.addSubview(locationBtn)
        mapView.addSubview(locationView)

    }

}

extension GoogleMap: CLLocationManagerDelegate {
  func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

    guard status == .authorizedWhenInUse else {

      return
    }

    locationManager.startUpdatingLocation()


    mapView.isMyLocationEnabled = true
    mapView.settings.myLocationButton = true
  }


  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.first else {
      return
    }

    // 7
    mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

    // 8
    locationManager.stopUpdatingLocation()
  }
}
extension GoogleMap: GMSMapViewDelegate {

    func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
     return
    }
  func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
    if #available(iOS 11.0, *) {
        reverseGeocodeCoordinate(position.target)
    } else {
        // Fallback on earlier versions
    }
  }
}

【问题讨论】:

  • 我告诉你,你需要在你的视图中添加地图视图......而不是将其设置为视图
  • 作为子视图怎么做?
  • 是 .. 将其添加为子视图 ...
  • 你试过了吗..我相信这会解决你的问题
  • 我正在尝试找出代码

标签: swift xcode google-maps layout


【解决方案1】:

改变你的设置方法

func setUpGoogleMap(){


            let camera = GMSCameraPosition.camera(withLatitude: 15.592778, longitude: 32.552278, zoom: 12)
        mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
        view .addSubview( mapView)
            mapView.animate(to: camera)
            mapView.isMyLocationEnabled = true
            locationManager.delegate = self
            locationManager.requestWhenInUseAuthorization()
            mapView.delegate = self
    //        mapView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(locationBtn)
        self.view.addSubview(locationView)

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 2016-05-06
    相关资源
    最近更新 更多