0

在我的应用程序中,我有一组位置及其相关注释。我有一个单击它的按钮,我转到一个调用 URL Web 服务的方法来获取谷歌街景。

我的代码是:

- (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 组成的 url。

我在注释针上有一个名为 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];
}

提前致谢!

4

1 回答 1

1

我将自己回答我的问题。我只是解决了我的问题,它工作得很好。该代码允许显示 web 视图,以便拥有注释地图套件注释的 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”,并在选择注释的情况下组成一个字符串,以便获得一个带有适当 GET php 参数的完整 url。url 是我的(非常简单的)网络服务的方向,它允许调用谷歌 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 复合到我的基本 url:http://myurl/streetview.php,GET 部分,我的意思是 lat lon 坐标:

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

我希望它可以有用!

于 2012-06-13T18:17:03.070 回答