1

我正在尝试在两个位置之间绘制路线。为此,我有两个CLLocationstartPoin,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委托方法中,我得到了位置名称。委托方法正在执行任何其他线程。

4

3 回答 3

2

MKReverseGeocoder在 iOS 5 中已弃用,因此您可能应该CLGeocoder改用。

除此之外,是的,该start方法是asynchronous,如文档中所述。这并不一定意味着您将在不同的线程上获得结果,但这意味着该方法通常会在结果可用之前返回。

您需要跟踪您的地理编码器以updateRoute在两者都完成后调用您的方法。

于 2012-04-17T10:27:19.680 回答
1

它是否在另一个线程上执行并不重要。重要的是,您无法控制何时收到回复。

你应该有一些逻辑说“当我得到开始和结束路线的值时,调用我的updateRoute方法”。您可以startPoint在获得 的值时检查变量的值是否已设置endPoint(反之亦然),然后才调用该updateRoute函数。

于 2012-04-17T10:27:52.837 回答
0

如果您仍然希望使用它,尽管它已被弃用....您应该 只在[self updateRoute];2 时打电话给您。- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark[locations count]

这样,当您拥有这两个值时,将调用 NSURLConnection 。

于 2012-04-17T10:33:35.193 回答