传递注释Mapkit的坐标

时间:2012-06-12 23:12:11

标签: iphone mapkit

在我的应用中,我有一组位置及其相对注释。我有一个按钮来抓住它,我去一个调用URL网络服务来获取谷歌街景的方法。

我的代码是:

- (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;
}

我编写了上一个方法,以获得一个由注释的LatLong组成的网址。

我在注释引脚上有一个名为PressMe的按钮。当我按下它时,我想要执行上一个方法来调用url,但我不明白如何将所选注释传递给streetView方法。

- (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];
}

提前感谢!

1 个答案:

答案 0 :(得分:1)

我将自己回答我的问题。我只是解决了我的问题,它完美地运作。该代码允许显示Web视图,以便获得注释地图Kit注释的steet View google服务。很明显,因为我是相对较新的iphone开发者,如果有人想添加/建议/改进,他是受欢迎的。

我的按钮实现:

- (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];
}

使用此按钮,我调用我的(跟随)方法“urlStreetview”,选择了注释,组成一个字符串,以便有一个带有apropriete GET php参数的complet url。该网址是我(非常简单)网络服务的方向,可以调用google API街景。

- (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];

//现在我选择了注释坐标,所以现在我能够将urladdingo复合到我的基本网址:http://myurl/streetview.php,GET部分,我的意思是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;
}

我希望它有用!

相关问题