Iphone 谷歌地图在xcode的方向
在TableView中,我有一个“方向”按钮,当它被点击时,谷歌地图应该会打开相应的url。我需要在网络视图上加载谷歌地图。但我无法加载网络视图Iphone 谷歌地图在xcode的方向,iphone,google-maps,uiwebview,Iphone,Google Maps,Uiwebview,在TableView中,我有一个“方向”按钮,当它被点击时,谷歌地图应该会打开相应的url。我需要在网络视图上加载谷歌地图。但我无法加载网络视图 ViewController.h: @property(nonatomic,retain)BusinessNearbyLocationsMapView *businessNearbyLocationMapView; In ViewController.m: @synthesize businessNearbyLocationMapView; -
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?我错在哪里?用于在谷歌地图应用程序中显示方向,使用新的谷歌地图URL方案,大致如下:
comgooglemaps://?saddr=Google+Inc,+8th+Avenue,+New+York,+NY&daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York&directionsmode=transit
您也可以签出链接只需使用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中的某处空格或null值太大了
更新:
-(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];
}
希望这对你有帮助…以前我在UIViewController中获取地图视图并显示时,得到了带有该url的谷歌地图。但我现在需要在网络视图中显示它,因为我需要有返回按钮,从谷歌地图返回到我的应用程序,这在我获取网络视图时是可能的。请检查您的url是否正确。请登录并在safari中打开它如果它在safari中打开,那么它在您的应用程序中工作完美您在BusinessNearbyLocationsMapView中检查过您的URL吗?为什么要在加载前释放URL?你在ViewdLoad中做了什么?我没有为ViewdLoad做任何事情。你能发布你的targeturl吗?问题与XCode功能/问题无关。XCode标签removed@Paras.Did正如你所说。但我的问题是它没有进入businessNearbyLocationMapView.m。我认为[self.navigationController pushViewController:self.businessNearbyLocationMapView:是];没有响应。请告诉我如何在不在appdelegate中的ViewController中创建NavigationController。我已直接将其添加到.xib文件中,并且没有链接到任何地方。我不知道应该在何处创建导航控制器以及如何将其连接到。xib@Sindhia看看吧,我使用xib为推送新类创建了代码,所以请将我的代码与您的方法一起使用ok。。只需注释您的代码并使用我的代码,然后重试..使用了它。但无法获取它。如果需要在.xib中连接任何内容,我必须在ViewController中创建它,因为我已经创建了一个UINavigation控制器,如下所示:in.h@property(nonatomic,retain)IBOutlet UINavigationController*navigationController;in.m:@synthetic导航控制器;嘿,但是看看你想要什么,那就是你的url在你的this类中的webview中打开,所以现在我也在这里更新新的数据使用这个,你不使用格式存储url,所以我认为这是问题所在,你想要在你的BusinessNearByLocationMapView类中使用导航栏??