我正在开发中,我想展示基于当前位置 对于在的applicationDidFinishLaunching我做这个最近的餐馆的iPhone应用程序:如何等待当前位置的位置管理器?
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSURL *url = [[NSURL alloc] initWithString:@"http://192.168.0.150/server1/Service.asmx/nearest?lat1=23.013163&lon1=72.559068"];
NSMutableURLRequest* request2=[NSMutableURLRequest requestWithURL:url];
[request2 setHTTPMethod:@"GET"];
[request2 setTimeoutInterval:10];
NSURLResponse *response=nil;
NSError *err=nil;
NSData *data1=[[NSURLConnection sendSynchronousRequest:request2 returningResponse:&response error:&err] retain];
if(data1 == nil)
{
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"The network is not available.\n Please check the Internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data1];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
@try {
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
}
@catch (NSException * e) {
NSLog(@"Exception in parsing %@ %@",[e name], [e reason]);
}
}
问题的方案。
位置管理器开始更新Web服务被之前执行的位置
,所以我不能得到的位置值。
我我把网络服务调用放在委托方法中,然后在web服务执行之前启动应用程序。 在委托方法中,我在适当的字符串中设置纬度和经度。
的问题是,如何确保服务不会被调用,直到位置管理器更新的位置,然后通过位置到Web服务,然后调用Web服务..
最重要的建议,我可以给:不要做这一切在你的应用程序代理像这样,不要试图强迫你的应用程序与网络同步交互。这可能不太复杂的代码,但它是一个更糟糕的用户体验。核心位置和URL加载系统在默认情况下都是异步的。拥抱那个! – 2009-10-28 06:19:47
我不明白你的想法。你能否详细说明一下。 – harshalb 2009-10-28 06:38:55