Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to make a clickable map image with each country producing a different action?

Tags:

I need to display a pretty image of a map of Europe, and I want my app to, e.g. bring up a different activity, when the user clicks each country - each country on the map needs to have a different onClickListener (or equivalent).

Essentially, I need to be able to call a different function when the user taps on France rather than Spain in an image such as this: http://commons.wikimedia.org/wiki/File:Blank_map_of_Europe_cropped.svg

How would I best go about this on Android?

I've got ideas, but there may be some simple way that I'm overlooking.

Many thanks in advance!

Cheers, r3mo

like image 777
r3mo Avatar asked Oct 18 '10 16:10

r3mo


1 Answers

Here is how I solved a similar problem.

First duplicate the image that you want to use as an image map and colour each section. Needless to say, a different colour for each section :D. Then create two ImageViews in your layout. Set the background of the first one as the image that you want displayed to screen and the background of the second as the coloured in one.

Then set the visibility of the second ImageView to invisible. If you run the program at this point you should see the image that you want displayed. Then use an OnTouch listener and get the colour of the pixel where you touched. The colour will correspond to that of the coloured image.

The following getColour method would need to be passed the x and y coordinates of the touch event. R.id.img2 is the invisible image.

private int getColour( int x, int y)
{
    ImageView img = (ImageView) findViewById(R.id.img2);
    img.setDrawingCacheEnabled(true); 
    Bitmap hotspots=Bitmap.createBitmap(img.getDrawingCache()); 
    img.setDrawingCacheEnabled(false);
    return hotspots.getPixel(x, y);
}

Hope this was of some help to you :).

like image 167
Scotty Avatar answered Nov 10 '22 01:11

Scotty