2011-12-29 5 views
0

私の考えは、Googleマップにいくつかの(おそらく100もの)ピンポイントを持つことです。マップビューのピンポイントで別のウェブサイトを開きます

ピンをクリックすると表示される注釈メッセージにボタンを作成しました。私はそれぞれのピンポイントをウェブサイトに接続したいと思う。ピンポイントごとに異なるウェブサイト。

しかし、私はこれを使用した瞬間に:

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

問題は、このコードでは、すべてのピンポイントは、同じWebサイトを開くことです。

ピンポイントごとにウェブアドレスを指定する方法はありますか?

#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 

答えて

0

注釈のウェブサイトを保存するカスタム注釈クラス(MKAnnotationを実装1)にプロパティを追加します。あなたが投稿、追加のコードに基づいて

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


、:そして、 calloutAccessoryControlTappedデリゲートメソッドでは、このプロパティにアクセスすることができます

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

:あなたがマップに注釈を追加するときに、このプロパティを設定します。問題は、ウェブサイトのURL文字列がURLWithStringメソッドが文字列をURLに変換する必要があるhttp://スキーム接頭辞を持たないことです。 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文字列を渡す前にエスケープする必要があります。 1つの方法は、stringByAddingPercentEscapesUsingEncodingメソッドを使用することです。 Dave DeLongのthis answerを参照してください。その方法の例といくつかの予防措置/制限/代替案があります。あなたはちょうどその座標を設定し、各注釈ための領域を作成する必要はありません

  • あなたの主な問題とは無関係の最後に

    、しかし、ここでのコードのいくつかの追加のコメントがあります。あなただけができますann.coordinate = CLLocationCoordinate2DMake(63.399785761795506, 12.91691780090332);

  • ARCを使用していない場合、いくつかのメモリリークがあります。
  • viewForAnnotationifステートメントにelseの部分を追加する必要があります。else MyPin.annotation = annotation;注釈ビューを別の注釈で再利用する場合。これはおそらく少数の注釈では起こりませんが、論理的かつ技術的に必要です。
  • は、ユーザーの場所ではなく、赤いピンの青い点として表示させたい場合は、viewForAnnotationメソッドの先頭にこれを追加する必要があります:返信用if ([annotation isKindOfClass:[MKUserLocation class]]) return nil;
+0

感謝。エラーは発生しませんが、ボタンをクリックすると何も起こりません。ここに私のコードです –

+0

返事をありがとう。エラーは発生しませんが、ボタンをクリックすると何も起こりません。ここに私のコード –

+0

あなたの質問の下の "編集"リンクをクリックし、それにコードを追加してください。 – Anna

関連する問題