谷歌在xcode中映射方向

时间:2012-12-25 06:27:58

标签: iphone google-maps uiwebview

在TableView中我有一个“方向”按钮,当它被点击时,Google地图应该打开相应的网址。我需要在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];
}

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

2 个答案:

答案 0 :(得分:2)

要在GoogleMaps应用中显示路线,请使用新的GoogleMaps网址方案:

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
  2. 您还可以查看链接here

答案 1 :(得分:0)

只需使用self.url就可以在此url之前添加http:// ..它已经附加在self.url变量中...例如使用像bellow ..

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

}

希望这对你有帮助......