viewForAnnotation 没有移动 userLocation [英] viewForAnnotation didn't moves the userLocation

查看:18
本文介绍了viewForAnnotation 没有移动 userLocation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation { 方法来更改 userLocation Annotation 的默认位置.在我使用的这个方法中:

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;
}

它成功地将默认的 userLocation 注释移动到中心底部.但由于我也在使用航向,所以当我移动手机时,它仍在从中心旋转地图.

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];
}

但是 userLocation 注释在下面.那么怎么做呢?

更新

我发现 this 问题并尝试应用它.但是当我使用标题时,它会导致应用程序崩溃.在我的 didUpdateUserLocation 方法中,我添加了 [self moveCenterByOffset:CGPointMake(0, 200) from:userLocation.coordinate]; ,这个方法如下:

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;
}

然后在我的 didUpdateUserLocation 方法中,我将其更新为:MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([self moveCenterByOffset:CGPointMake(0, -250) from:userLocation.coordinate], 800.0f,200.0f);[mapView setRegion:region animation:NO]; 因为我正在使用标题,所以动画设置为 NO.但是现在当我运行代码时,它开始在中心点和新点之间抖动.

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.

推荐答案

MKCoordinateRegion region = mapClinicsView.region;

MKCoordinateRegion region = mapClinicsView.region;

设置'region.center'它将帮助您导航地图中心位置

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

确保'region.center'和注释中心纬度和经度相同

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

这篇关于viewForAnnotation 没有移动 userLocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆