3

My Current MKMapView is:

But when I zoom in I want to change Pin Image to this:

enter image description here

and again zoom out change to default violet circle.

I just want to change Pin Image when user zoom In/Out MKMapView in iOS Swift 4. Thanks In Advance! Happy Coding!

Sachin Dobariya
  • 744
  • 7
  • 16

1 Answers1

1

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.

kalpesh
  • 1,285
  • 1
  • 17
  • 30