0

在 TableView 中,我有一个“方向”按钮,当单击它时,Google 地图应该打开相应的 url。我需要在 WebView 上加载 GoogleMaps。但我无法加载 webView。

ViewController.h:

@property(nonatomic,retain)BusinessNearbyLocationsMapView *businessNearbyLocationMapView;

In ViewController.m:
@synthesize businessNearbyLocationMapView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed: @"ic_launcher.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
    imageView.frame =CGRectMake(0,0,120,50);
    [self.navigationBar setFrame:CGRectMake(0,0,320,70)];
    self.navigationBar.tintColor=[UIColor whiteColor];
    [self.navigationBar addSubview:imageView];
    [imageView release];
}
-(IBAction)showDirections:(id)sender
{
     selectedRow=[sender tag];
    NSMutableDictionary * location = [[[places objectAtIndex:selectedRow]objectForKey:@"geometry"]objectForKey:@"location"];
    NSString * lat = [location objectForKey:@"lat"];
    NSString * lng = [location objectForKey:@"lng"];

    AppAppDelegate  *appDelegate=(AppAppDelegate *)[[UIApplication sharedApplication]delegate];

    businessNearbyLocationMapView.url =@"http://maps.google.com/maps?saddr=%f,%f&daddr=%@,%@",appDelegate.userlatitude,appDelegate.userlongitude,lat,lng;

    [self.navigationController pushViewController:self.businessNearbyLocationMapView animated:YES];
}

BusinessNearbyLocationsMapView.m:

- (void)viewWillAppear:(BOOL)animated {
    NSString *fullUrl = [[NSString alloc] initWithFormat:@"http://%@", self.url];
    NSURL *aboutURL = [[NSURL alloc] initWithString:fullUrl];
    [fullUrl release];
    [viewWebView loadRequest:[NSURLRequest requestWithURL:aboutURL]];
    [super viewWillAppear:animated];
}

但是我看不到加载了相应 url 的 webview?我哪里错了?

4

2 回答 2

2

为了在 GoogleMaps 应用程序中显示路线,使用新的 GoogleMaps URL 方案,如下所示:

comgooglemaps://?saddr=Google+Inc,+8th+Avenue,+New+York,+NY&daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York&directionsmode=transit
  1. 在此处查看此文档URl Scheme

您也可以在此处查看链接

于 2012-12-25T06:33:51.823 回答
0

只需使用 self.url 而不是http://在此 url 之前添加..它已经附加在self.url变量中...例如使用如下所示..

- (void)viewWillAppear:(BOOL)animated {
        viewWebView.delegate=self;
        NSString *fullUrl =  self.url;
        fullUrl = [self removeNull:fullUrl];
        [fullUrl retain];
        NSURL *targetURL = [NSURL fileURLWithPath:fullUrl];
        NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
        [viewWebView loadRequest:request];
        [super viewWillAppear:animated];
}

并在您的文件中添加这两种方法BusinessNearbyLocationsMapView.m..

-(NSString*) trimString:(NSString *)theString {
    NSString *theStringTrimmed = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return theStringTrimmed;
}

-(NSString *) removeNull:(NSString *) string {    

    NSRange range = [string rangeOfString:@"null"];
    //NSLog(@"in removeNull : %d  >>>> %@",range.length, string);
    if (range.length > 0 || string == nil) {
        string = @"";
    }
    string = [self trimString:string];
    return string;
}

以上两种方法是我的自定义方法,通过我们null从字符串中删除或空格,它的问题可能是因为在 url 某处空格或空值消失了......

更新:

-(IBAction)showDirections:(id)sender
{
     selectedRow=[sender tag];
    NSMutableDictionary * location = [[[places objectAtIndex:selectedRow]objectForKey:@"geometry"]objectForKey:@"location"];
    NSString * lat = [location objectForKey:@"lat"];
    NSString * lng = [location objectForKey:@"lng"];

    AppAppDelegate  *appDelegate=(AppAppDelegate *)[[UIApplication sharedApplication]delegate];

    BusinessNearbyLocationsMapView *newbusinessNearbyLocationMapView =[[BusinessNearbyLocationsMapView alloc]initWithNibName:@"BusinessNearbyLocationsMapView" bundle:nil];
    newbusinessNearbyLocationMapView.url =[NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%@,%@",appDelegate.userlatitude,appDelegate.userlongitude,lat,lng];
    [newbusinessNearbyLocationMapView.url retain];
    [self.navigationController pushViewController:newbusinessNearbyLocationMapView animated:YES];
    [newbusinessNearbyLocationMapView relese];

}

希望这对你有帮助...

于 2012-12-25T07:04:32.960 回答