2013-02-15 86 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

相關問題