更改 mapView 注释的图像和颜色
Changing the image and color of a mapView annotation
我该如何更改此代码,以便我可以为受尊敬的组实现颜色和独特的图像?现在,当我调用这些函数时,它们都会产生相同的通用红色注释。我希望这些组具有不同的图像和颜色,而不是多个组共享相同的注释。我该如何更改?
这是我的代码。
import UIKit
import MapKit
struct PlacesOnMap {
var name: String
var latitude: Double
var longitude: Double
init(name: String, latitude: Double, longitude: Double) {
self.name = name
self.latitude = latitude
self.longitude = longitude
}
}
class MapViewController: UIViewController {
@IBOutlet var mapView: MKMapView!
var placesFirst = [PlacesOnMap(name: "place 1", latitude: 28.551700, longitude: -81.374800),
PlacesOnMap(name: "place 2", latitude: 28.553018, longitude: -81.374206),
PlacesOnMap(name: "place 3", latitude: 28.553019, longitude: -81.367839)]
var placesSecond = [PlacesOnMap(name: "place 1", latitude: 28.556969, longitude: -81.364319),
var placesThird = [PlacesOnMap(name: "place 1", latitude: 28.54693, longitude: -81.393071)]
PlacesOnMap(name: "place 2", latitude: 28.538523, longitude: -81.385399)]
override func viewDidLoad() {
super.viewDidLoad()
}
let markerView = MKMarkerAnnotationView()
func setupFirstPlacesAnnotations() {
let places = placesFirst.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView.addAnnotations(places)
}
func setupSecondPlacesAnnotations() {
let places = placesSecond.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView.addAnnotations(places)
}
func setupThirdPlacesAnnotations() {
let places = placesThird.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView.addAnnotations(places)
}
extension MapViewController: CLLocationManagerDelegate, MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
}
}
您可以按照本教程下载源代码,您会看到它将解决如何更改标记颜色和图像。
https://www.raywenderlich.com/7738344-mapkit-tutorial-getting-started
我该如何更改此代码,以便我可以为受尊敬的组实现颜色和独特的图像?现在,当我调用这些函数时,它们都会产生相同的通用红色注释。我希望这些组具有不同的图像和颜色,而不是多个组共享相同的注释。我该如何更改?
这是我的代码。
import UIKit
import MapKit
struct PlacesOnMap {
var name: String
var latitude: Double
var longitude: Double
init(name: String, latitude: Double, longitude: Double) {
self.name = name
self.latitude = latitude
self.longitude = longitude
}
}
class MapViewController: UIViewController {
@IBOutlet var mapView: MKMapView!
var placesFirst = [PlacesOnMap(name: "place 1", latitude: 28.551700, longitude: -81.374800),
PlacesOnMap(name: "place 2", latitude: 28.553018, longitude: -81.374206),
PlacesOnMap(name: "place 3", latitude: 28.553019, longitude: -81.367839)]
var placesSecond = [PlacesOnMap(name: "place 1", latitude: 28.556969, longitude: -81.364319),
var placesThird = [PlacesOnMap(name: "place 1", latitude: 28.54693, longitude: -81.393071)]
PlacesOnMap(name: "place 2", latitude: 28.538523, longitude: -81.385399)]
override func viewDidLoad() {
super.viewDidLoad()
}
let markerView = MKMarkerAnnotationView()
func setupFirstPlacesAnnotations() {
let places = placesFirst.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView.addAnnotations(places)
}
func setupSecondPlacesAnnotations() {
let places = placesSecond.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView.addAnnotations(places)
}
func setupThirdPlacesAnnotations() {
let places = placesThird.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView.addAnnotations(places)
}
extension MapViewController: CLLocationManagerDelegate, MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
}
}
您可以按照本教程下载源代码,您会看到它将解决如何更改标记颜色和图像。
https://www.raywenderlich.com/7738344-mapkit-tutorial-getting-started