Ios MapKit始终以用户位置为中心

Ios MapKit始终以用户位置为中心,ios,map,geolocation,Ios,Map,Geolocation,我有一个地图,可以在加载时适当地缩放到当前位置,但如果不立即将其放大到用户位置,则不允许在地图周围平移。我一直在玩跟踪模式,但没有得到正确的修复。。这里是一些代码,我感谢你的帮助 - (void)viewDidLoad { [super viewDidLoad]; contentArray = [[NSMutableArray alloc] init]; mapViewuno.delegate = self; mapViewuno.mapType = MK

我有一个地图,可以在加载时适当地缩放到当前位置,但如果不立即将其放大到用户位置,则不允许在地图周围平移。我一直在玩跟踪模式,但没有得到正确的修复。。这里是一些代码,我感谢你的帮助

    - (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];
}
-(void)viewDidLoad
{
[超级视图下载];
contentArray=[[NSMutableArray alloc]init];
mapViewuno.delegate=self;
mapViewuno.mapType=MKMapTypeStandard;
mapViewuno.userInteractionEnabled=YES;
locationManager=[[CLLocationManager alloc]init];
//加载视图后,通常从nib执行任何其他设置。
}
-(BOOL)更喜欢StatusBarHidden{
返回YES;
}
-(无效)视图将显示:(BOOL)动画
{
[超级视图将显示:动画];
self.navigationController.navigationBarHidden=是;
indexValue=0;
NSString*plistPath=[[NSBundle mainBundle]pathForResource:@“plist”类型的“mapAddress”;
NSMutableDictionary*dict=[[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
NSString*strID=[NSString stringWithFormat:@“%d”,intID];
NSLog(@“数组:%@,[dict objectForKey:strID]);
[contentArray removeAllObjects];
[contentArray addObjectsFromArray:[dict objectForKey:strID]];
[内容数组保留];
[自缩放鼠标位置:mapViewuno.userLocation];
}
-(void)zoomToUserLocation:(MKUserLocation*)userLocation
{
如果(!userLocation)
返回;
协调区域;
region.center=userLocation.location.coordinate;
region.span=MKCoordinateSpanMake(.5,5);
region=[mapViewuno RegionAtfits:region];
[mapViewuno setRegion:区域动画:是];
计数器=0;
[mapViewuno removeAnnotations:mapViewuno.annotations];
如果([contentArray计数]!=0)
{

对于(indexValue=0;indexValue此部件导致问题:

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

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

此部分导致问题:

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

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

如果删除
di,它是否会停止对用户位置进行缩放dUpdateUserLocation
?如果删除
didUpdateUserLocation
,它是否会停止在用户位置上缩放?谢谢,这使它变得非常完美。很抱歉,我对Xcode有点陌生。你能告诉我如何编写Bool函数吗?这里没有足够的空间来解释它,但你需要一个Bool实例变量或@property来存储它值。查看大NED牧场目标C编程书,你会得到一个伟大的基础Obj-C.我试着声明这个BOL属性:@属性(非原子,赋值)BOOL zoomedToUserLocation;但似乎无法调用它来实现?谢谢,这很完美。很抱歉,我对Xcode有点陌生。你能告诉我如何编写BOOL函数吗?这里没有足够的空间来解释它,但你需要一个BOOL实例变量或@property来存储该值。请查看Big Nerd RaNCH Objject C编程手册,您将获得Obj-C.的一个伟大的基础。我尝试声明这个BOL属性:@属性(非原子,赋值)BooZooDouTouSerS位置;但似乎不能调用它以进行调用。