doxsi
doxsi

Reputation: 1014

Passing coords of annotation Mapkit tapped

In my app, I have a set of location and their relative annotation. I have a button where cliking it, i go to a method that call a URL web service to get the google street view.

My code is:

- (NSString*)urlStreetview:(id<MKAnnotation>)annotation{

    CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];

    NSLog(@"lat:%f",pinLocation.coordinate.latitude);
    NSLog(@"lon:%f",pinLocation.coordinate.longitude);


    NSString *lat_string = [[NSString alloc] initWithFormat:@"%f", pinLocation.coordinate.latitude];
    NSString *lon_string = [[NSString alloc] initWithFormat:@"%f", pinLocation.coordinate.longitude];


    NSString *url1 = [NSString stringWithFormat:@"http:myweb/streetview.php"];

    NSString *url2 =  [url1 stringByAppendingString: @"?lat="];
    NSString *url3 =  [url2 stringByAppendingString:lat_string];
    NSString *url4 =  [url3 stringByAppendingString:@"&lon="];
    NSString *url  =  [url4 stringByAppendingString:lon_string];


    NSLog(@"url:%@",url);
    return url;
}

I wrote the previous method to have a url that is composed with the LatLong of the annotation.

I have a button called PressMe on the annotation pin. When I push it, I want to to excute the previous method, to calle the url, but I do not understand how pass the selected annotation to the streetView method.

- (IBAction)PressMe:(id)sender
{
    UIViewController *webViewController = [[[UIViewController alloc] init] autorelease];
    UIWebView *uiWebView = [[[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,480)] autorelease];

    [uiWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: [self urlStreetview: ????]]]];      //HERE IS MY MY PROBLEM

    [webViewController.view addSubview: uiWebView];
    webViewController.title = @"web bar";
    [self.navigationController pushViewController:webViewController animated:YES];

    //[[self navigationController] pushViewController:thirdViewController animated:YES];
}

thanks in advance!

Upvotes: 0

Views: 275

Answers (1)

doxsi
doxsi

Reputation: 1014

I am going to answer my question myself. I just resolve my problem an it work perfectly. The code allows to show a web view in order to have a steet View google service of the annotation map Kit annotation. Obviusly, as I am relatively new iphone developer,if someone wants to add/suggest/improve, he is welcome.

My button implementation:

- (IBAction)PressMe:(id)sender
{
    UIViewController *webViewController = [[[UIViewController alloc] init] autorelease];
    UIWebView *uiWebView = [[[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,480)] autorelease];

    [uiWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: self.urlStreetview]]];

    [webViewController.view addSubview: uiWebView];
    webViewController.title = @"web bar";
    [self.navigationController pushViewController:webViewController animated:YES];

    //[[self navigationController] pushViewController:thirdViewController animated:YES];
}

With this button I call my (following) method "urlStreetview" the, with the annotation selected, composes a string in order to have a complet url with the apropriete GET php parameter. The url is the direction of my (very simple) web service that allow to call the google API street View.

- (NSString*)urlStreetview{

    //there can only be one selected annotation so get the one at index 0
    id<MKAnnotation> selectedAnnotation = [_mapView.selectedAnnotations objectAtIndex:0];

    CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:selectedAnnotation.coordinate.latitude longitude:selectedAnnotation.coordinate.longitude];

//Now I have the annotation coordinate selected, so now I am able to compound the urladdingo to my base url:http://myurl/streetview.php, the GET part, I mean the lat lon coords:

    NSString *lat_string = [[NSString alloc] initWithFormat:@"%f", pinLocation.coordinate.latitude];
    NSString *lon_string = [[NSString alloc] initWithFormat:@"%f", pinLocation.coordinate.longitude];

    NSString *url1 = [NSString stringWithFormat:@"http://myurl/streetview.php"];

    NSString *url2 =  [url1 stringByAppendingString: @"?lat="];
    NSString *url3 =  [url2 stringByAppendingString:lat_string];
    NSString *url4 =  [url3 stringByAppendingString:@"&lon="];
    NSString *url  =  [url4 stringByAppendingString:lon_string];

    NSLog(@"url:%@",url);
    return url;
}

I hope It can be useful!

Upvotes: 1

Related Questions