viewForAnnotation didn't moves the userLocatio

2019-09-15 09:47发布

问题:

I've implemented - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { method to change the default location of userLocation Annotation. Inside this method I've used:

if (annotation==(mapView.userLocation)) {
    MKAnnotationView *myAnotation = [[MKAnnotationView alloc] init];

    myAnotation.image = self.userPointImage.image; 
    myAnotation.centerOffset = CGPointMake(0, 250);

    return myAnotation;
}
else
{
    return nil;
}

Which successfully moves the default userLocation annotation to center bottom. But as I'm also using heading so when move my phone it is still rotating the map from center.

- (void)locationManager:(CLLocationManager *) manager didUpdateHeading:(CLHeading *) newHeading {

    [self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:true];
}

But the userLocation annotation is below. So how to do that?

UPDATE

I found this question and tried applying it. But as I'm using heading so it crashes the app. In my didUpdateUserLocation method I added [self moveCenterByOffset:CGPointMake(0, 200) from:userLocation.coordinate]; and this method is as follows:

- (CLLocationCoordinate2D)moveCenterByOffset:(CGPoint)offset from:(CLLocationCoordinate2D)coordinate
{
    CGPoint point = [self.mapView convertCoordinate:coordinate toPointToView:self.mapView];
    point.x += offset.x;
    point.y += offset.y;
    CLLocationCoordinate2D center = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    return center;
}

and then in my didUpdateUserLocation method I updated it as: MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([self moveCenterByOffset:CGPointMake(0, -250) from:userLocation.coordinate], 800.0f, 200.0f); and [mapView setRegion:region animated:NO]; as I'm using heading so animation is set to NO. But now as I run the code it starts jerking between center and the new point.

回答1:

MKCoordinateRegion region = mapClinicsView.region;

set 'region.center' It will helps you to navigate your map center position

make sure 'region.center' and annotation center latitute and longitute is same