更改mapView批注的图像和颜色

问题描述 投票:0回答:1

我将如何更改此代码,以便为受尊敬的人群提供颜色和独特的图像?现在,当我调用这些函数时,它们都会产生相同的通用红色注释。我希望这些组具有不同的图像和颜色,而不是多个组共享相同的注释。我将如何更改?

这是我的代码。

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? {

}
}
ios swift xcode mkmapview
1个回答
1
投票

您可以遵循本教程并下载源代码,可以看到它将解决如何更改标记颜色和图像。

https://www.raywenderlich.com/7738344-mapkit-tutorial-getting-started

© www.soinside.com 2019 - 2024. All rights reserved.