Iphone 如何添加';地图';指向每个“我的地图”批注的应用程序链接
这方面有一些教程和问题,但我的知识还不足以理解如何在我的特定应用程序中实现它们。我从URL获取JSON注释数据,并对其进行解析,然后将每个注释添加到for循环中。我想在每个注释上添加一个链接,以打开方向图 这是我的ViewController.HIphone 如何添加';地图';指向每个“我的地图”批注的应用程序链接,iphone,ios,xcode,ios6,Iphone,Ios,Xcode,Ios6,这方面有一些教程和问题,但我的知识还不足以理解如何在我的特定应用程序中实现它们。我从URL获取JSON注释数据,并对其进行解析,然后将每个注释添加到for循环中。我想在每个注释上添加一个链接,以打开方向图 这是我的ViewController.H #import <UIKit/UIKit.h> #import <MediaPlayer/MediaPlayer.h> #import <MapKit/MapKit.h> //MAP Setup @interfac
#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
《位置感知编程指南》的第二部分说:
如果您希望在“地图”应用程序中显示地图信息而不是自己的应用程序,则可以使用以下两种技术之一以编程方式启动地图:
在iOS 6及更高版本中,使用对象打开地图。在iOS 5及更早版本中,创建并打开特殊格式的地图URL,如中所述。
打开地图应用程序的首选方法是使用
MKMapItem
类。此类提供类方法和实例方法,用于打开应用程序并显示位置或方向
有关如何打开地图应用程序的示例,请参见
因此,你应该:
代理
addressDictionary
关于如何使用
MKPlacemark
初始值设定方法的addressDictionary
参数的后续问题,如果街道address
、city
、state
、zip
等有NSString
变量,则如下所示:
NSDictionary *addressDictionary = @{(NSString *)kABPersonAddressStreetKey : street,
(NSString *)kABPersonAddressCityKey : city,
(NSString *)kABPersonAddressStateKey : state,
(NSString *)kABPersonAddressZIPKey : zip};
要使其工作,您必须,AddressBook.framework
,将标题导入到项目中,并将其导入.m文件中:
#import <AddressBook/AddressBook.h>
谢谢正是我需要的。你能给我解释一下这本字典吗?我将其保留为nil,注释仍然显示并允许我在地图中布线,但将未知位置显示为pin的名称。我可能错误地认为这与地址字典有关?我使用字典在另一种方法中迭代注释的值;这就是我想在这个方法中引用的字典吗?@user2051879有一系列有趣的属性。使用
注释的标题
设置MKMapItem的名称
。(参见修订后的答案。)的地址字典
用于地址的组成部分(街道、城市、州等)。这取决于你的KML有什么。
- (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];
}
NSDictionary *addressDictionary = @{(NSString *)kABPersonAddressStreetKey : street,
(NSString *)kABPersonAddressCityKey : city,
(NSString *)kABPersonAddressStateKey : state,
(NSString *)kABPersonAddressZIPKey : zip};
#import <AddressBook/AddressBook.h>
mapitem.name = annotation.title;