android google地图开发总结——往地图上添加各种东西

学姐吩咐:在某一个位置添加一个闪耀的图片,我但是第一个想到的就是gif图片,但是学姐说android不支持gif,所以只能用带动画效果的View了。那怎样在地图上添加各种东西呢,包括图片,文字与View,比如地图上用的比较多的效果:点击一个地点,弹出一个气泡,显示一些这个地点的信息。

要做到这样的效果,首先要做的是给地图上标记位置。这里涉及到几个类 

GeoPoint 

OverlayItem 

ItemizedOverlay<OverlayItem> 

 

GeoPoint 表示一个地理坐标点,存放经度和纬度,以微度的整数形式存储。 

ItemizedOverlay<OverlayItem> 与OverlayItem :

ItemizedOverlay<OverlayItem> 是一个虚拟类,通过实现ItemizedOverlay<OverlayItem> 类来向地图添加标记,绘制图片文字等信息,还可以给标记添加点击事件。OverlayItem 是ItemizedOverlay<OverlayItem> 的组成元素,一个OverlayItem  代表着一个标记。

 

实现源代码:

View Code 

 

 MainActivity.java的调用:

map=(MapView)this.findViewById(R.id.map_view);
mc=map.getController();
pio=new PositionItemizedOverlay(this.getResources().getDrawable(menu_toolbar_image_array[0]),this);
        map.setBuiltInZoomControls(true);
GeoPoint p1 = new GeoPoint((int)(38.0*1E6),(int)(102*1E6));
        GeoPoint p2 = new GeoPoint((int)(38.0*1E6),(int)(121*1E6));
        GeoPoint p3 = new GeoPoint((int)(28.0*1E6),(int)(121*1E6));
        GeoPoint p4 = new GeoPoint((int)(28.0*1E6),(int)(102*1E6));
        OverlayItem o1 = new OverlayItem(p1,"hello1","helloO1");
        OverlayItem o2 = new OverlayItem(p2,"hello2","helloO2");
        OverlayItem o3 = new OverlayItem(p3,"hello3","helloO3");
        OverlayItem o4 = new OverlayItem(p4,"hello4","helloO4");
        //OverlayItem o3=new OverlayItem();
        pio.addOverlay(o1,BitmapFactory.decodeResource(this.getResources(), menu_toolbar_image_array[0]));
        pio.addOverlay(o2,BitmapFactory.decodeResource(this.getResources(), menu_toolbar_image_array[1]));
        pio.addOverlay(o3,BitmapFactory.decodeResource(this.getResources(), menu_toolbar_image_array[3]));
        pio.addOverlay(o4,BitmapFactory.decodeResource(this.getResources(), menu_toolbar_image_array[4]));
        List<Overlay> listO=map.getOverlays();
        listO.add(pio);
        map.setTraffic(true);
        mc.setZoom(5);mc.animateTo(p1); //设置地图的中心位置

       

通过上面的代码向地图添加了4个标记,在每一个标记上绘制图片与文字信息,在各个标记之间绘制了直线,但是这样还没用解决闪耀图片的问题,在标记上绘制图片是不行的,所以得向地图上添加view。带动画效果view的实现代码我就不说了,看在 MainActivity.java的代码:

map.addView(shineView); 
MapView.LayoutParams geoLp =new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT, o2.getPoint(), 0);
map.updateViewLayout(shineView, geoLp);

         

 shineView是带闪耀动画效果的view

geoLp 是view在地图上的布局的参数

 通过设置标记,根据标记的位置绘制图片文字,向地图上添加view,我们就可以向地图上添加各种各样的东西了

 

 

 

 

posted on 2012-07-11 16:22  民谣  阅读(777)  评论(0编辑  收藏  举报

导航