我正在尝试在两个位置之间绘制路线。为此,我有两个CLLocation
startPoin,endPoind 实例。我想将这两个位置转换为两个对应的位置地址。之后我想从谷歌地图网络服务中获取所有路线点。下面的代码也是如此。
- (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]; 正在执行首先与MKReverseGeocoder
委托方法进行比较。在此方法中,位置数组为空。获得 EXEBADACCESS。我怎么能超过这个值。在MKReverseGeocoder
委托方法中,我得到了位置名称。委托方法正在执行任何其他线程。