You need to add a fixed invoice, like on a map in iOS

I need to create a map display interface, which is similar to the OLA Cabs app in iOS. What I definitely want to do is fix the overlay on the mapView and allow the user to scroll the map display on it. So that the overlay could be fixed in any place that the user wants, I searched a lot about overlays, in iOS and MapKit, but could not make it possible. If someone can give me advice to achieve this, I would be very grateful. Here is a screenshot

enter image description here

Here, the annotation remains fixed, and you can move it around the map, so when you stop displaying the map, the overlay will point to the new location where you left off

+1
source share
2 answers

Click here to download a demo ...

enter image description here

Create the MKAnnotation fix and image viewer to animate the effect of the location change in the map view.

 @property (nonatomic, strong) CustomAnnotation *fixAnnotation; @property (nonatomic, strong) UIImageView *annotationImage; 

Add this code to the viewDidLoad() method:

  // Fix annotation _fixAnnotation = [[CustomAnnotation alloc] initWithTitle:@"Fix annotation" subTitle:@"Location" detailURL:nil location:self.mapView.userLocation.coordinate]; [self.mapView addAnnotation:self.fixAnnotation]; // Annotation image. CGFloat width = 64; CGFloat height = 64; CGFloat margiX = self.mapView.center.x - (width / 2); CGFloat margiY = self.mapView.center.y - (height / 2) - 32; // 32 is half size for navigationbar and status bar height to set exact location for image. _annotationImage = [[UIImageView alloc] initWithFrame:CGRectMake(margiX, margiY, width, height)]; [self.annotationImage setImage:[UIImage imageNamed:@"mapannotation.png"]]; 

Now you need to delete the image when you drag the map view and add an image that looks like an annotation. And after completing this, add an annotation and remove the image from the map view.

 - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated { NSLog(@"Region will changed..."); [self.mapView removeAnnotation:self.fixAnnotation]; [self.mapView addSubview:self.annotationImage]; } - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { NSLog(@"Region did changed..."); [self.annotationImage removeFromSuperview]; CLLocationCoordinate2D centre = [mapView centerCoordinate]; self.fixAnnotation.coordinate = centre; [self.mapView addAnnotation:self.fixAnnotation]; } 
+6
source

This is not an overlay of annotations on the map, its regular UIImageView , which was placed on top of MKMapView , and it always used to get the lat-long for the center point of the map.

Hope this will be an easy way to achieve your goal.

@Kampai added the same code for you.

+3
source

Source: https://habr.com/ru/post/1212166/


All Articles