Iphone iOS-用户是否允许使用其当前位置

Iphone iOS-用户是否允许使用其当前位置,iphone,ios,ios5,mapkit,cllocationmanager,Iphone,Ios,Ios5,Mapkit,Cllocationmanager,在地图视图中的我的应用程序中,我想显示用户当前位置最近的10家商店 但首先,我必须先获取当前位置,然后才能根据用户的位置显示商店 在应用程序的第一次启动中,应用程序询问用户是否允许获取当前位置,因此我必须执行以下操作 如果用户允许 在地图上列出商店 其他的 返回主页 现在我使用下面的代码: mtMap.showsUserLocation=YES; mymanager=[[CLLocationManager alloc] init]; mymanager.delegate=self; CLLoc

在地图视图中的我的应用程序中,我想显示用户当前位置最近的10家商店

但首先,我必须先获取当前位置,然后才能根据用户的位置显示商店

在应用程序的第一次启动中,应用程序询问用户是否允许获取当前位置,因此我必须执行以下操作

如果用户允许 在地图上列出商店 其他的 返回主页

现在我使用下面的代码:

 mtMap.showsUserLocation=YES;
mymanager=[[CLLocationManager alloc] init];
mymanager.delegate=self;
CLLocation *location = [mymanager location];
CLLocationCoordinate2D coordinate2 = [location coordinate];

NSString *latitude1 = [NSString stringWithFormat:@"%f", coordinate2.latitude]; 
NSString *longitude1 = [NSString stringWithFormat:@"%f", coordinate2.longitude];

NSString *myURL = [[NSString alloc] initWithFormat:@"http://www.xxxx.com/xxxx/aaaaa.ashx?term=%@,%@",latitude1,longitude1];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:myURL]];
NSInputStream *dataStream=[[NSInputStream alloc]initWithData:data];

[dataStream open];
if(dataStream)
{

    NSError *err=nil;
    id jsonobject=[NSJSONSerialization JSONObjectWithStream:dataStream options:NSJSONReadingAllowFragments error:&err];
    if([jsonobject respondsToSelector:@selector(objectForKey:)])
    {
        //fill arr
    }
}
但是,当用户第一次打开应用程序时,它就不起作用了,因为延迟允许或延迟获取当前位置。我无法到达他所在的位置,因此无法显示最近的位置

我的逻辑可能有问题。我的意思是,也许我不应该做所有的工作


那么,有人能帮我解决这个问题吗?

与其在viewDidLoad部分下进行,不如在这个方法中插入代码

// Delegate method from the CLLocationManagerDelegate protocol. 
- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
fromLocation:(CLLocation *)oldLocation 
{ 
CLLocation *location = newLocation;
...
这将使代码在用户位置更新后运行。
您还可以在方法中添加if语句,以检查代码是否是第一次运行,如果不是,则返回

是的,这是一个好主意,但这次它不是一直在运行吗?我只想运行一次,这样我可以停止更新位置或其他想法吗?谢谢