如何在swift中添加位置图标?

问题描述:

我该如何将这些东西添加到我的项目中。它们是否集成在MKMapView如何在swift中添加位置图标?

我没有找到关于他们的互联网什么。

Screenshot

+1

再见地图 – Ved

+0

好创建的UIView,但我怎么能安装该系统的部位图标使用外部图像白化? (对不起,我的英语) –

+0

有没有像屏幕右上角的“我”圈 –

  1. 上创建MKMapView的顶部的两个按钮添加则它们的约束在viewDidLayourSubviews功能编写此代码舍入所希望的角部和阴影和图像添加到按钮。从身份检查员中选择他们的浅灰色背景颜色。

    override func viewDidLayoutSubviews() { 
    
    let infoButtonShape = CAShapeLayer() 
    infoButtonShape.bounds = infoButton.frame 
    infoButtonShape.position = infoButton.center 
    
    let locationButtonShape = CAShapeLayer() 
    locationButtonShape.bounds = locationButton.frame 
    locationButtonShape.position = locationButton.center 
    
    
    infoButton.clipsToBounds = true 
    infoButton.layer.masksToBounds = false 
    infoButton.layer.shadowColor = UIColor.white.cgColor 
    infoButton.layer.shadowRadius = 2 
    infoButton.layer.shadowOpacity = 0.5 
    infoButton.layer.shadowOffset = CGSize(width: 2, height: -2) 
    infoButton.layer.shadowPath = UIBezierPath(rect: infoButton.bounds).cgPath 
    
    
    
    locationButton.clipsToBounds = true 
    locationButton.layer.masksToBounds = false 
    locationButton.layer.shadowColor = UIColor.white.cgColor 
    locationButton.layer.shadowRadius = 2 
    locationButton.layer.shadowOpacity = 0.5 
    locationButton.layer.shadowOffset = CGSize(width: -2, height: 2) 
    locationButton.layer.shadowPath = UIBezierPath(rect: locationButton.bounds).cgPath 
    
    infoButton.layer.shouldRasterize = true 
    locationButton.layer.shouldRasterize = true 
    
    locationButtonShape.path = UIBezierPath(roundedRect: locationButton.bounds, byRoundingCorners: [UIRectCorner.bottomLeft , UIRectCorner.bottomRight], cornerRadii: CGSize(width:10.0, height:10.0)).cgPath 
    
    infoButtonShape.path = UIBezierPath(roundedRect: infoButton.bounds, byRoundingCorners: [UIRectCorner.topRight , UIRectCorner.topLeft], cornerRadii: CGSize(width:10.0, height:10.0)).cgPath 
    
    infoButton.layer.mask = infoButtonShape 
    locationButton.layer.mask = locationButtonShape 
    
    infoButton.contentMode = .scaleAspectFit 
    locationButton.contentMode = .scaleAspectFit 
    
    
    infoButton.setImage(#imageLiteral(resourceName: "information-icon-3"), for: .normal) 
    locationButton.setImage(#imageLiteral(resourceName: "LocationArrow-512"), for: .normal) 
    
    
    } 
    
  2. 然后添加按钮的IBAction更新位置

    let manager = CLLocationManager() 
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    
    //Use location.last add to map then stop updating location 
    manager.stopUpdatingLocation() 
    
    } 
    
    @IBAction func updateLocationAction(_ sender: UIButton) { 
    
    manager.startUpdatingLocation() 
    } 
    
  3. 对于两个其它UIViews只是创建一个UILabel用于温度以及用于云图标一个的UIImageView。对于UILabel和UIImageView的背景颜色及其圆角半径,您可以做类似于我为按钮所做的操作,但是这次为UIImageView和UILabel的左角选择了正确的角点。

这里是输出:当然,您可以使用更好的大小的图标图像为您的按钮。

enter image description here

+0

很棒,谢谢。明天我会试试它! –

子视图在地图视图的顶部。我是一个iOS资源。的UIButton - >型 - >详细披露