2013-02-15 89 views
4

有几个教程和关于此的问题,但我还没有足够的知识,以了解如何将它们实现到我的特定应用程序。我从URL获取JSON注释数据并解析它并在for循环中添加每个注释。我想在每个注释中添加一个链接以打开地图获取路线。如何将'地图'应用链接添加到我的每个地图注释

这里是我的ViewController.H

#import <UIKit/UIKit.h> 
#import <MediaPlayer/MediaPlayer.h> 
#import <MapKit/MapKit.h> 

//MAP Setup 
@interface ViewController : UIViewController <MKMapViewDelegate> 

//map setup 
@property (weak, nonatomic) IBOutlet MKMapView *mapView; 
@property (nonatomic, strong) NSMutableData *downloadData; 
//- (IBAction)refreshTapped:(id)sender; 


@end 

和我ViewController.m

- (void)viewDidLoad 
{ 
    //////////////////////// 
    //Connection to download JSON map info 
    //////////////////////// 
    self.downloadData = [NSMutableData new]; 

    NSURL *requestURL2 = [NSURL URLWithString:@"http:OMITTED"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:requestURL2]; 
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; 

    //scroller 
    [scroller setScrollEnabled:YES]; 
    [scroller setContentSize:CGSizeMake(320,900)]; 

    [super viewDidLoad];  

//Map 
    [self.mapView.userLocation addObserver:self 
           forKeyPath:@"location" 
            options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) 
            context:nil]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [self.downloadData appendData:data]; 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    id parsed = [NSJSONSerialization JSONObjectWithData:_downloadData options:kNilOptions error:nil]; 

    //////////////////////// 
    //Iterating and adding annotations 
    //////////////////////// 
    for (NSDictionary *pointInfo in parsed) 
    { 
     NSLog([pointInfo objectForKey:@"name"]); 
     double xCoord = [(NSNumber*)[pointInfo objectForKey:@"lat"] doubleValue]; 
     double yCoord = [(NSNumber*)[pointInfo objectForKey:@"lon"] doubleValue]; 
     CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(xCoord, yCoord); 


     MKPointAnnotation *point = [MKPointAnnotation new]; 
     point.coordinate = coords; 
     point.title = [pointInfo objectForKey:@"name"]; 

     [self.mapView addAnnotation:point];// or whatever your map view's variable name is 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

//centers map on user loc and then allows for movement of map without re-centering on userlocation check. 
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ 
    if ([self.mapView showsUserLocation]) 
    { 
     MKCoordinateRegion region; 
     region.center = self.mapView.userLocation.coordinate; 

     MKCoordinateSpan span; 
     span.latitudeDelta = .50; // Change these values to change the zoom 
     span.longitudeDelta = .50; 
     region.span = span; 

     [self.mapView setRegion:region animated:YES]; 

     self.mapView.showsUserLocation = NO;} 
} 

- (void)dealloc 
{ 
    [self.mapView.userLocation removeObserver:self forKeyPath:@"location"]; 
    [self.mapView removeFromSuperview]; // release crashes app 
    self.mapView = nil; 
} 

@end 

回答

7

位置感知编程指南Launching the Maps App说:

如果您希望在地图应用中显示地图信息而不是您自己的应用,则可以使用以下两种技术之一以编程方式启动地图:

在iOS 6及更高版本中,使用MKMapItem对象打开地图。
在iOS 5及更低版本中,按照Apple URL Scheme Reference中所述创建并打开特殊格式的地图URL。
打开地图应用的首选方式是使用MKMapItem类。此课程提供openMapsWithItems:launchOptions:课程方法和openInMapsWithLaunchOptions:实例方法,用于打开应用程序并显示位置或方向。

有关说明如何打开地图应用示例,请参见“Asking the Maps App to Display Directions.”

所以,你应该:

  1. 确保定义您的视图控制器是delegate您的地图视图;

  2. viewForAnnotation,轮流在canShowCallout并打开callout accessory view

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
    { 
        if ([annotation isKindOfClass:[MKUserLocation class]]) 
         return nil; 
    
        MKAnnotationView* annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                        reuseIdentifier:@"MyCustomAnnotation"]; 
    
        annotationView.canShowCallout = YES; 
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    
        return annotationView; 
    } 
    
  3. 然后写打开的地图如上文所述一个calloutAccessoryControlTapped方法,根据你支持什么的iOS版本,例如,针对iOS 6:

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
    { 
        id <MKAnnotation> annotation = view.annotation; 
        CLLocationCoordinate2D coordinate = [annotation coordinate]; 
        MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]; 
        MKMapItem *mapitem = [[MKMapItem alloc] initWithPlacemark:placemark]; 
        mapitem.name = annotation.title; 
        [mapitem openInMapsWithLaunchOptions:nil]; 
    } 
    

    我不知道你在你的KML哪些额外的地理信息,但你可以在想必填写210你认为合适。


在回答有关如何使用MKPlacemark初始化方法,initWithCoordinateaddressDictionary参数,如果你有NSString变量街道address,在city,该state,在你的后续问题zip等,它看起来像:

NSDictionary *addressDictionary = @{(NSString *)kABPersonAddressStreetKey : street, 
            (NSString *)kABPersonAddressCityKey : city, 
            (NSString *)kABPersonAddressStateKey : state, 
            (NSString *)kABPersonAddressZIPKey : zip}; 

对于这个工作,你必须add the appropriate frameworkAddressBook.framework,到项目,并在你的.m文件导入头:

#import <AddressBook/AddressBook.h> 

真正的问题,虽然,如何设置nameMKMapItem,因此它不会在地图应用中显示为“未知位置”。这是因为设置name属性,可能只是从你annotation抓住title简单:

mapitem.name = annotation.title; 
+0

感谢;正是我需要的。 – Adama 2013-02-15 21:02:28

+0

你能为我解释一下addressDictionary吗?我将它保留为零,并且注释仍显示并允许我在地图中路由,但显示“未知位置”作为引脚名称。我假设,可能不正确,这与地址词典有关?我用一个字典在另一个方法上迭代我的注释的值;我想在这个方法中引用相同的字典吗? – Adama 2013-02-15 21:21:01

+0

@ user2051879 ['MKMapItem'](http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapItem_class/Reference/Reference.html#//apple_ref/occ/cl/MKMapItem)有一个一系列有趣的属性。使用'annotation'的'title'来设置'MKMapItem'的'name'。 (请参阅修订后的答案)['initWithCoordinate'的'addressDictionary'](http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKPlacemark_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40008322-CH1-SW3)用于地址构成组件(街道,城市,州等)。这取决于您的KML有什么。 – Rob 2013-02-15 21:54:15

相关问题