BuySellAds.com

My book Barcodes with iOS 7 is nearing completion. Buy it now to get early access!
Our DNA is written in Objective-C
Jump

OpenStreetMap in a UIWebView

Nicholas asks:

I’ve made an attempt recently to incorporate OpenStreetMap into my app. I’ve failed miserably. The result is a blank webpage. Any help/hints would be appreciated.

I immediately suspected that there was wrong with how he constructed the URL for the UIWebView, because I did run into a similar problem before.

He sent this HTML code as maps.html:

    <!--mce:0-->
 
 
    <!--mce:1-->
    <!--mce:2-->
    <!--mce:3-->
 
<!-- body.onload is called once the page is loaded (call the 'init' function) -->
 
    <!-- define a DIV into which the map will appear. Make it take up the whole window -->

And the following code snippet as his experiment:

NSString *path = [[NSBundle mainBundle] pathForResource:@"maps" ofType:@"html"];
NSString *urlWithPrams = [NSString stringWithFormat:@"%@?mylat=%0.6f&amp;mylon=%0.6f",path,lat,lon];
NSURL *url = [NSURL fileURLWithPath:urlWithPrams];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[mapWebView loadRequest:request];

After having tried out a couple of things I got the local web page to display even with URL parameters. Here’s what I learned:

  • fileURLWithString does not seem to work with parameters, you can only pass a path
  • At least in simulator the path to the file contains a space, so you need to escape the path with addPercentEscapes
  • this leaves only the method of manually assembling the full url and then using URLwithString

Therefore my solution to the problem was the following code:

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
     [super viewDidLoad];
 
     double lat=0;  // just so that those are defined
     double lon=0;
 
     NSString *path = [[NSBundle mainBundle] pathForResource:@"maps" ofType:@"html"];
     NSString *urlWithPrams = [@"file:" stringByAppendingString:[[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByAppendingString:[NSString stringWithFormat:@"?mylat=%0.6f&amp;mylon=%0.6f",lat,lon]]];
 
     NSURL *url = [NSURL URLWithString:urlWithPrams];
     NSURLRequest *request = [NSURLRequest requestWithURL:url];
     [mapWebView loadRequest:request];
 }

If you are wondering why your UIWebView only displays a blank page then I guarantee you that either the URL passed in the NSURLRequest is invalid or non-reachable. If this is the case one thing you might want to assert is that NSURL is not nil. With the code above one should in theory be able to embed an OpenStreetMap into his app.

However such an endeavor seems to me to be of limited value since with OS version 3.0 comes integration with the Google Maps API which will most likely prompt most people to use this instead of resorting to such workarounds. One of the reasons why I am looking forward to June.


Categories: Q&A

%d bloggers like this: