gpt4 book ai didi

ios - 如何使用MapKit在Swift 3中更改图钉颜色

转载 作者:行者123 更新时间:2023-11-28 10:15:44 24 4
gpt4 key购买 nike

下面是我的代码,列出了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)

}


}

最佳答案

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

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

然后将颜色指定给批注:
 MKPinAnnotationView.bluePinColor()

关于ios - 如何使用MapKit在Swift 3中更改图钉颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41785265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com