【问题标题】:Open different websites through pinpoints in mapview通过mapview中的pinpoints打开不同的网站
【发布时间】:2012-01-04 08:40:25
【问题描述】:

我的想法是在 Google 地图中设置多个(可能多达 100 个)精确点。

我在单击图钉时出现的注释消息中创建了一个按钮。我希望每个精确点都连接到一个网站。每个精确点都有一个不同的网站。

但目前我使用这个:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control
{
    [[UIApplication sharedApplication] 
        openURL:[NSURL URLWithString: @"http://www.google.co.uk"]];

问题在于,使用此代码时,所有精确定位点都会打开同一个网站。

有什么方法可以为每个精确点指定一个网址吗?

#import "ViewController.h"
#import "NewClass.h"

@implementation ViewController
@synthesize mapview;

NSString *myString;

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    //[[UIApplication sharedApplication] 
     //openURL:[NSURL URLWithString: @"http://www.arebikepark.se"]];

   NewClass *ann = (NewClass *)view.annotation;
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:ann.website]];
}

-(IBAction)getLocation {
    mapview.showsUserLocation = YES;
}

-(IBAction)setMap:(id)sender {
    switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
        case 0:
            mapview.mapType = MKMapTypeStandard;
            break;
        case 1:
            mapview.mapType = MKMapTypeSatellite;
            break;
        case 2:
            mapview.mapType = MKMapTypeHybrid;
            break;

        default:
            break;
 }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [mapview setMapType:MKMapTypeStandard];
    [mapview setZoomEnabled:YES];
    [mapview setScrollEnabled:YES];

    MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
    region.center.latitude = 63.399785761795506;
    region.center.longitude =  12.91691780090332;
    region.span.longitudeDelta = 100.0f;
    region.span.latitudeDelta = 100.0f;
    [mapview setRegion:region animated:YES];

    NewClass *ann = [[NewClass alloc] init];
    ann.title = @"Åre";
    ann.subtitle = @"www.arebikepark.se";
    ann.coordinate = region.center;
    ann.website = @"arebikepark.se";
    [mapview addAnnotation:ann];

    MKCoordinateRegion region1 = { {0.0, 0.0 }, {0.0, 0.0 } };
    region1.center.latitude = 61.717050948488904;
    region1.center.longitude =  16.153764724731445;
    region1.span.longitudeDelta = 100.0f;
    region1.span.latitudeDelta = 100.0f;
    [mapview setRegion:region1 animated:YES];

    NewClass *ann1 = [[NewClass alloc] init];
    ann1.title = @"Järvsö";
    ann1.subtitle = @"www.jarvsobergscykelpark.se";
    ann1.coordinate = region1.center;
    ann.website = @"www.jarvsobergscykelpark.se";
    [mapview addAnnotation:ann1];

    MKCoordinateRegion region2 = { {0.0, 0.0 }, {0.0, 0.0 } };
    region2.center.latitude = 57.84191869696362;
    region2.center.longitude =  12.02951431274414;
    region2.span.longitudeDelta = 100.0f;
    region2.span.latitudeDelta = 100.0f;
    [mapview setRegion:region2 animated:YES];

    NewClass *ann2 = [[NewClass alloc] init];
    ann2.title = @"Ale";
    ann2.subtitle = @"www.alebikepark.se";
    ann2.coordinate = region2.center;
    [mapview addAnnotation:ann2];

    MKCoordinateRegion region3 = { {0.0, 0.0 }, {0.0, 0.0 } };
    region3.center.latitude = 61.17768100166834;
    region3.center.longitude =  13.261871337890625;
    region3.span.longitudeDelta = 100.0f;
    region3.span.latitudeDelta = 100.0f;
    [mapview setRegion:region3 animated:YES];

    NewClass *ann3 = [[NewClass alloc] init];
    ann3.title = @"Kläppen";
    ann3.subtitle = @"www.klappen.se/sv/Sommar/Bikepark";
    ann3.coordinate = region3.center;
    ann3.website = @"www.klappen.se/sv/Sommar/Bikepark";
    [mapview addAnnotation:ann3];

    MKCoordinateRegion region4 = { {0.0, 0.0 }, {0.0, 0.0 } };
    region4.center.latitude = 65.82881853569008;
    region4.center.longitude =  15.067813396453857;
    region4.span.longitudeDelta = 100.0f;
    region4.span.latitudeDelta = 100.0f;
    [mapview setRegion:region4 animated:YES];

    NewClass *ann4 = [[NewClass alloc] init];
    ann4.title = @"Hemavan";
    ann4.subtitle = @"www.bikepark.nu";
    ann4.coordinate = region4.center;
    ann4.website = @"www.bikepark.nu";
    [mapview addAnnotation:ann4];

    MKCoordinateRegion region5 = { {0.0, 0.0 }, {0.0, 0.0 } };
    region5.center.latitude = 63.29058608431198;
    region5.center.longitude =  18.7042236328125;
    region5.span.longitudeDelta = 100.0f;
    region5.span.latitudeDelta = 100.0f;
    [mapview setRegion:region5 animated:YES];

    NewClass *ann5 = [[NewClass alloc] init];
    ann5.title = @"Örnsköldsvik";
    ann5.subtitle = @"www.hkbikepark.se";
    ann5.coordinate = region5.center;
    ann5.website = @"www.hkbikepark.se";
    [mapview addAnnotation:ann5];
}

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *MyPin =(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MyPin"];
    if (!MyPin) {
        MyPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyPin"];
        MyPin.pinColor = MKPinAnnotationColorRed;
        MyPin.canShowCallout = YES;
        UIButton *details = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        MyPin.rightCalloutAccessoryView = details;
        MyPin.canShowCallout = YES;
    }
    return MyPin;
}

@end

【问题讨论】:

    标签: ios mkmapview


    【解决方案1】:

    向您的自定义注释类(实现MKAnnotation 的类)添加一个属性,用于存储注释的网站。在向地图添加注释时设置此属性:

    ann.website = @"http://www.google.co.uk";
    [mapView addAnnotation:ann];
    

    那么在calloutAccessoryControlTapped委托方法中,就可以访问这个属性了:

    MyAnnotationClass *ann = (MyAnnotationClass *)view.annotation;
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:ann.website]];
    


    根据您发布的附加代码,问题在于网站 url 字符串没有 http:// 方案前缀,URLWithString 方法需要将字符串转换为 URL。由于 url 字符串无效,该方法返回 nil,这导致 openURL 方法什么也不做。

    因此,例如,设置website 属性的行应该是:

    ann.website = @"http://www.arebikepark.se";
    ann1.website = @"http://www.jarvsobergscykelpark.se";
    ann2.website = @"http://www.alebikepark.se";  //(line was missing completely)
    ann3.website = @"http://www.klappen.se/sv/Sommar/Bikepark";
    ann4.website = @"http://www.bikepark.nu";
    ann5.website = @"http://www.hkbikepark.se";
    

    此外,虽然上述 url 不是问题,但如果您打算将 url 与可能包含特殊字符的查询字符串一起使用,您还需要在传递之前对 url 字符串进行转义。一种方法是使用stringByAddingPercentEscapesUsingEncoding 方法。请参阅 Dave DeLong 的 this answer 示例以及该方法的一些预防措施/限制/替代方案。

    最后,与您的主要问题无关,但这里有一些关于代码的附加 cmets:

    • 您不需要为每个注释创建一个区域来设置其坐标。你可以做ann.coordinate = CLLocationCoordinate2DMake(63.399785761795506, 12.91691780090332);
    • 如果您未使用 ARC,则存在多个内存泄漏。
    • viewForAnnotation 中,您应该在if 语句中添加else 部分:else MyPin.annotation = annotation; 以防注释视图被重新用于不同的注释。注释这么少可能不会发生,但在逻辑和技术上都是必需的。
    • 如果您希望用户位置显示为蓝点而不是红色图钉,您需要将其添加到 viewForAnnotation 方法的顶部:if ([annotation isKindOfClass:[MKUserLocation class]]) return nil;

    【讨论】:

    • 感谢您的回复。即使我没有收到任何错误,当我单击按钮时也没有任何反应。这是我的代码
    • 感谢您的回复。即使我没有收到任何错误,当我单击按钮时也没有任何反应。这是我的代码
    • 请点击您问题下方的“编辑”链接并添加代码。
    • 现在我已经从viewController.m输入了代码,再次感谢!
    • 非常感谢!现在它的工作。我还更改了其他建议,但我在第一个关于 CLLocationCoordinate2d 的建议中遇到了一些问题。我试图将这行代码添加到我的注释中并删除区域部分。但这没有用。你能再解释一下吗?再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 2021-04-29
    • 2011-10-26
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多