2012-03-26 31 views
1

谷歌地圖我有一個使用蘋果地圖非常成功的應用程序:我有一個清單,一些感興趣的點,如果我點擊其中的一個第一應用顯示在地圖上,那麼,如果我點擊它,它會打開Apple Maps應用程序進行導航。獲得「路線不可用」,呼籲與URLWithString

問題是,在第一次點擊/開始時,應用程序會打開Apple Maps,但如果它詢問使用用戶位置的權限,或者它沒有,則Apple Maps不起作用並返回:「Directions Not可用,在這些位置之間找不到方向。「但在一般設置我的應用程序的位置服務設置爲ON。

我以爲我不得不使用一種超時,因爲我的應用程序需要太長的時間來解析導航數據(我從XML檢索數據),但在第二次點擊一切正常,即使我從Mac運行它在調試模式(並將其解析XML甚至第二次)......

我希望我解釋好。任何提示?

-(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後的代碼。

(即使在委託方法,它可能是一個好主意,以檢查是否newLocationnil使用它之前,它與地圖視圖有時會發生,而它的等待用戶授予權限的對位置服務(見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

我做你告訴我的修正,但現在的應用程序,這要求正確使用用戶的位置准許進入一種循環:從地圖移動到App在didUpdateToLocation方法進入總:我忘記管理什麼? – Miwi 2012-03-30 13:33:10

+1

是的,我忘了提:調用的OpenURL之前,做'[的LocationManager stopUpdatingLocation]'否則,當談到回你的應用程序,它再次更新位置,並一直追溯到地圖應用程序。 – Anna 2012-03-30 13:36:54