在google 地图弹出气泡

在google地图上定位一个地点,然后点击弹出一个popView显示一下详细信息
这个popView 随便整的,没有美化练着玩的
Android专业开发群1:150086842
Android专业开发群2:219277004
标签: <无>

代码片段(5)

[图片] 未命名3.jpg

[图片] 未命名4.jpg

[代码] 新建一个图层

01package com.android.angking.yibai;
02 
03import java.util.ArrayList;
04 
05import android.content.Context;
06import android.graphics.Canvas;
07import android.graphics.Color;
08import android.graphics.Paint;
09import android.graphics.Point;
10import android.graphics.drawable.Drawable;
11 
12import com.google.android.maps.ItemizedOverlay;
13import com.google.android.maps.MapView;
14import com.google.android.maps.OverlayItem;
15import com.google.android.maps.Projection;
16 
17public classCustomItemizedOverlay extendsItemizedOverlay<OverlayItem>{
18     privateArrayList<OverlayItem> mOverlays = newArrayList<OverlayItem>();
19     privateContext context;
20     publicCustomItemizedOverlay(Context context,Drawable defaultMarker){
21         super(boundCenterBottom(defaultMarker));
22         this.context = context;
23     }
24     publicvoid draw(Canvas canvas,MapView mapView,booleanshadow){
25         super.draw(canvas, mapView, shadow);
26         //Projection 接口用于屏幕像素点坐标体系和地球概况纬度点坐标体系之间的变换
27         Projection projection = mapView.getProjection();
28         //遍历所有的OverlayItem
29         for(intindex = this.size()-1;index>=0;index--){
30             //获得给定索引的item
31             OverlayItem overlayItem = getItem(index);
32             //把经纬度变换相对于MapView左上角的屏幕像素坐标
33             Point point = projection.toPixels(overlayItem.getPoint(),null);
34              
35             Paint paintText =new Paint();
36             paintText.setColor(Color.RED);
37             paintText.setTextSize(13);
38             //绘制文本
39             canvas.drawText(overlayItem.getTitle(), point.x+10, point.y-15, paintText);
40         }
41     }
42     protectedboolean onTap(intindex){
43         setFocus(mOverlays.get(index));
44        returnsuper.onTap(index);
45          
46     }
47     publicvoid removeAll(){
48       if(mOverlays.size()>=0){
49           mOverlays.removeAll(mOverlays);
50       }
51     }
52     publicvoid addOverlay(OverlayItem overlay){
53         mOverlays.add(overlay);
54         populate();
55     }
56//  public CustomItemizedOverlay(Drawable defaultMarker) {
57//      super(boundCenterBottom(defaultMarker));
58//      // TODO Auto-generated constructor stub
59//  }
60 
61    @Override
62    protectedOverlayItem createItem(inti) {
63        // TODO Auto-generated method stub
64        returnmOverlays.get(i);
65    }
66 
67    @Override
68    publicint size() {
69        // TODO Auto-generated method stub
70        returnmOverlays.size();
71    }
72     
73 
74}

[代码] 自定义的OverlayItem

01package com.android.yibai.antking;
02 
03import android.graphics.Bitmap;
04 
05import com.google.android.maps.GeoPoint;
06import com.google.android.maps.OverlayItem;
07 
08public classMyOverlayItem extends OverlayItem{
09     privateBitmap bitmap;
10    publicMyOverlayItem(GeoPoint point, String title, String snippet,Bitmap bitmap) {
11        super(point, title, snippet);
12        this.bitmap = bitmap;
13        // TODO Auto-generated constructor stub
14    }
15    public Bitmap getBitmap(){
16        returnbitmap;
17    }
18 
19}

[代码] 主类

001package com.android.yibai.antking;
002 
003import java.util.List;
004 
005import android.graphics.Bitmap;
006import android.graphics.BitmapFactory;
007import android.graphics.drawable.Drawable;
008import android.os.Bundle;
009import android.view.View;
010import android.widget.ImageView;
011import android.widget.TextView;
012 
013import com.google.android.maps.GeoPoint;
014import com.google.android.maps.ItemizedOverlay;
015import com.google.android.maps.MapActivity;
016import com.google.android.maps.MapView;
017import com.google.android.maps.Overlay;
018import com.google.android.maps.OverlayItem;
019 
020public classMapMain extends MapActivity{
021    /**
022     * 地图
023     */
024    protectedMapView mapView;
025    /**
026     * 弹出的气泡View
027     */
028    privateView popView;
029     
030    privateint[] image={R.drawable.icon};
031     
032     publicvoid onCreate(Bundle savedInstanceState) {
033            super.onCreate(savedInstanceState);
034             
035            //初始化气泡,并设置为不可见
036            popView = View.inflate(this, R.layout.popview,null);
037            setContentView(R.layout.main);
038             
039            //获得map
040            mapView = (MapView)this.findViewById(R.id.mapview);
041            mapView.addView(popView,newMapView.LayoutParams(
042                    MapView.LayoutParams.WRAP_CONTENT,
043                    MapView.LayoutParams.WRAP_CONTENT,null
044                    ,MapView.LayoutParams.BOTTOM_CENTER));
045           //这里没有给GeoPoint ,在onFoucusChangeListener中设置
046            popView.setVisibility(View.GONE);
047            /**
048             * 创建图标资料(用于显示在overlayItem所表示表记的地位
049             */
050            Drawable drawable =this.getResources().getDrawable(R.drawable.icon);
051             
052            //为mark定以地位和鸿沟
053            drawable.setBounds(0,0,drawable.getIntrinsicWidth(),
054                    drawable.getIntrinsicHeight());
055            CustomItemizedOverlay overlay =new CustomItemizedOverlay(this,drawable);
056            //设置显示/隐藏气泡的位置
057            overlay.setOnFocusChangeListener(onFocusChangeListener);
058            
059            /**
060             * 创建并添加一个标志
061             */
062           GeoPoint point =new GeoPoint(35422006,119524095);
063           //创建标识
064           Bitmap bitmap = BitmapFactory.decodeResource(MapMain.this.getResources(), R.drawable.psu);
065           MyOverlayItem overlayItem =new MyOverlayItem(point
066                   ,"银河公园","这是一个充满神奇的公园,一步一景,我去的时候有一种柳暗花明又一村的感觉",bitmap);
067           overlay.addOverlay(overlayItem);
068           /**
069            * 创建第二个标识
070            */
071           Bitmap bitmap1 = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
072            GeoPoint point1 =new GeoPoint((int)(22.53108*1E6),(int)(113.99151*1E6));
073           MyOverlayItem overlayItem1 =new MyOverlayItem(point1,"秀丽中华","中国最好的旅游胜地之一",bitmap1);
074           overlay.addOverlay(overlayItem1);
075            
076           //向地图里添加自定义的ItemizedOverlay
077           List<Overlay> mapOverlays =mapView.getOverlays();
078           mapOverlays.add(overlay);
079           //设置地图为卫星地图
080           mapView.setSatellite(true);
081           //设置地图可以缩放
082           mapView.setBuiltInZoomControls(true);
083           /**
084            * 取得地图管理对象,用于把握地图
085            *
086            */
087           //设置地图的中间
088           mapView.getController().setCenter(point);
089           //设置地图默认的缩放级别
090           mapView.getController().setZoom(13);
091            
092     }
093    @Override
094    protectedboolean isRouteDisplayed() {
095        // TODO Auto-generated method stub
096        returnfalse;
097    }
098    privatefinal ItemizedOverlay.OnFocusChangeListener onFocusChangeListener =new ItemizedOverlay.OnFocusChangeListener() {
099 
100        @Override
101        publicvoid onFocusChanged(ItemizedOverlay overlay, OverlayItem newFocus) {
102            // TODO Auto-generated method stub
103            //创建气泡窗口
104            if(popView!=null){
105                popView.setVisibility(View.GONE);
106            }
107            if(newFocus !=null){
108                MapView.LayoutParams geoLp =(MapView.LayoutParams) popView
109                .getLayoutParams();
110                geoLp.point = newFocus.getPoint();//这行用于popView的定位
111                TextView title = (TextView)popView.findViewById(R.id.map_bubbleTitle);
112                title.setText(newFocus.getTitle());
113                 
114                TextView desc = (TextView)popView.findViewById(R.id.map_bubbleText);
115                ImageView image = (ImageView)popView.findViewById(R.id.map_bubbleImage);
116                image.setImageBitmap(((MyOverlayItem) newFocus).getBitmap());
117                if(newFocus.getSnippet()==null
118                    ||newFocus.getSnippet().length()==0){
119                    desc.setVisibility(View.GONE);
120                }else{
121                    desc.setVisibility(View.VISIBLE);
122                    desc.setText(newFocus.getSnippet());
123                }
124                mapView.updateViewLayout(popView,geoLp);
125                popView.setVisibility(View.VISIBLE);
126            }
127        }
128     
129     
130    };
131 
132}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值