如何移动MKMapView的中心,使默认位置图标转到底部中心

时间:2016-07-19 06:32:11

标签: ios objective-c mkmapview mapkit mkannotationview

我在stackOverflow上找到了与此相关的一些问题,例如this onethisthis one。最后一个实际工作但问题是我在heading中使用MapKit我的意思是(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading。因此,当我应用此解决方案时,问题就出现了:

MKCoordinateRegion oldRegion = [mapView regionThatFits:MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 800.0f, 200.0f)];
    CLLocationCoordinate2D centerPointOfOldRegion = oldRegion.center;

    //Create a new center point (I added a quarter of oldRegion's latitudinal span)
    CLLocationCoordinate2D centerPointOfNewRegion = CLLocationCoordinate2DMake(centerPointOfOldRegion.latitude + oldRegion.span.latitudeDelta/5.0, centerPointOfOldRegion.longitude);

    //Create a new region with the new center point (same span as oldRegion)
    MKCoordinateRegion newRegion = MKCoordinateRegionMake(centerPointOfNewRegion, oldRegion.span);

    //Set the mapView's region
    [_mapView setRegion:newRegion animated:NO];

它成功更新但是当我移动手机并且访问了accelometer时,它在newRegion和oldRegion之间启动了一个抽搐效果。有什么想法吗?

UPDATE
我发现这个方法让我改变了userLocation annotation的默认图像,这个属性我是可以像myAnnotaton.center那样设置,这样任何想法可能会有效或如何设置其中心?

- (MKAnnotationView *)mapView:(MKMapView *)mapView
            viewForAnnotation:(id<MKAnnotation>)annotation {


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

        myAnotation.image = [UIImage imageNamed:@"marker"];
        myAnotation.frame = CGRectMake(0, 0, 100, 100);

        return myAnotation;
    }
    else
    {
    return nil;
    }
}

1 个答案:

答案 0 :(得分:1)

您需要添加一个覆盖默认注释的委托方法。

- (MKAnnotationView *)mapView:(MKMapView *)mapView
            viewForAnnotation:(id<MKAnnotation>)annotation {


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

        myAnotation.image = self.userPointImage.image; //give any image here
        myAnotation.centerOffset = CGPointMake(0, 250);

        return myAnotation;
    }
    else
    {
    return nil;
    }
}

centerOffset会将注释设置为底部,地图将相应地旋转