iphone – 如何在我的每个地图注释中添加“地图”应用链接

iphone – 如何在我的每个地图注释中添加“地图”应用链接,第1张

概述有一些关于此的教程和问题,但我还不够了解如何将它们实现到我的特定应用程序中.我从URL获取 JSON注释数据并解析它并在for循环中添加每个注释.我想在每个注释上添加一个链接,以打开地图的方向. 这是我的ViewController.H #import <UIKit/UIKit.h>#import <MediaPlayer/MediaPlayer.h>#import <MapKit/MapKi 有一些关于此的教程和问题,但我还不够了解如何将它们实现到我的特定应用程序中.我从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
解决方法 Launching the Maps App位置感知编程指南说:

If you would prefer to display map information in the Maps app as opposed to your own app,you can launch Maps programmatically using one of two techniques:

In iOS 6 and later,use an 07001 object to open Maps.
In iOS 5 and earlIEr,create and open a specially formatted map URL as described in 07002.
The preferred way to open the Maps app is to use the MKMAPItem class. This class offers both the 07003 class method and the 07004 instance method for opening the app and displaying locations or directions.

For an example showing how to open the Maps app,see 07005

所以,你应该:

>确保将视图控制器定义为地图视图的委托;
>写一个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属性一样简单,可能只是从注释中抓取标题:

mAPItem.name = annotation.Title;
总结

以上是内存溢出为你收集整理的iphone – 如何在我的每个地图注释中添加“地图”应用链接全部内容,希望文章能够帮你解决iphone – 如何在我的每个地图注释中添加“地图”应用链接所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1014838.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存