我正在尝试在两个location.For之间绘制路由,我有两个CLLocation startPoin实例,endPoind。我想转换这两个位置,两个相应的位置Address.After,我想从谷歌地图网络服务获取所有路由点。下面的代码也是如此。
- (void)viewDidLoad{
[super viewDidLoad];
locations =[[NSMutableArray alloc]initWithCapacity:2];//For location Name
[self.mapView_MP setMapType:MKMapTypeStandard];
[self.mapView_MP setZoomEnabled:YES];
[self.mapView_MP setScrollEnabled:YES];
[self update:startPoint]; //MKReverseGeocoder start point
[self update:endPoint]; //MKReverseGeocoder end point
[self updateRoute];
}
- (void)update:(CLLocation*)loc{
MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:loc.coordinate];
geoCoder.delegate = self;
[geoCoder start];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
NSMutableDictionary *cplace=[[NSMutableDictionary alloc]initWithDictionary:[placemark addressDictionary]];
// NSLog(@"The geocoder has returned: %@", [cplace objectForKey:@"FormattedAddressLines"] );
// NSLog(@"The geocoder has returned: %@", [cplace objectForKey:@"Street"] );
[locations addObject:[cplace objectForKey:@"SubAdministrativeArea"]];
NSLog(@"The geocoder has returned: %@", locations );
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
}
- (void)updateRoute {
self.responseData=[NSMutableData data];
NSLog(@" string Formate.................................===%@",locations);
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&mode=driving&sensor=false", [locations objectAtIndex:0],[locations objectAtIndex:1]]]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}在viewDidLoad中,我按如下顺序调用方法
[self update:startPoint]; //MKReverseGeocoder start point
[self update:endPoint]; //MKReverseGeocoder end point
[self updateRoute];但不幸的是,self updateRoute;正在首先执行compare to MKReverseGeocoder委托方法。在此方法中,位置数组为null。获取EXEBADACCESS。我如何才能克服这个值。在MKReverseGeocoder委托方法中,我得到了位置名称。Delegate方法正在执行任何其他线程。
发布于 2012-04-17 18:27:20
iOS 5中不推荐使用MKReverseGeocoder,因此您可能应该使用CLGeocoder。
除此之外,是的,start方法是异步的,正如文档中指出的那样。这并不一定意味着您将在不同的线程上获得结果,但这意味着该方法通常会在结果可用之前返回。
您需要跟踪您的地理编码器,以便在两者都完成时调用updateRoute方法。
发布于 2012-04-17 18:27:53
它是否在另一个线程上执行并不重要。重要的是,你不能控制什么时候得到响应。
您应该有这样的逻辑:“当我获得开始和结束路由的值时,调用我的updateRoute方法”。您可以在获取endPoint的值时检查startPoint变量的值是否已设置(反之亦然),然后才能调用updateRoute函数。
发布于 2012-04-17 18:33:35
如果您仍然希望使用它,尽管它已被弃用...仅当[locations count]为2时,才应在- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark中调用[self updateRoute];。
这样,当您拥有这两个值时,就会调用NSURLConnection。
https://stackoverflow.com/questions/10189305
复制相似问题