rkirac
rkirac

Reputation: 161

How to add clickable icons on the google map with android?

I have a problem, I added some icons from Drawable on Google-Map in Android, And I want to give a link each onesI want to that they can be clickable);

is there anybody to help me plz?

there are my codes :


public class Main extends MapActivity{
    MapView mapView;    
MapController mc;`enter code here`
GeoPoint p;
String[][] cordinates=new String[4][2];
int[] iconsID=new int[4];   

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    getCordinate();
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
   // mapView.setStreetView(true);
    mapView.setSatellite(true);                

    mc = mapView.getController();

    String coordinates[] = {"40.783244", "30.400898"};

    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint(
        (int) (lat * 1E6), 
        (int) (lng * 1E6));

    mc.animateTo(p);
    mc.setZoom(17); 

  //---Add a location marker---
    MapOverlay mapOverlay = new MapOverlay();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay); 
    mapView.invalidate();  
}

 private void getCordinate() {
    // I get icon Id from drawable and I give 4 cordinates to add my icons
    for(int i=0;i<4;i++){
        kordinat[i][0]="40.78"+i+"244";
        kordinat[i][1]="30.40"+i+"898";
    }

    iconsID[0]=R.drawable.hizmet;
    iconsID[1]=R.drawable.kafe;
    iconsID[2]=R.drawable.hastane;
    iconsID[3]=R.drawable.petrolofisi;      

}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

class MapOverlay extends com.google.android.maps.Overlay // Thid class add icon on map
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
        {
            double lat;
            double lng;
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels--- I added 4 icons on the map 
            Point screenPts = new Point();

            for(int i=0;i<4;i++){

                 lat = Double.parseDouble(cordinates[i][0]);
                 lng = Double.parseDouble(cordinates[i][1]);

                p = new GeoPoint(
                    (int) (lat * 1E6), 
                    (int) (lng * 1E6));


              mapView.getProjection().toPixels(p, screenPts);

              //---add the marker---
              Bitmap bmp = BitmapFactory.decodeResource(getResources(), iconsID[i]);            
              canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);  
            }
            return true;
        }
    } 

Upvotes: 0

Views: 794

Answers (2)

Deepika Lalra
Deepika Lalra

Reputation: 1045

You can use BalloonItemized OverLay class.

Go to this link hope you get your answer.

Anyone implemented the BalloonItemizedOverlay (with multiple points) successfully?

Upvotes: 1

Scorpion
Scorpion

Reputation: 6901

You have to implement the onTap() method to make your drawable clickable. Here I am providing link for the example. In this example it shows AlertDialog on the click of overlay(Drawable in your case).

http://developer.android.com/resources/tutorials/views/hello-mapview.html

Hope this will help!!!

Upvotes: 0

Related Questions