iPhone, Map, Clickable non-rectangular areas
Asked Answered
L

2

6

Anyone have any ideas on how I could implement the following on the iPhone? We have a map of the US, and it has different regions (climate regions, I believe) that are different colors. They are not rectangular, and don't follow state or zip or county or any other defined lines. So, our areas a very round-y and not even necessarily contiguous.

I would LOVE to give our users the ability to click on their region and I would be able to tell from where they touched what region it was, but for the life of me, short of making many many many rectangles to do a best fit on the curves, I can't figure out how to do this.

Any ideas?

EDIT: The regions could be as hard as this... link text

Lorielorien answered 15/5, 2009 at 20:6 Comment(3)
If the map is color-coded, why not let your users click anywhere and then look at the pixel they selected?Armlet
Uh. Uh. Holy crap that's brilliant. I'll look into it.Lorielorien
Nikhil -- 2 things. First, make your comment an answer and I'll accept it. But secondly, do you know of code to do a hittest on what color was touched from the iPhone SDK?Lorielorien
R
10

How are these regions defined? If you can get the point data, then create a CGPath using:

CGPathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,null,xpoints[0],ypoints[0])
for (int i = 1; i < numpoints; ++i) {
  CGPathAddLineToPoint(path,null,xpoints[i],ypoints[i]);
}
CGPathCloseSubpath(path);

Then whenever the user touches, for each region, check whether it contains the touch point:

if (CGPathContainsPoint(path,null,touchPoint,false)) ...

And don't forget to release when you're done with the regions:

CGPathRelease(path);

Note that you can create several separate subpaths in one CGPathRef and it will check all the subpaths when you check for containment.

If you want to, you can try using arcs or curves to get the lines right, but that's something I'm not too familiar with.

Restaurateur answered 15/5, 2009 at 22:59 Comment(3)
Great idea. I'm not sure I could pull it off (I edited my question to link to a sample map). making those kinds of paths, or at least an approximation of them, might be a bit difficult.Lorielorien
After looking at the picture, I'm just curious, how accurate is that image as far as size is concerned? You don't want to have an area too much smaller than about 44x44 pixels or else the user won't be able to click on it. However, Nikhil's idea wasn't too bad; take a look at the color they clicked on. Out of curiosity... is it really just one image that you're showing, without any other data to go with it?Restaurateur
Ed: Honestly, we are in the planning phase right now, so I'll work with the graphic artist. In reality we'll probably make a HUGE map cut into sections, and allow zooming so they can click pretty precisely. Great comments, though.Lorielorien
C
8

Maybe you need a color coded bitmap to do your hit testing against.

You would have one bitmap to display, but another bitmap that has single-color-coded regions. When the user clicks on the display map you check out what color that pixel is in your hit test map, and then perform the approriate action.

Contumelious answered 18/5, 2009 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.