如何移动 MKMapView 的中心,以便默认位置图标转到底部中心
How to move the center of MKMapView so that the default location icon goes to bottom center
我在 Whosebug 上发现了一些与此相关的问题,例如 this one, this and this one 此处。最后一个确实有效,但问题是我在 MapKit
中使用 heading
我的意思是这个 (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];
它成功更新,但是当我移动我的 phone 并且访问加速度计时,它开始在 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;
}
}
您需要添加一个委托方法来覆盖您的默认注释。
- (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 会将注释设置到底部,地图将相应地旋转
我在 Whosebug 上发现了一些与此相关的问题,例如 this one, this and this one 此处。最后一个确实有效,但问题是我在 MapKit
中使用 heading
我的意思是这个 (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];
它成功更新,但是当我移动我的 phone 并且访问加速度计时,它开始在 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;
}
}
您需要添加一个委托方法来覆盖您的默认注释。
- (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 会将注释设置到底部,地图将相应地旋转