Change Pin Image on Mkmapview in Swift

Change pin image on MKMapView in Swift

Don't forget to set:

map.delegate = self

And make sure your UIViewController implements the MKMapViewDelegate protocol.

If you forget to do this, your implementation of mapView:viewForAnnotation: won't be invoked for your map.

Besides, it looks like pinView!.animatesDrop = true breaks custom images. You'd have to set it to false, or use MKAnnotationView (which doesn't have an animatesDrop property).

See this related question if you want to implement a custom drop animation.

How to change selected pin image in Map Kit in Swift 3?

"selected" means "tapped"? If so, Try the following code:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
view.image = UIImage(named: "marker_gray")
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
view.image = UIImage(named: "marker_red")
}

How to set custom pin image in Mapview swift ios?

  mapview.delegate = self

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else {
return nil
}

let annotationIdentifier = "Identifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}

if let annotationView = annotationView {

annotationView.canShowCallout = true
annotationView.image = UIImage(named: "yourImagename”)
}
return annotationView
}

How to set DIFFERENT image pins in different locations SWIFT

You can add an image property to your AnnotationPin class and then in viewForAnnotation you use a conditional downcast to see if you are dealing with one of your annotations. If you are then you can use the image property

class AnnotationPin:  NSObject, MKAnnotation {
let coordinate: CLLocationCoordinate2D
let title: String?
let subtitle: String?
let image: UIImage?

init(title:String, subtitle: String, image: UIImage, coordinate: CLLocationCoordinate2D) {
self.title = title
self.subtitle = subtitle
self.coordinate = coordinate
self.image = image
super.init()
}}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

guard let myAnnotation = annotation as? AnnotationPin else {
return nil
}

let annotationIdentifier = "AnnotationIdentifier"

var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}

if let annotationView = annotationView {

annotationView.canShowCallout = true
annotationView.image = myAnnotation.image
}

return annotationView
}

How to change non selected annotation pin images on mapkit with Swift

Conform to MKMapViewDelegate protocol and then:

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
let selectedAnnotation = view.annotation
for annotation in mapView.annotations {
if let annotation = annotation as? MKAnnotation where !annotation.isEqual(selectedAnnotation) {
// do some actions on non-selected annotations in 'annotation' var
}
}

Also you can save the selected annotation for later use here, if you want to process all annotations in another moment.

Custom pin image in annotationView in iOS

The accepted answer doesn't work, as it has annotationView uninitialized in else block.

Here's a better solution. It dequeues annotation view if possible or creates a new one if not:

// https://stackoverflow.com/a/38159048/1321917
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !(annotation is MKUserLocation) else {
return nil
}

// Better to make this class property
let annotationIdentifier = "AnnotationIdentifier"

var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}

if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "yourImage")
}

return annotationView
}

Swift - MKPointAnnotation custom pin image

The way I usually do it is to create a new swift file that will be your custom annotation which inherits from MKAnnoatation. Example below

import MapKit

class MyAnnotation: NSObject, MKAnnotation {
let title: String?
let subtitle: String?
let coordinate: CLLocationCoordinate2D
var image: UIImage? = nil

init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D) {
self.title = title
self.subtitle = subtitle
self.coordinate = coordinate
//self.image
super.init()
}
}

You will need to initialise your annotation where the CLCoordinate is compulsory. Then set the image property with your custom image. MyAnnotation.image = "myImage.png". You will then need to add your annotation to your map viewmapView.addAnnotations(MyAnnotation).
I also implement the method below from the MKMapViewDelegate (Make sure your inherit this in your class). This is so that the user can tap on the annotation and receive information about it. Hope this helps.

In your view controller:

let marker = MyAnnotation(title: "title" as! String, subtitle: "subtitle" as! String, coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
marker.image = UIImage("my image.png")
self.mapView.addAnnotations(marker)

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? MyAnnotation {
let identifier = "identifier"
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.image = annotation.image //add this
annotationView?.canShowCallout = true
annotationView?.calloutOffset = CGPoint(x: -5, y: 5)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView
return annotationView
}
return nil
}

Cant set Custom Pin Image In MapView, Swift 3

Your issue is that you´re missing add your UIViewController as delegate of your MKMapView so you need to add this line in your viewDidLoad as I said in my comments

self.ProfileMapView.delegate = self

I also recommend you to use the extension pattern to make your code more readable

extension YourViewController : MKMapViewDelegate{

func mapView(_ ProfileMapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

if annotation.isMember(of: MKUserLocation.self) {
return nil
}

let reuseId = "ProfilePinView"

var pinView = ProfileMapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)}
pinView!.canShowCallout = true
pinView!.image = UIImage(named: "CustomPinImage")

return pinView

}
}

Change Pin Image of MKMapView on specific zoom In-Out iOS Swift 4

Step 1. First of all you need to get information which pin/annotation display on you map.

Objective c

-(void)getAnotationsInVisibleMapRectangle
{
NSSet *annotationSet = [myMapView annotationsInMapRect:myMapView.annotationVisibleRect];
NSArray *annotationArray = [annotationSet allObjects];
}

Swift

extension MKMapView {
func visibleAnnotations() -> [MKAnnotation] {
return self.annotationsInMapRect(self.visibleMapRect).map { obj -> MKAnnotation in return obj as! MKAnnotation }
}
}

Step 2. You will have delegate method will call on zoom in/out regionDidChangeAnimated. just call the as above function. And update the Your pin.

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
print("call on zoom and zoom out ")
let aryData = mapView.visibleAnnotations()
print(aryData[0].coordinate)
//remove pin from this coordinate
//And add new pin as you want
}

Here you can just find the pin. And remove old pin and add new pin as you want.



Related Topics



Leave a reply



Submit