2013-05-09 63 views
0

我有一個地圖視圖與幾個註釋。註釋的標題是業務的名稱,字幕是地址。在註釋視圖中有一個detailDisclosure按鈕,它將用戶帶到所選註釋的詳細視圖。詳細信息視圖有一個UILabel,其中包含註釋標題中的文本,UILabel中包含註釋字幕中的文本(地址),帶有商業網站的按鈕,帶有公司電話號碼的按鈕以及最後一個用於獲取方向的地址。我想從註解字幕中獲取地址,這樣當用戶點擊「獲取方向」按鈕時,它會在Apple地圖應用程序中打開該地址,以便他們獲取路線。我的問題是,如何使用註解副標題中的地址打開地圖應用程序?從AnnotationDetailView獲取地圖上的方向按鈕註釋詳細視圖

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    [ti setText:annTitle]; 
    [sub setText:annSub]; 
} 

- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    // If it's the user location, return nil 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    // Try to dequeue an existing pin view first 
    static NSString *annotationIdentifier = @"AnnotationIdentifier"; 
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationIdentifier]; 
    pinView.animatesDrop = YES; 
    pinView.canShowCallout = YES; 

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
    // [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside]; 
    pinView.rightCalloutAccessoryView = rightButton; 

    return pinView; 
} 

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    AnnotationDetailView *detail = [[AnnotationDetailView alloc] initWithNibName:nil bundle:nil]; 
    id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0]; 
    ann = view.annotation; 
    detail.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    NSLog(@"%@", ann.title); 
    NSLog(@"%@", ann.subtitle); 
    detail.annTitle = ann.title; 
    detail.annSub = ann.subtitle; 
    [self.navigationController pushViewController:detail animated:YES]; 
} 

viewDidLoad中這是我與註釋地圖視圖。

// 
// RSFM.m 


#import "RSFM.h" 
#import "AnnotationDetailView.h" 

@interface RSFM() 

@end 

@implementation RSFM 
{ 

} 

@synthesize centerCoordinate, coordinate, title, subtitle, marketAnnotation, location; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    if (self) 
    { 
     self.title = NSLocalizedString(@"Farm Markets", @"Farm Markets"); 
     // Create location manager object 
     locationManager = [[CLLocationManager alloc]init]; 

     [locationManager setDelegate:self]; 

     // And we want it to be as accurate as possible regardless of how much time/power it takes 
     [locationManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers]; 

     // Tell our manager to start looking for its location immediately 
     // [locationManager startUpdatingLocation]; 
    } 

    return self; 
} 
/* 
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate 
{ 
    centerCoordinate = CLLocationCoordinate2DMake(37.7885, 85.3279); 
} 
*/ 
- (void)findLocation 
{ 
    [locationManager startUpdatingLocation]; 
    [activityIndicator startAnimating]; 
    // [locationTitleField setHidden:YES]; 
    [locationManager stopUpdatingLocation]; 
} 

- (void)foundLocation:(CLLocation *)loc 
{ 
    CLLocationCoordinate2D coord = [loc coordinate]; 

    // Zoom the region to this location 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 700000, 700000); 
    [worldView setRegion:region animated:YES]; 

    // Reset the UI 
    // [locationTitleField setText:@""]; 
    [activityIndicator stopAnimating]; 
    // [locationTitleField setHidden:NO]; 
    [locationManager stopUpdatingLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"%@", newLocation); 

    // How many seconds ago was this new location created? 
    NSTimeInterval t = [[newLocation timestamp]timeIntervalSinceNow]; 

    // CLLocationManagers will return the last found location of the device first, you don't want that data in this case. 
    // If this location was made more than 3 minutes ago, ignore it. 
    if (t < -180) 
    { 
     // this is cached data, you don't want it, keep looking 
     return; 
    } 

    [self foundLocation:newLocation]; 
} 

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

- (void)dealloc 
{ 
    // Tell the location manager to stop sending us messages 
    [locationManager setDelegate:nil]; 
} 
/* 
- (IBAction)showDetails:(id)sender 
{ 
    AnnotationDetailView *detail = [[AnnotationDetailView alloc] initWithNibName:nil bundle:nil]; 
    detail.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    NSLog(@"%@", marketAnnotation.title); 
    detail.ti.text = marketAnnotation.title; 
    [self.navigationController pushViewController:detail animated:YES]; 
} 
*/ 
- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    // If it's the user location, return nil 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    // Try to dequeue an existing pin view first 
    static NSString *annotationIdentifier = @"AnnotationIdentifier"; 
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationIdentifier]; 
    pinView.animatesDrop = NO; 
    pinView.pinColor = MKPinAnnotationColorRed; 
    pinView.canShowCallout = YES; 

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
    // [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside]; 
    pinView.rightCalloutAccessoryView = rightButton; 

    return pinView; 
} 

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    AnnotationDetailView *detail = [[AnnotationDetailView alloc] initWithNibName:nil bundle:nil]; 
    id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0]; 
    // ann = [mapView.selectedAnnotations objectAtIndex:0]; 
    ann = view.annotation; 
    detail.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    NSLog(@"%@", ann.title); 
    NSLog(@"%@", ann.subtitle); 
    detail.annTitle = ann.title; 
    detail.annSub = ann.subtitle; 
    // detail.annCoord = ann.coordinate; 
    [self.navigationController pushViewController:detail animated:YES]; 

    /* 
    id<MKAnnotation> ann = [mapView.selectedAnnotations objectAtIndex:0]; 
    CLLocationCoordinate2D coord = ann.coordinate; 
    MKPlacemark *placemark = [[MKPlacemark alloc]initWithCoordinate:coord addressDictionary:nil]; 
    MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placemark]; 
    [mapItem openInMapsWithLaunchOptions:nil]; 
    */ 
} 

- (void)viewDidLoad 
{ 
    [locationManager startUpdatingLocation]; 
    [worldView setShowsUserLocation:YES]; 
    [locationManager stopUpdatingLocation]; 

    NSMutableArray *marketLocations = [[NSMutableArray alloc]init]; 

    NSMutableArray *lat = [[NSMutableArray alloc]initWithObjects:@"37.7867266", @"37.0703517", @"37.1610806", @"37.318367", @"37.3559204", @"37.4154066", @"37.4757622", @"37.7450252", @"37.6318978", @"37.0716803", nil]; 

    NSMutableArray *lon = [[NSMutableArray alloc]initWithObjects:@"-87.608209", @"-88.1237899", @"-87.9148629", @"-87.5074402", @"-87.5448032", @"-87.8003148", @"-87.9515986", @"-87.9061638", @"-87.1148574", @"-87.3008418", nil]; 

    NSMutableArray *title1 = [[NSMutableArray alloc]initWithObjects:@"Cates Farm", @"Broadbent B & B Foods", @"Cayce's Pumpkin Patch", @"Metcalfe Landscaping", @"Brumfield Farm Market", @"Dogwood Valley Farm", @"Country Fresh Meats & Farmers Market", @"Jim David Meats", @"Trunnell's Farm Market", @"Lovell's Orchard & Farm Market", nil]; 

    NSMutableArray *subtitle1 = [[NSMutableArray alloc]initWithObjects:@"Hwy 425 Henderson, KY 42420", @"257 Mary Blue Road Kuttawa, KY 42055", @"153 Farmersville Road Princeton, KY 42445", @"410 Princeton Road Madisonville, KY 42431", @"3320 Nebo Road Madisonville, KY 42431", @"4551 State Route 109N Clay, KY 42404", @"9355 US Hwy 60 W Sturgis, KY 42459",@"350 T. Frank Wathen Rd. Uniontown, KY 42461", @"9255 Hwy 431 Utica, KY 42376", @"22850 Coal Creek Road Hopkinsville, KY 42240", nil]; 

    // CLLocationCoordinate2D location; 
    // MKPointAnnotation *marketAnnotation; 

    for (int x = 0; x < [lat count]; x++) 
    { 
     marketAnnotation = [[MKPointAnnotation alloc]init]; 
     location.latitude = [[lat objectAtIndex:x]floatValue]; 
     location.longitude = [[lon objectAtIndex:x]floatValue]; 
     marketAnnotation.coordinate = location; 
     marketAnnotation.title = [title1 objectAtIndex:x]; 
     marketAnnotation.subtitle = [subtitle1 objectAtIndex:x]; 
     [marketLocations addObject:marketAnnotation]; 
    } 

    [worldView addAnnotations:marketLocations]; 

    /* 
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 
    point.coordinate = CLLocationCoordinate2DMake(37.7867266, -87.608209); 
    point.title = @"Cates Farm"; 
    point.subtitle = @"Hwy 425 Henderson, KY 42420"; 
    [worldView addAnnotation:point]; 
    */ 
} 

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    CLLocationCoordinate2D loc = [userLocation coordinate]; 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 700000, 700000); 
    [worldView setRegion:region animated:YES]; 
    [locationManager stopUpdatingLocation]; 
    locationManager.delegate = nil; 
} 

- (IBAction)selectSegmentControl 
{ 
    int segmentTouched = [mapVarieties selectedSegmentIndex]; 
    NSString *segmentName = [mapVarieties titleForSegmentAtIndex:segmentTouched]; 
    if ([segmentName isEqualToString:@"Street"]) 
    { 
     [worldView setMapType:MKMapTypeStandard]; 
    } 
    if ([segmentName isEqualToString:@"Satellite"]) 
    { 
     [worldView setMapType:MKMapTypeSatellite]; 
    } 
    if ([segmentName isEqualToString:@"Hybrid"]) 
    { 
     [worldView setMapType:MKMapTypeHybrid]; 
    } 
} 

@end 
+0

問題和疑問是? – Wain 2013-05-09 18:34:47

+0

我的問題是,如何打開註解字幕中的地址的地圖應用程序? – raginggoat 2013-05-09 19:38:11

回答

0

您需要使用MKMapItem -openInMapsWithLaunchOptions:記錄here。您需要先創建MKPlacemark,如果您具有所需的座標,則可以直接創建地標,並且如果只有地址,則需要使用CLGeocoder

+0

我確實有座標,我正在使用這些座標將註釋放在我的地圖上。 – raginggoat 2013-05-09 20:15:47

+0

@ user2029585 - 是的,但Wain是正確的,在iOS 6中,您只需從'MKPlacemark'(您可以從您的註釋中的「座標」構建而成,或者如果您的註釋是來自' MKPlacemark',你可以直接從你的註釋中完成),然後調用['openInMapsWithLaunchOptions'](http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapItem_class/Reference/Reference.html#//apple_ref/DOC/UID/TP40011746-CH1-SW14)。 – Rob 2013-05-09 20:58:42

+1

這也在[要求地圖應用程序顯示路線]中描述(http://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/ProvidingDirections/ProvidingDirections.html#//apple_ref/doc/ uid/TP40009497-CH8-SW6)部分_位置感知編程指南._ – Rob 2013-05-09 21:01:35

相關問題