谷歌地圖我有一個使用蘋果地圖非常成功的應用程序:我有一個清單,一些感興趣的點,如果我點擊其中的一個第一應用顯示在地圖上,那麼,如果我點擊它,它會打開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]];
}
我做你告訴我的修正,但現在的應用程序,這要求正確使用用戶的位置准許進入一種循環:從地圖移動到App在didUpdateToLocation方法進入總:我忘記管理什麼? – Miwi 2012-03-30 13:33:10
是的,我忘了提:調用的OpenURL之前,做'[的LocationManager stopUpdatingLocation]'否則,當談到回你的應用程序,它再次更新位置,並一直追溯到地圖應用程序。 – Anna 2012-03-30 13:36:54