Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapView show overlay details when clicked

Tags:

android

Ive been trying to acheive the same, this is exactly what i want :- Google Maps Image

i have implemented googleMapView with overlays, i have one issue i want to show a popup when clicked on each overlay, and when i click on another overlay the previous popus should disappear and new one should appear at the clicked location(ie projection points). And popup shouldnt appear when i click anywhere else on screen.Im using onTap event to record onclick. (map_overlay) is the layout that i want to show when someone click a projection point on the map. The code is below and map_overlay.xml could be any file.

Class: ItemizedOverlay

public boolean onTap(GeoPoint p, MapView mapView) {

LayoutInflater inflater = (LayoutInflater)cContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LayoutParams lp = new MapView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, p, LayoutParams.WRAP_CONTENT);
LinearLayout view = (LinearLayout)inflater.inflate(R.layout.map_overlay, null);

mapView.removeView(view);    
mapView.invalidate();    
mapView.addView(view,lp);

mapView.invalidate();

return true;
}

Below is the Main class in which im displaying projection points which is working fine and im calling I have implemented the onTap event in another class as shown above:

public **class MapView** extends MapActivity{
    private ArrayList<MyClass> overlayItem ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.large_mapview);
        mapView = (MapView) findViewById(R.id.mapview);
       // mapView.setBuiltInZoomControls(true);

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.mappointer2);
        ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable,this);
        OverlayItem overlayitem;
        GeoPoint point;
        double lat;
        double lng;        

        for (int i = 0; i < overlayItem.size(); i++) {

            lat = Double.parseDouble(overlayItem.get(i).getLatitude());
            lng = Double.parseDouble(overlayItem.get(i).getLongitude());
            point = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6));

            overlayitem = new OverlayItem(point, i+"".toString(), overlayItem.get(i).getDetails().toString());
            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);

        }

        mapView.invalidate();
    }
}
like image 566
user606669 Avatar asked Feb 07 '11 16:02

user606669


1 Answers

Check out Android-mapviewballoons on github. Has an ItemizedOverlay that you can extend that does this exact thing. I use it in several of my apps.

like image 130
Robby Pond Avatar answered Sep 19 '22 10:09

Robby Pond