大佬教程收集整理的这篇文章主要介绍了iphone – 如何在我的每个地图注释中添加“地图”应用链接,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的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 *@R_673_10613@estuRL2 = [NSURL URLWithString:@"http:OMITTED"]; NSURL@R_673_10613@est *@R_673_10613@est = [NSURL@R_673_10613@est @R_673_10613@estWithURL:@R_673_10613@estuRL2]; NSURLConnection *connection = [NSURLConnection connectionWith@R_673_10613@est:@R_673_10613@est 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
所以,你应该:
>确保将视图控制器定义为地图视图的委托;
>写一个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; }
>然后编写一个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中有哪些其他地理信息,但您可以根据需要填写地址字典.
在回答有关如何使用MKPlacemark初始化方法initWithCoordinate
的addressDictionary参数的后续问题时,如果您有街道地址,城市,州,拉链等的NSString变量,它将如下所示:
NSDictionary *addressDictionary = @{(NSString *)kABPersonAddressStreetKey : street,(NSString *)kABPersonAddressCityKey : city,(NSString *)kABPersonAddressStateKey : state,(NSString *)kABPersonAddressZIPKey : zip};
为此,您必须将add the appropriate framework,AddressBook.framework添加到您的项目并导入.m文件中的标头:
#import <AddressBook/AddressBook.h>
但真正的问题是如何设置MKMapItem的名称,以便它不会在地图应用程序中显示为“未知位置”.这就像设置name属性一样简单,可能只是从注释中抓取标题:
以上是大佬教程为你收集整理的iphone – 如何在我的每个地图注释中添加“地图”应用链接全部内容,希望文章能够帮你解决iphone – 如何在我的每个地图注释中添加“地图”应用链接所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。