Skip to content
This repository was archived by the owner on Aug 25, 2023. It is now read-only.

MBTiles for MBXMapKit (fix for issue #3 & #7) - #20

Closed
ghost wants to merge 13 commits into
masterfrom
unknown repository
Closed

ghost wants to merge 13 commits into
masterfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Jan 7, 2014

Copy link
Copy Markdown

This worked for some very simple testing on the iOS side. I don't have a Mac Dev program membership to see what happens on the Mac side. I've got things #ifdef'ed pretty well, so I'm optimistic this could be merged without breaking anything for people who haven't turned on MBTiles support, but this could definitely use some further work.

I'm interested in polishing up to the point that it's ready for production, and I think I will need help (code review & testing) in order to do that.

This is for issue #3

@incanus

incanus commented Jan 7, 2014

Copy link
Copy Markdown
Contributor

Thanks a bunch for this, @wsnook. I don't have the cycles to get too deep into it right now, but I will have a look, plus it's here for others. I especially appreciate the #ifdef. At first glance, this looks pretty good.

@kontextbewusst

Copy link
Copy Markdown

Wow, this looks very good indeed. Thank you! I have thrown a highly detailed 75 MB mbtiles map of my city on it and it is handled very well. However, there is no way to disable the base layer because of the way your checks for showDefaultBaseLayerMode are structured. For me, it would be preferable to have the showDefaultBaseLayer argument of the init method override everything instead. That might be my strange edge case though...

@ghost

ghost commented Jan 8, 2014

Copy link
Copy Markdown
Author

@kontextbewusst,
Thanks, that's exactly the kind of feedback I was hoping to get. I had a feeling there might be problems of some sort with MBTiles maps which don't cover the whole world, but I haven't had a chance to do any more testing on that yet.

I think what you're saying is that you want the map of your city to be the only thing that you see on the screen, and for any tiles which are not included in your MBTiles file (e.g. when you pan past the edge of the city), you want to have a blank background. However, what's actually happening is that Apple's map is showing through around the edges of your city map. Is that correct?

I copied the code for the stuff involving showDefaultBaseLayerMode from initWithTileJSONDictionary:mapView: in the hope that it would work correctly without me needing to make additional changes. However, I'm not entirely clear about what that code was intended to do, how to test whether my copy of it is working as intended, or if the original intent accommodates what you have in mind.

I agree that ideally it should be possible to show only the tiles from an MBTiles file without Apple's map behind them, and I think it should be simple enough to do that. However, it would be nice to do it in a way that stays consistent with the existing behavior of the showDefaultBaseLayer stuff for the MapBox map ID side.

@incanus, do you see an obvious way that I ought to proceed with this? For the moment I can do some testing and figure out what's possible, but I would like to go about this in a way that you'd feel good about merging back into mapbox:master.

@ghost

ghost commented Jan 9, 2014

Copy link
Copy Markdown
Author

@kontextbewusst,
I did some testing tonight. I was able to reproduce your issue, and I came up with a solution which seems pretty clean. It turned out that I didn't have to do anything very different from the original tileJSON code. There are now some examples in MBXViewController.m demonstrating how to make it work (try changing whichExampleToUse).

My solution was to leave the code related to showDefaultBaseLayerMode alone but make the interface for MBXMapViewTileOverlay public so that it's possible to change the value of canReplaceMapContent after a layer has been initialized. Basically I just made it so the underlaying MKTileOverlay API was no longer hidden. I also changed initWithMBTilesPath:mapView: to become initWithMBTilesPath:useWorldForBounds:mapView: so that it would be possible to control the value of boundingMapRect.

The trick to getting the behavior you wanted is having the MKTileOverlay's boundingMapRect set to MKMapRectWorld so that loadTileAtPath:result: will get called for the tiles which are outside of the bounding box set in the MBTiles file's metadata table. If loadTileAtPath:result: does a result(nil,nil) for the missing tiles and canReplaceMapContent is set to YES, then missing tiles will be rendered as blank (no Apple map in the background!).

@incanus,
For whatever it's worth, after the testing and fixes I did tonight, I'm feeling reasonably confident that this code ought to behave itself. One remaining question is what happens on the Mac OS side, but I can't do anything about that on my own since I'm not in the Mac Dev program.

@kontextbewusst

Copy link
Copy Markdown

@wsnook Great, thank you very much. Works perfectly fine for me. Is there anything else you would like me to test? So far I have done some basic stuff, like drawing a few polygons on the map and it behaves perfectly fine, just like the regular MKMapView with Apple's vector tiles.

@ghost

ghost commented Jan 9, 2014

Copy link
Copy Markdown
Author

@kontextbewusst Cool, I'm glad to hear that worked for you. As for further testing, the behavior of maps which don't cover the whole world was the main thing I had doubts about, but now that seems to be okay. If you happen to have a Mac Developer program membership, I'm still curious if the MBTiles code works on Mac. The one other question I can think of is whether initialization from a XIB works correctly. Thanks.

@ghost

ghost commented Jan 10, 2014

Copy link
Copy Markdown
Author

I just realized that making MBXMapViewTileOverlay public has the added benefit of enabling a straightforward solution to issue #7 (comment) without requiring any additional changes to MBXMapKit.

@incanus

incanus commented Jan 15, 2014

Copy link
Copy Markdown
Contributor

Just FYI @wsnook I've been following along at a cursory level (which goes for #21 as well) but haven't had time to dive into code just yet. Hugely appreciated! And thanks @kontextbewusst for your participation, too. I should be able to hit this before too long.

@ghost

ghost commented Jan 15, 2014

Copy link
Copy Markdown
Author

@incanus, Cool, you're welcome. As I've been digging through all this stuff in the past week, I'm starting to wonder if it might be possible to get MBXMapKit set up with the whole works (slick project setup/install, UTFGrid, mapnik vector tile source, OS X compatibility, sample apps showing how to use it all, docs, etc) with a fairly reasonable level of effort. Compared to having worked a bit last year on the main Mapbox iOS SDK, it sure seems like there's way less friction on the MBXMapKit side... so much less code to wrestle with.

@radven

radven commented Jan 17, 2014

Copy link
Copy Markdown

Woah - this looks awesome! I dove into this a bit a while back, but I didn't have time to get very far. You seem to have solved all the key problems.

FYI - as I understand it, now that Mavericks is out of beta you do not need to be in the Mac developer program to get at the SDK, it is included in Xcode. Or is the mapping interface limited to only registered developers?

@incanus

incanus commented Jan 17, 2014

Copy link
Copy Markdown
Contributor

MapKit in OS X is restricted to developer entitlements and apps released in the Mac App Store.

@radven

radven commented Feb 6, 2014

Copy link
Copy Markdown

I've begun playing around with MBTiles support in an overlay on top of a base map - great work!

The one issue I am running into is that when I "overzoom" that map it becomes possible for me to "slip under" the overlay and see the basemap without the overlay rendered if there are not overlay tiles going deep enough.

Consider as an example having a semi-transparent cloud cover MBTiles layer over a MBXMapKit basemap. I'd like to allow deep zooming into the basemap, but the MBTile clouds on top should always be visible - just rendered chunkier as you zoom in. Zooming should never be able to turn a cloudy area clear to the user.

I may just be missing something - but right now this jumps out at me in the MBTiles samples.

Any ideas on the best way to support this sort of overzooming?

@ghost

ghost commented Feb 6, 2014

Copy link
Copy Markdown
Author

To get the behavior you described, loadTileAtPath:result: would need to be somewhat smarter. The key thing is that it would need to invoke the result:(void (^)(NSData *tileData, NSError *error))result completion handler with the appropriate scaled and cropped images for the overzoomed tiles.

Right now, this is all the pull request MBTiles tile loading code does:

NSData *data = [self mbtilesImageDataForPath:path];
result(data, nil);

When mbtilesImageDataForPath: does the sqlite calls to retrieve a tile, it will return nil if there isn't image data for that tile in the MBTiles database.

To implement something smarter, you'd need to check the metadata table for the max zoom, and for tiles at zoom levels higher than that, fetch the highest available zoom level, do the math to crop and scale it, then call result(). That type of functionality might need to be configurable so that people could turn it off, although I suppose maybe you could make a decent argument in favor of scale & crop overzoom being a reasonable default behavior. From what I've seen, vanishing layers tend to bother people, but scaling seems to go over pretty well.

edit: Actually, it's possible that I might be wrong about a lot of that stuff I just wrote -- it might be way easier. I just remembered that initWithMBTilesPath:useWorldForBounds:mapView: sets the MBXMapViewTileOverlay's self.maximumZ to whatever value is in the MBTiles metadata table. I think that means the MBTiles layer's loadTileAtPath:result: wouldn't even get called for the overzoomed tiles. You could try just increasing maximumZ to match your base map. It's possible that MKMapKit will take care of all the scaling for you.

@ghost ghost mentioned this pull request Feb 6, 2014
@KrauseFx

KrauseFx commented Feb 7, 2014

Copy link
Copy Markdown
Contributor

@wsnook Setting the maximumZ property seems to not change anything. Try setting it to ~15 and zoom in on a map, it will display the same image as when calling result(nil, nil)

@ghost

ghost commented Feb 7, 2014

Copy link
Copy Markdown
Author

@KrauseFx, I just spent some time messing around with this using the case 2: MBTiles example (USAMapZoom0to6.mbtiles) from this pull request. After initializing the layer, I set maximumZ all the way up to 22 and added a NSLog(@"zoom:%ld",(long)path.z); to the beginning of loadTileAtPath:result:. Then I carefully zoomed in and out while watching the log.

For zooming in, what I saw happening was that MKMapView was consistently requesting the current zoom level and 3 zoom levels less than that. It seemed to request the lower zoom level first and scale that up, then it requests the higher zoom level and draws that on top (if the higher zoom level is available).

The drawing also seems to work a little differently based on whether you're zooming in or zooming out though. When I zoomed in, MKMapView requested tiles for zoom 2&5, then 6&3, 7&4, 8&5, 9&6, 10&7. For all of those, it shows either the correct size tiles, or scaled up zoom 6 tiles. However, when I zoomed past level 10, and it requested tiles for zoom 11 and zoom 7, the map goes blank.

As I zoomed back out...

  1. Tiles for zoom 10 and 8 are requested, but nothing draws
  2. A bunch of zoom 7 tiles are requested, but still nothing draws
  3. Zoom 9 and more zoom 7 tiles are requested, but nothing draws
  4. All the sudden, scaled up zoom 6 tiles started to draw, but they appeared to be coming from some kind of MKMapKit internal cache, because they weren't ever requested from loadTileAtPath:result:
  5. As I continue to zoom out, lots of tiles get requested for zoom 7, 5, and 4. The drawing and scaling were working well by that point.

What that looks like to me is that MKMapKit does reliably provide automatic scaling for two or three zoom levels beyond the end of what a tile source provides, but it doesn't help with larger gaps. For example, an overlay layer that ends at zoom 10 wouldn't work on top of a zoom 16 street map, but it ought to work at zoom 12 as long as you set the overlay's maximumZ to 12.

It appears that in order to have the type of over-zooming that you wanted over in #35, and that @radven mentioned here, it would be necessary to implement some kind of fancy caching, cropping, and scaling in loadTileAtPath:result:. I don't think there's going to be a quick and easy solution like I was hoping for in my comment above.

@ghost

ghost commented Feb 8, 2014

Copy link
Copy Markdown
Author

I'm working on implementing overzooming in loadTileAtPath:result:. So far I've got it working for MBTiles, and online maps are next. For the moment I'm doing this over at https://github.com/wsnook/mbxmapkit/tree/issue35, which is based off of my dev-0.1.1 branch rather than HEAD of mapbox/mbxmapkit or my branch for this pull request.

I'm doing it this way because I'm concerned that merging overzooming in parallel with MBTiles and Maki markers would be a real hassle. My dev branch already has most of the open pull requests merged into it, so it's a lot easier for me to work on new stuff there without creating future merge conflicts.

@ghost

ghost commented Feb 17, 2014

Copy link
Copy Markdown
Author

I'm gonna close this for now as I've incorporated it into a new branch over at #38.

@ghost ghost closed this Feb 17, 2014
@incanus incanus mentioned this pull request Feb 18, 2014
@ghost ghost mentioned this pull request Feb 18, 2014
@incanus incanus reopened this Feb 21, 2014
@incanus incanus added this to the 0.2.0 milestone Feb 21, 2014
@ghost ghost mentioned this pull request Feb 26, 2014
@ghost

ghost commented Feb 26, 2014

Copy link
Copy Markdown
Author

To anybody who's keeping an eye on this or who might happen along later looking for the MBTiles code...

@incanus and I have been doing a bunch of coordinating today towards getting things cleaned up and merged for the 0.2.0 milestone. As part of that, we've agreed that some of my code needs to be separated out so that only one issue is being addressed per pull request. Also, I made an unfortunate early decision to write the MBTiles code for this pull request on wsnook/master, and now that's causing me a bit of trouble.

For those reasons:

  1. I'm going to close this pull request with the expectation of doing a new MBTiles pull request for the 0.3.0 milestone.
  2. I've archived a copy of the of the original code from this pull request (i.e. cherry picked commits) over at https://github.com/wsnook/mbxmapkit/tree/issues3+7
  3. I'm going to hard reset wsnook:master and do a new pull so that I can stay sync'ed up with mapbox:master. That probably means the history of commits and changed files for this pull request will get messed up. Sorry about that.

@ghost ghost closed this Feb 26, 2014
@ghost

ghost commented Feb 26, 2014

Copy link
Copy Markdown
Author

Wow... I just finished getting wsnook:master cleaned up, and it looks like all the commits for this pull request were actually not affected. GitHub is so cool!

This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants