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
4

1 回答 1

7

启动位置感知编程指南的地图应用程序说:

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

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

有关如何打开地图应用的示例,请参阅“要求地图应用显示路线”。</a>

所以,你应该:

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

  2. 编写一个viewForAnnotation打开canShowCallout并打开标注附件视图的代码:

    - (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. 然后根据您支持的 iOS 版本(例如,对于 iOS 6)编写一个calloutAccessoryControlTapped打开上述地图的方法:

    - (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 中还有哪些额外的地理信息,但您大概可以根据需要填写addressDictionary


在回答有关如何使用初始化方法的addressDictionary参数的后续问题时,如果您有street 、 the 、 the 、 the等的变量,它看起来像:MKPlacemarkinitWithCoordinateNSStringaddresscitystatezip

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

为此,您必须将适当的框架, AddressBook.framework, 添加到您的项目中,并将标头导入您的 .m 文件中:

#import <AddressBook/AddressBook.h>

不过,真正的问题是如何设置 ,name以便MKMapItem它不会在地图应用程序中显示为“未知位置”。这就像设置name属性一样简单,可能只是title从你的annotation:

mapitem.name = annotation.title;
于 2013-02-15T19:56:43.723 回答