Need to add a Fixed Overlay like on mapview in IOS

拜拜、爱过 提交于 2019-11-28 06:49:36

问题


I need to create a map view interface, which is something similar to the OLA Cabs Application in iOS. What I exactly wanna do is to fix an overlay on mapView and allow the user to scroll the map view across it. So that the overlay can be fixed at any location the User wants it to, I searched a lot about overlays, in iOS and MapKit, but couldn't make it possible. If some one can give me tips for achieving this I would be really grateful. Here is a snapshot of the screen

Here the annotation remains fixed and you can move the map view across it, So that when you stop the mapview, the overlay will be pointing to the new location, where you stopped


回答1:


Click here to download demo...

Create a fix MKAnnotation and image view object to animating the location change effect in Map view.

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

Add this code in 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 have to remove image when you drag a map view and add image which looks like an annotation. And after completion of that add annotation and remove image from 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];
}



回答2:


Its not an map annotation overlay, its a normal UIImageView which has been placed over MKMapView, and it always used to get the lat-long for the center point of the map.

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

@Kampai has added the same code for you.



来源:https://stackoverflow.com/questions/27394656/need-to-add-a-fixed-overlay-like-on-mapview-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!