Ios 通过mapview中的pinpoints打开不同的网站

Ios 通过mapview中的pinpoints打开不同的网站,ios,mkmapview,Ios,Mkmapview,我的想法是在谷歌地图上有几个(可能多达100个)精确定位点 我在单击pin时显示的注释消息中创建了一个按钮。我希望每个点都连接到一个网站。每个精确定位都有一个不同的网站 但现在我用的是: - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { [[UIApplication s

我的想法是在谷歌地图上有几个(可能多达100个)精确定位点

我在单击pin时显示的注释消息中创建了一个按钮。我希望每个点都连接到一个网站。每个精确定位都有一个不同的网站

但现在我用的是:

- (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
#导入“ViewController.h”
#导入“NewClass.h”
@实现视图控制器
@综合地图视图;
NSString*myString;
-(无效)地图视图:(MKMapView*)地图视图注释视图:(MKAnnotationView*)视图调用访问控制点击:(UIControl*)控制
{
//[[UIApplication sharedApplication]
//openURL:[NSURL URLWithString:@”http://www.arebikepark.se"]];
NewClass*ann=(NewClass*)view.annotation;
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:ann.website]];
}
-(iAction)获取位置{
mapview.showsUserLocation=是;
}
-(iAction)setMap:(id)发送方{
开关(((UISegmentedControl*)发送器。已选择分段索引){
案例0:
mapview.mapType=MKMapTypeStandard;
打破
案例1:
mapview.mapType=MKMapTypeSatellite;
打破
案例2:
mapview.mapType=MKMapTypeHybrid;
打破
违约:
打破
}
}
-(无效)viewDidLoad{
[超级视图下载];
[mapview setMapType:MKMapTypeStandard];
[mapview SetZoomeEnabled:是];
[地图视图设置可克隆:是];
MKCoordinateRegion={0.0,0.0},{0.0,0.0};
region.center.lation=63.399785761795506;
region.center.longitude=12.91691780090332;
region.span.longitudeDelta=100.0f;
region.span.latitudeDelta=100.0f;
[地图视图设置区域:区域动画:是];
NewClass*ann=[[NewClass alloc]init];
ann.title=@“Åre”;
ann.subtitle=@“www.arebikepark.se”;
ann.coordinate=region.center;
ann.website=@“arebikepark.se”;
[地图视图添加注释:ann];
MkCoordinateRegion1={{0.0,0.0},{0.0,0.0};
region1.center.lation=61.717050948488904;
region1.center.longitude=16.153764724731445;
区域1.span.longitudeDelta=100.0f;
区域1.span.latitudeDelta=100.0f;
[地图视图设置区域:区域1动画:是];
NewClass*ann1=[[NewClass alloc]init];
ann1.title=@“Järvsö”;
ann1.subtitle=@“www.jarvsobergscykelpark.se”;
ann1.坐标=区域1.中心;
ann.website=@“www.jarvsobergscykelpark.se”;
[地图视图添加注释:ann1];
MkCoordinateRegion2={{0.0,0.0},{0.0,0.0};
region2.center.lation=57.841918696362;
region2.center.longitude=12.02951431274414;
区域2.span.longitudeDelta=100.0f;
区域2.span.latitudeDelta=100.0f;
[地图视图设置区域:区域2动画:是];
NewClass*ann2=[[NewClass alloc]init];
ann2.title=@“Ale”;
ann2.subtitle=@“www.alebikepark.se”;
ann2.坐标=区域2.中心;
[地图视图添加注释:ann2];
MkCoordinateRegion3={{0.0,0.0},{0.0,0.0};
region3.center.lation=61.17768100166834;
region3.center.longitude=13.261871337890625;
区域3.span.longitudeDelta=100.0f;
区域3.span.latitudeDelta=100.0f;
[地图视图设置区域:区域3动画:是];
NewClass*ann3=[[NewClass alloc]init];
ann3.title=@“Kläppen”;
ann3.subtitle=@“www.klappen.se/sv/Sommar/Bikepark”;
ann3.坐标=区域3.中心;
ann3.website=@“www.klappen.se/sv/Sommar/Bikepark”;
[地图视图添加注释:ann3];
MkCoordinateRegion4={{0.0,0.0},{0.0,0.0};
region4.center.lation=65.82881853569008;
region4.center.longitude=15.067813396453857;
区域4.span.longitudeDelta=100.0f;
区域4.span.latitudeDelta=100.0f;
[地图视图设置区域:区域4动画:是];
NewClass*ann4=[[NewClass alloc]init];
ann4.title=@“Hemavan”;
ann4.subtitle=@“www.bikepark.nu”;
ann4.坐标=区域4.中心;
ann4.website=@“www.bikepark.nu”;
[地图视图添加注释:ann4];
MkCoordinateRegion5={0.0,0.0},{0.0,0.0};
region5.center.lation=63.29058608431198;
region5.center.longitude=18.7042236328125;
区域5.span.longitudeDelta=100.0f;
区域5.span.latitudeDelta=100.0f;
[地图视图设置区域:区域5动画:是];
NewClass*ann5=[[NewClass alloc]init];
ann5.title=@“Örnsköldsvik”;
ann5.subtitle=@“www.hkbikepark.se”;
ann5.坐标=区域5.中心;
ann5.website=@“www.hkbikepark.se”;
[地图视图添加注释:ann5];
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
MKPinAnnotationView*MyPin=(MKPinAnnotationView*)[mapView出列可重用AnnotationViewWithIdentifier:@“MyPin”];
如果(!MyPin){
MyPin=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“MyPin”];
MyPin.pinColor=MKPinAnnotationColorRed;
MyPin.canShowCallout=是;
UIButton*详细信息=[UIButton Button WithType:UIButtonTypeDetails];
MyPin.rightCalloutAccessoryView=详细信息;
MyPin.canShowCallout=是;
}
返回MyPin;
}
@结束

将属性添加到存储批注网站的自定义批注类(实现
MKAnnotation
)中。将注释添加到地图时设置此属性:

ann.website = @"http://www.google.co.uk";
[mapView addAnnotation:ann];
然后在
calloutAccessoryControlTapped
delegate方法中,您可以访问此属性:

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

根据您发布的其他代码,问题在于网站url字符串没有
http://
scheme前缀,
URLWithString
方法需要将字符串转换为url。由于url字符串无效,该方法返回
nil
,其中
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";