1

2 つの場所の間のルートを描画しようとしていますCLLocation。そのために、startPoin、endPoind の 2 つのインスタンスがあります。この 2 つの場所を 2 つの対応する場所の住所に変換したいと考えています。その後、Google マップ Web サービスからすべてのルート ポイントを取得したいと考えています。次のコードも同様です。

- (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デリゲート メソッドとの最初の比較を実行しています。このメソッドの側で位置配列がnullであるためです。EXEBADACCESS を取得します。どうすればこの値を克服できますか。MKReverseGeocoderデリゲート メソッド内で、場所の名前を取得しました。Delegate メソッドは他のスレッドを実行しています。

4

3 に答える 3

2

MKReverseGeocoderは iOS 5 で非推奨になったため、おそらくCLGeocoder代わりに使用する必要があります。

それはさておき、ドキュメントに示されているように、startメソッドは非同期です。これは、必ずしも別のスレッドで結果を取得することを意味するわけではありませんが、結果が得られる前にメソッドが通常戻ることを意味します。

updateRoute両方が終了したときにメソッドを呼び出すには、ジオコーダーを追跡する必要があります。

于 2012-04-17T10:27:19.680 に答える
1

別のスレッドで実行されているかどうかは問題ではありません。重要なのは、応答を受け取るタイミングを制御できないことです。

「開始ルートと終了ルートの両方の値を取得したら、updateRouteメソッドを呼び出す」というロジックが必要です。startPoint値を取得したときに変数の値が設定されていることを確認しendPoint(逆も同様)、updateRoute関数を呼び出すことができます。

于 2012-04-17T10:27:52.837 に答える
0

これが非推奨であるにも関わらずこれを使用したい場合.... が 2 の場合[self updateRoute];- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemarkのみ呼び出す必要があります[locations count]

このようにして、両方の値がある場合に NSURLConnection が呼び出されます。

于 2012-04-17T10:33:35.193 に答える