2012-03-26 8 views
1

Ich habe eine App, die Apple Maps recht erfolgreich verwendet: Ich habe eine Liste mit einigen interessanten Punkten und wenn ich eine davon antippen, zeigt die Anwendung sie auf der Karte an, Wenn ich darauf tippe, wird die App von Apple Maps zur Navigation geöffnet."Wegbeschreibungen nicht verfügbar" aufrufen Google Maps mit URLWithString

Das Problem ist, dass beim ersten Antippen/Starten der App öffnet Apple Maps, aber weder, wenn es die Erlaubnis fragt, den Standort des Benutzers zu verwenden, oder wenn nicht, funktioniert Apple Maps nicht und gibt zurück: "Wegbeschreibung nicht Verfügbar. Die Richtung konnte nicht zwischen diesen Orten gefunden werden. " aber im Allgemeinen Der Standortdienst für meine App ist auf EIN gesetzt.

Ich dachte, ich müsste eine Art Timeout verwenden, weil meine App zu lange braucht, um Daten für die Navigation zu parsen (ich erhalte Daten von einem XML), aber beim zweiten Antippen funktioniert alles, auch wenn ich es vom Mac aus starte im Debugging-Modus (und es analysiert XML auch das zweite Mal) ...

Ich hoffe, ich habe es gut erklärt. Irgendwelche Hinweise?

-(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]]; 
} 

Antwort

2

In calloutAccessoryControlTapped, Sie verwenden [locationManager location] unmittelbar nach startUpdatingLocation aufrufen.

Die location ist in der Regel nicht direkt nach dem Start für die Suche nach dem aktuellen Standort verfügbar (und wenn es ist, kann es unzuverlässig oder veraltet sein).

Wenn der location verfügbar ist, ruft der Standortmanager seine Delegatenmethode didUpdateToLocation auf, und hier sollten Sie den gesamten Code nach startUpdatingLocation verschieben.

(Selbst in diesem Delegatmethode, könnte es eine gute Idee sein, zu überprüfen, ob newLocationnil vor der Verwendung ist. Manchmal kommt es vor der Kartenansicht, während es für den Benutzer wartet Erlaubnis für standortbezogene Dienste zu gewähren (siehe 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); 
} 

Sie können auch wollen didFailWithError implementieren jedes Problem den Standort zu erhalten zu behandeln oder zumindest dessen bewusst sein.

+0

Ich habe die Korrektur durchgeführt, die Sie mir gesagt haben, aber jetzt fragt die App, die die Erlaubnis, den Standort des Benutzers richtig zu verwenden, eine Art von Schleife: es bewegt sich von Karte zu App immer in didUpdateToLocation Methoden eingeben: habe ich vergessen zu verwalten etwas? – Miwi

+1

Ja, ich habe vergessen zu erwähnen: Bevor Sie openURL aufrufen, tun Sie '[locationManager stopUpdatingLocation];' Anderenfalls, wenn es wieder zu Ihrer App kommt, aktualisiert es den Standort erneut und kehrt zur Google Maps App zurück. – Anna

Verwandte Themen