Reputation: 31
My application I'm developing requires a user to be able to get directions to a specific location. I have a view set up to call a specific web page from my server with a form where they enter their address and it provides a link to Google Maps.
When submitting this page though Safari - it works great! User enters their address, clicks 'Go' and they are brought to the Maps application and guided to the destination. When using an instance of UIWebView within my application, the window loads Google Mobile Maps.
So far I have tried to edit the browser agent identifier and use Method Swizzling (neither worked). An instance of MapView within my application is not suitable because of the UI, otherwise I would have used that approach.
Basically, I want the google maps link within my UIWebView to open in the Maps application as it would in Safari.
Ideas?
User presses the button hbgView to bring up hbgViewController using this code:
-(IBAction)hbgButton:(id)sender{
hbgViewController *hbgView=[[hbgViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:hbgView animated:YES];
}
Then, I load the UIWebView session with the form using this code:
- (void)viewDidLoad
{
[super viewDidLoad];
[svcWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"...com/mobile/map/hbg.html"]]];
}
The form code of the hbg.html document is as follows:
<script>
function redirect()
{
var baseUrl = "http://maps.google.com/maps?saddr="
var suffix = "&daddr=endaddress&hl=en";
document.gmform.action=baseUrl+document.getElementById('url').value+suffix;
}
</script>
<form name="gmform" method="post" onsubmit="redirect()">
<input type="text" id="url">
<input type="submit" value="Go">
</form>
Upvotes: 1
Views: 5303
Reputation: 31
-(IBAction)hbgdirButton:(id)sender{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=harrisburg+pa&hl=en"]];
}
http://maps.google.com/maps?q=
must be the prefix of the link
Upvotes: 2
Reputation: 15813
To persuade your app to open a link in the maps app, you will need to use the openURL method a bit like this;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com?saddr=xxx&daddr=yyy&hl=en"]]
I do this in one of my apps and it works a treat. Obviously you've now handed off to the maps app and can't navigate back to your own though, so this only works for very specific use cases.
Upvotes: 0