I found myself in need of a C# (.NET/Mono) control for displaying and interacting with maps. I’d already implemented the same functionality in my application using an embedded browser, but that’s a horrible hack, so I decided to implement something nicer. I ended up with this:
I can hear the whining sirens of the don’t-reinvent-the-wheel police approaching already, so a couple of points about that. Yes, there are a couple of projects around already that I could have used almost off the shelf. They’re too complicated for my liking. What I’ve implemented does the basics of what I needed straight away in two small classes, contained in one file. I find this much more pleasant that having to wrangle with someone else’s 25-class monster implementation. Sometimes you can spend more time figuring out how to integrate an external module than you would just writing it yourself, and having spent all that time you still don’t understand the underlying implementation. This becomes more important when you start to extend things further down the line, which I will need to do. I’ve even done all this before, about 7 years ago, using tile data sneakily grabbed from various online proprietary mapping services of the day – I could have used that code, but I even rejected my own wheel.
Furthermore, if all you ever do is piece together other people’s ‘wheels’, you miss out on a lot of understanding, and more importantly fun. I don’t advocate reinventing wheels when someone else is paying for your time or you just need to get something done, but aside from that it’s frequently a good idea. So hopefully the don’t-reinvent-the-wheel police have now gone away to annoy someone else, and I can get back on topic…
In this first incarnation, the control does the following:
- Presents a scrollable (by dragging) and zoomable map, using OpenStreetMap data from their tileserver.
- Selects an appropriate set of tiles for the current zoom level.
- Asynchronously loads and caches (in memory only, for now) tiles, updating the display as new ones are retrieved.
- Allows markers to be overlaid at specified lat/lon positions.
- Raises events to notify the client of map movements made by the user, and clicks at lat/lon positions
I’ve released the whole this under the GPL – get the source code for the control, and the demo app shown in the screenshot, from here. If you use this a lot, or in a widely distributed application, you should probably consider running your own tileserver (easy enough to do) rather than expecting the OpenStreetMap server to take the strain.
Comments and contributions are welcome, as are feature requests which I may well implement if they look easy and/or fun. In fact, I have a big list of things I intend to implement already – the thing you want is probably already on it.
-
cool demo. thnx for posting this


2 comments
Comments feed for this article
Trackback link: http://ciarang.com/posts/csharp-map-control/trackback