0

My idea is to have several (maybe as much as 100) pinpoints in a Google map.

I have created a button in the annotation message that appears when I click the pin. I want each pinpoint to be connected to a website. A different website for each pinpoint.

But at the moment I use this:

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

The problem is that with this code, all the pinpoints open the same website.

Is there any way I can specify a web address for each pinpoint?

#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
Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101

1 Answers1

0

Add a property to your custom annotation class (the one that implements MKAnnotation) that stores the annotation's website. Set this property when you add the annotation to the map:

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

Then in the calloutAccessoryControlTapped delegate method, you can access this property:

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


Based on the additional code you posted, the problem is that the website url strings don't have the http:// scheme prefix which the URLWithString method needs to convert the string to a URL. Because the url strings are invalid, the method returns nil which results in the openURL method doing nothing.

So, for example, the lines that set the website property should be:

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";

Additionally, though the above urls are not a problem, if you intend to use urls with query strings that might contain special characters, you'll also need to escape the url string before passing it. One way is to use the stringByAddingPercentEscapesUsingEncoding method. See this answer by Dave DeLong for an example and some precautions/limitations/alternatives with that method.

Finally, unrelated to your main issue but, here are some additional comments on the code:

  • You don't need to create a region for each annotation just to set its coordinate. You can just do ann.coordinate = CLLocationCoordinate2DMake(63.399785761795506, 12.91691780090332);
  • If you are not using ARC, there are several memory leaks.
  • In viewForAnnotation, you should add an else part to the if statement: else MyPin.annotation = annotation; in case an annotation view is re-used for a different annotation. It probably won't happen with so few annotations but it's logically and technically required.
  • If you want the user location to appear as a blue dot instead of a red pin, you'll need to add this to the top of the viewForAnnotation method: if ([annotation isKindOfClass:[MKUserLocation class]]) return nil;
Community
  • 1
  • 1
  • Thanks for the reply. Even though I don't get any errors, nothing happens when i click the buttons. Here is my code – Anton Hjertén Dec 29 '11 at 17:13
  • Thanks for the reply. Even though I don't get any errors, nothing happens when i click the buttons. Here is my code – Anton Hjertén Dec 29 '11 at 17:14
  • Please click the "edit" link under your question and add the code to it. –  Dec 29 '11 at 17:22
  • Now I have entered the code from viewController.m Thank you very much again! – Anton Hjertén Jan 03 '12 at 10:06
  • Thank you so much! Now its working. I also changed the other suggestions but I'm having some trouble with the first one about CLLocationCoordinate2d. I tried to just add this line of code to my annotation and remove the region-part. But that did't work. Could you explain this a bit further? Thank you once again! – Anton Hjertén Jan 05 '12 at 09:42
  • Did you get a compiler warning or error? What did it say? To use the CLLocationCoordinate2DMake function, you'll need to add the Core Location framework. See http://stackoverflow.com/questions/6987685/what-could-be-the-reason-for-this-error-during-build-undefined-symbols-for-ar and http://stackoverflow.com/questions/7530733/error-while-implementing-kmlviewer-in-my-project. –  Jan 05 '12 at 13:23
  • I have changed to a plist now, so I got rid of the extra code. Thank you so much for all the help!!! – Anton Hjertén Jan 06 '12 at 09:05