Search code examples
androidimagemagickclickable

Android clickable map image


I am creating an app which is going to have as a background USA map. I want to click on the map and wherever I click I want to create a small red dot. But it should not be created outside of the map borders. Can I use GridView? Thanks.


Solution

  • You can just create a Linear Layout with a USA map as the background and then set an onTouchListener to that Linear Layout, inside the onTouch just add a red dot on the x and y coordinates that the person clicked. Something like this:

    public class MyAppActivity extends Activity {    
    
    LinearLayout mLayout;
    double x, y;    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);       
    
        mLayout = (LinearLayout) findViewById(R.id.yourID); 
        mLayout.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
    
                x = event.getX();
                y = event.getY();
                //Draw a ball using the x and y coordinates. You can make a separate class to do that.
    
                return false;
        });        
    
     }
    }
    

    and your xml could look something like:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/yourID"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/yourUSABackGround"
        >
    
    
    
    
    </LinearLayout>
    

    Hope that helps.