【问题标题】:how to change a pin color in swift 3 using MapKit如何使用 MapKit 在 swift 3 中更改引脚颜色
【发布时间】:2017-01-21 22:33:18
【问题描述】:

下面是我列出 1 个注释引脚的代码。现在是红色的。我怎样才能让它变成蓝色?我需要放入扩展文件吗?该代码还包括当前用户的位置。我也可以将注释设为按钮吗?

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet var map: MKMapView!

let manager = CLLocationManager()


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]
    let span: MKCoordinateSpan = MKCoordinateSpanMake(1.0, 1.0)
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)

    map.setRegion(region, animated: true)

    print(location.altitude)
    print(location.speed)

    self.map.showsUserLocation = true

}




override func viewDidLoad() {
    super.viewDidLoad()

    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()

    let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(37.5656, -122.443)
    let annoation = MKPointAnnotation()

           annoation.coordinate = location
    annoation.title = "MYSHOP"
    annoation.subtitle = "Come Visit"
    map.addAnnotation(annoation)

}


}

【问题讨论】:

    标签: ios swift annotations swift3 mapkit


    【解决方案1】:

    我看到这个问题已经有一段时间了,但我记得我在创建自定义别针颜色时遇到了麻烦,所以也许将来有人可以使用它。 MKPinAnnotationColor 已被弃用,因此如果您想要不是红色、绿色或紫色的引脚,则需要创建自己的颜色。为此,您可以创建 MKPinAnnotationView 的扩展并定义新颜色。例如,如果你想要一个蓝色别针:

     extension MKPinAnnotationView {
         class func bluePinColor() -> UIColor {
             return UIColor.blue
         }
     }
    

    然后将颜色分配给您的注释:

     MKPinAnnotationView.bluePinColor()
    

    【讨论】:

      【解决方案2】:

      你需要设置一些对象作为地图的委托,然后实现viewForAnnotation。您可以使用 MKPinAnnotationView 返回标准引脚注释,或者创建自定义视图并在需要特殊内容时返回。

      应该有大量的教程来展示如何做到这一点,因为它是非常简单的地图定制。

      快速的 Google 搜索发现了这个 SO 帖子:

      How to change MKAnnotation Color using Swift?

      请注意,该问题的答案使用 pinColor,它在 iOS 10 中已被弃用。您需要改用 MKPinAnnotationColor

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多