2012-03-26 4 views
1

「地図が利用できません」というメッセージが表示されているAppleマップを使用しているアプリがあります。興味のある点がいくつかあります。そのうちの1つをタップすると、それをタップすると、Apple Maps Applicationが開き、ナビゲートします。URLWithStringでGoogle Mapsを呼び出す

問題は、最初のタップ/スタート時にアプリケーションがApple Mapsを開きますが、ユーザーの場所の使用許可を求められていない場合でもAppleマップが機能せず、これらの場所の間に方向性が見つかりませんでした。しかし一般設定で私のアプリの位置情報サービスはONに設定されています。

私のアプリではナビゲーション用のデータを解析するのに時間がかかりすぎます(私はxmlからデータを取得しますが)2回目のタップですべてがうまく動作するため、Macから実行したとしてもデバッグモードで(xmlを2度目でも解析します)...

私はうまく説明したいと思います。何かヒント?

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    { 
    static NSString *defaultPinID = @"com.invasivecode.pin"; 
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
    if (pinView == nil) pinView = [[[MKPinAnnotationView alloc] 
             initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; 

    pinView.pinColor = MKPinAnnotationColorRed; 


    UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)]; 
    [myAccessoryButton setBackgroundColor:[UIColor clearColor]]; 
    [myAccessoryButton setImage:[UIImage imageNamed:@"flag.png"] forState:UIControlStateNormal]; 
    pinView.rightCalloutAccessoryView = myAccessoryButton; 
    pinView.canShowCallout = YES; 
    pinView.animatesDrop = YES; 
    [myAccessoryButton release]; 
} 
else { 
    [mapView.userLocation setTitle:@"You're here"]; 
} 
return pinView; 
} 

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{ 

locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
[locationManager startUpdatingLocation]; 

CLLocation *location = [locationManager location]; 

// Configure the new event with information from the location 
CLLocationCoordinate2D coordinate = [location coordinate]; 

NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; 
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude]; 


CLLocationCoordinate2D start = { [latitude floatValue], [longitude floatValue] }; 
CLLocationCoordinate2D end = {[posto.latitude floatValue], [posto.longitude floatValue]}; 

NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", start.latitude, start.longitude, end.latitude, end.longitude]; 

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]]; 
} 

答えて

2

calloutAccessoryControlTappedでは、あなたはすぐにstartUpdatingLocationを呼び出した後[locationManager location]を使用しています。

locationは通常、現在の場所の検索を開始した直後には利用できません(存在する場合は、信頼できないか、または古くなっている可能性があります)。

locationが利用可能な場合、ロケーションマネージャはdidUpdateToLocationデリゲートメソッドを呼び出し、startUpdatingLocationの後にあるすべてのコードを移動する必要があります。

(でもそのデリゲートメソッドでは、newLocationがそれを使用する前にnilであるかどうかを確認するのは良い考えかもしれません。それは、位置情報サービスのための権限を付与するユーザーを待っている間それは、マップビューで時々起こる(this questionを参照してください) )。

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation]; 
} 

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    if (newLocation == nil) 
    { 
     NSLog(@"didUpdateToLocation: newLocation is nil"); 
     return; 
    } 

    //May want to check newLocation.horizontalAccuracy and 
    //newLocation.timestamp to see if the location is accurate 
    //enough for you. If not, return and wait for a more 
    //accurate location (which may never come). 

    //all the code currently after "startUpdatingLocation" goes here... 
    CLLocation *location = [locationManager location]; 
    ... 
} 

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"locationManager:didFailWithError: %@", error); 
} 

また、場所を取得するすべての問題を処理するためにdidFailWithErrorを実装するか、少なくともそれを認識することができます。

+0

あなたが私に言った訂正をしましたが、現在、ユーザーの場所を使用する権限を適切に要求し、ループの一種を入力します。マップから常にdidUpdateToLocationメソッドに入るアプリケーションに移動します。何か? – Miwi

+1

はい、忘れてしまいました:openURLを呼び出す前に '[locationManager stopUpdatingLocation];を実行してください。そうしないと、アプリに戻って再び場所を更新し、Mapsアプリに戻ります。 – Anna

関連する問題