MapKit始终以用户位置为中心

时间:2013-10-25 21:19:40

标签: ios map geolocation

我有一张地图可以在加载时适当缩放到当前位置,但是如果没有立即缩放到用户位置,则不允许您在地图上平移。我一直在玩跟踪模式,但没有得到正确的解决方案..这是一些代码,我很感激帮助。

    - (void)viewDidLoad
{
    [super viewDidLoad];
    contentArray = [[NSMutableArray alloc] init];
    mapViewuno.delegate = self;
    mapViewuno.mapType = MKMapTypeStandard;
    mapViewuno.userInteractionEnabled=YES;
    locationManager = [[CLLocationManager alloc] init];
    // Do any additional setup after loading the view, typically from a nib.
}
- (BOOL)prefersStatusBarHidden {
    return YES;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    indexValue = 0;
    NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"mapAddress" ofType:@"plist"];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSString *strID = [NSString stringWithFormat:@"%d",intID];
    NSLog(@"array : %@",[dict objectForKey:strID]);
    [contentArray removeAllObjects];
    [contentArray addObjectsFromArray:[dict objectForKey:strID]];
    [contentArray retain];
    [self zoomToUserLocation:mapViewuno.userLocation];
}
- (void)zoomToUserLocation:(MKUserLocation *)userLocation
{
    if (!userLocation)
        return;

    MKCoordinateRegion region;
    region.center = userLocation.location.coordinate;
    region.span = MKCoordinateSpanMake(.5, .5);
    region = [mapViewuno regionThatFits:region];
    [mapViewuno setRegion:region animated:YES];
    counter = 0;

    [mapViewuno removeAnnotations:mapViewuno.annotations];

    if([contentArray count] != 0)
    {
        for(indexValue = 0; indexValue<[contentArray count];indexValue++)
        {
            FirstAnnotation *obj=[[FirstAnnotation alloc]init];

            obj.latitude = [[[contentArray objectAtIndex:indexValue] objectForKey:@"lattitude"] floatValue];
            obj.longitude = [[[contentArray objectAtIndex:indexValue] objectForKey:@"Longitude"] floatValue];

            obj.titleName=[[contentArray objectAtIndex:indexValue] objectForKey:@"Title"];

            obj.Address = [[contentArray objectAtIndex:indexValue] objectForKey:@"Address"];

            obj.Phone = [[contentArray objectAtIndex:indexValue] objectForKey:@"Phone"];

            obj.intTag = indexValue;
            [mapViewuno addAnnotation:obj];
        }
        if ([mapViewuno.annotations count] == 0) return;

                //        [self.mapView setRegion:newRegion animated:YES];
    }

}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [self zoomToUserLocation:userLocation];
}

1 个答案:

答案 0 :(得分:2)

这部分导致问题:

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [self zoomToUserLocation:userLocation];
}

每次mapview的位置管理器获得新的修复(即每隔一秒左右)时,mapview会调用该委托方法,该方法又会调用您的方法进行缩放。加入BOOL zoomedToUserLocation并在初始时设置为NO,然后在第一次点击YES时设置为zoomToUserLocation