【问题标题】:Google maps in xcode for directions谷歌地图在xcode中的方向
【发布时间】:2012-12-25 15:38:59
【问题描述】:

在 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?我哪里出错了?

【问题讨论】:

  • 与 XCode 功能/问题无关的问题。 XCode 标签已移除

标签: iphone google-maps uiwebview


【解决方案1】:

为了在 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

您也可以查看链接here

【讨论】:

  • 以前,当我在 UIViewController 中获取地图视图并显示时,我得到了带有该 URL 的 Googlemaps。但是我现在需要在 WebView 中显示它,因为我需要有从 googlemaps 返回到我的应用程序的后退按钮,当我使用 webView 时这是可能的
  • 请正确检查您的网址。请 NSlog 并在 safari 中打开它,如果它在 safari 中打开,那么它在您的应用中完美运行
  • 您检查过您在 BusinessNearbyLocationsMapView 中的 URL 吗?为什么你在加载之前发布 URL?你在 viewDidLoad 中做了什么?
  • 我没有为 viewDdLoad 做任何事情
  • 你可以发布你的目标网址吗?
【解决方案2】:

只使用 self.url 而不是在此 url 之前添加 http://..它已经附加在 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];

}

希望对您有所帮助...

【讨论】:

  • @Paras.Did 正如你所说。但我的问题是它没有进入 BusinessNearbyLocationsMapView.m。我认为 [self.navigationController pushViewController:self.businessNearbyLocationMapView animated:YES];没有响应。你能告诉我如何在不在 appdelegate 中的 ViewController 中创建 NavigationController。我已直接将它添加到 .xib 文件中并且没有链接到任何地方
  • 我很困惑我应该在哪里创建导航控制器以及如何将它连接到 .xib
  • @Sindhia 看看,我用 xib 在你的新类中创建推送代码,所以用我的代码和你的方法 ok.. 只需评论你的代码并使用我的代码,然后尝试它..跨度>
  • 使用了它。但无法得到它。如果我需要在 .xib 中连接任何东西,我必须在 ViewController 中创建它,因为我已经创建了这样的 UINavigation 控制器:在 .h @property(nonatomic,保留)IBOutlet UINavigationController *navigationController;在 .m 中:@synthesize navigationController;
  • 嘿,但是看看你想要什么是你的 url 在你的这个类的 webview 中打开所以现在我也在这里更新新数据使用这个,你不存储带有格式的 url 所以我认为这是问题,您想要 BusinessNearbyLocationsMapView 类中的导航栏??
猜你喜欢
  • 2021-10-02
  • 2013-08-20
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-16
相关资源
最近更新 更多