Android用户定位Google Map显示地图

来源:互联网 发布:java认证资格培训 编辑:程序博客网 时间:2024/03/28 16:43

本文转载自 http://mzba520.iteye.com/blog/964657

作者mzba520 ,谢谢作者的辛勤劳动。

 

 

Java代码 
  1. package cn.cate.main;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.location.Criteria;  
  8. import android.location.Location;  
  9. import android.location.LocationListener;  
  10. import android.location.LocationManager;  
  11. import android.os.Bundle;  
  12. import android.view.View;  
  13. import android.view.Window;  
  14. import android.view.WindowManager;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.ImageButton;  
  17. import android.widget.Toast;  
  18.   
  19. import com.google.android.maps.GeoPoint;  
  20. import com.google.android.maps.MapActivity;  
  21. import com.google.android.maps.MapController;  
  22. import com.google.android.maps.MapView;  
  23. import com.google.android.maps.Overlay;  
  24.   
  25.   
  26. public class MyMapActivity extends MapActivity implements OnClickListener{  
  27.     private MapController myMapController = null;  
  28.     private MapView mapView = null;  
  29.     private double c_lon = 0;  
  30.     private double c_lat = 0;  
  31.     private ImageButton myImageButton = null;  
  32.     private LocationManager lm = null;  
  33.       
  34.     @Override  
  35.     protected boolean isRouteDisplayed() {  
  36.         return false;  
  37.     }  
  38.       
  39.     @Override  
  40.     protected void onCreate(Bundle icicle) {  
  41.         super.onCreate(icicle);  
  42.         requestWindowFeature(Window.FEATURE_NO_TITLE); //全屏    
  43.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    
  44.                       WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  45.         this.setContentView(R.layout.map);  
  46.           
  47.         mapView = (MapView) this.findViewById(R.id.myMapView);//得到myMapView的引用  
  48.         myMapController = mapView.getController();  
  49.         myMapController.setZoom(15);                          //设置放大缩小的比例  
  50.         mapView.setBuiltInZoomControls(true);                 //设置是否在地图上显示放大缩小按钮  
  51.           
  52.         lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    
  53.         String bestProvider = lm.getBestProvider(getCriteria(), true);   //设置查询条件  
  54.         Location l = lm.getLastKnownLocation(bestProvider);      //获取用户位置信息  
  55.         updateView(l);  
  56.         lm.requestLocationUpdates(bestProvider, 10008, ll);    //绑定事件监听  
  57.           
  58.         myImageButton = (ImageButton)findViewById(R.id.MyImageButton);  
  59.         myImageButton.setOnClickListener(this);  
  60.           
  61.         MyMapOverlay myOverlay = new MyMapOverlay();  
  62.         List<Overlay> overlays = mapView.getOverlays();  
  63.         overlays.clear();  
  64.         overlays.add(myOverlay);  
  65.     }  
  66.       
  67.     public void updateView(Location location){  
  68.         if(location != null){  
  69.             c_lat = location.getLatitude();  
  70.             c_lon = location.getLongitude();  
  71.             System.out.println("纬度:"+c_lat+",经度:"+c_lon);  
  72.             GeoPoint gp = new GeoPoint((int)(c_lat * 1E6), (int)(c_lon * 1E6));  
  73.             myMapController.animateTo(gp);  
  74.         }  
  75.     }  
  76.   
  77.     @Override  
  78.     public void onClick(View v) {  
  79.         if(v == myImageButton){  
  80.             boolean flag = true;  
  81.             List<Overlay> overlays = mapView.getOverlays();  
  82.             for(Overlay overlay:overlays){  
  83.                 if(overlay instanceof MyBallonOverlay){  
  84.                     flag = false;  
  85.                     GeoPoint gp = ((MyBallonOverlay) overlay).getGeoPoint();  
  86.                     Intent intent = new Intent();  
  87.                     Bundle bundle = new Bundle();  
  88.                     bundle.putDouble("lat", gp.getLatitudeE6()/1E6);  
  89.                     bundle.putDouble("lon", gp.getLongitudeE6()/1E6);                     
  90.                     intent.putExtras(bundle);  
  91.                     MyMapActivity.this.setResult(RESULT_OK, intent);  
  92.                     MyMapActivity.this.finish();  
  93.                 }  
  94.             }  
  95.             if(flag){  
  96.                 Toast.makeText(this"请点击地图获取店铺位置", Toast.LENGTH_LONG).show();  
  97.             }  
  98.         }  
  99.     }  
  100.       
  101.     public Criteria getCriteria(){  
  102.         Criteria c = new Criteria();  
  103.         c.setAccuracy(Criteria.ACCURACY_FINE);          //设置查询精度  
  104.         c.setSpeedRequired(false);                      //设置是否要求速度  
  105.         c.setCostAllowed(false);                        //设置是否允许产生费用  
  106.         c.setBearingRequired(false);                    //设置是否需要得到方向  
  107.         c.setAltitudeRequired(false);                   //设置是否需要得到海拔高度  
  108.         c.setPowerRequirement(Criteria.POWER_LOW);      //设置允许的电池消耗级别  
  109.         return c;   
  110.     }  
  111.       
  112.     LocationListener ll = new LocationListener() {  
  113.           
  114.         @Override  
  115.         public void onStatusChanged(String provider, int status, Bundle extras) {  
  116.               
  117.         }  
  118.           
  119.         @Override  
  120.         public void onProviderEnabled(String provider) {  
  121.             //当设备启用时触发事件  
  122.             Location l = lm.getLastKnownLocation(provider);  
  123.             updateView(l);  
  124.         }  
  125.           
  126.         @Override  
  127.         public void onProviderDisabled(String provider) {  
  128.             //当设备被禁用时触发事件  
  129.             updateView(null);  
  130.         }  
  131.           
  132.         @Override  
  133.         public void onLocationChanged(Location location) {  
  134.             //当位置改变时触发事件  
  135.             updateView(location);  
  136.         }  
  137.     };  
  138. }  

Java代码 
  1. import java.util.List;  
  2.   
  3. import android.view.MotionEvent;  
  4.   
  5. import com.google.android.maps.GeoPoint;  
  6. import com.google.android.maps.MapView;  
  7. import com.google.android.maps.Overlay;  
  8.   
  9. //覆盖整个地图捕捉触控事件的OverLay  
  10. public class MyMapOverlay extends Overlay{  
  11.     boolean flagMove=false;  
  12.     @Override   
  13.     public boolean onTouchEvent(MotionEvent event, MapView mv) {  
  14.         if(event.getAction() == MotionEvent.ACTION_MOVE){//若移动了触控笔则设置移动标志为true  
  15.             flagMove=true;  
  16.         }  
  17.         else if(event.getAction() == MotionEvent.ACTION_DOWN){//若抬起了触控笔则设置移动标志为false  
  18.             flagMove=false;  
  19.         }  
  20.         else if (MyBallonOverlay.currentBallon==null&&  
  21.                  !flagMove&&  
  22.                  event.getAction() == MotionEvent.ACTION_UP ){  
  23.             //若没有当前选中的气球也没有移动触控笔且触控笔抬起则获取此处的经纬度并添加新气球  
  24.             GeoPoint gp = mv.getProjection().fromPixels(  
  25.                  (int) event.getX(),  //触控笔在屏幕上的X坐标  
  26.                  (int) event.getY()   //触控笔在屏幕上的Y坐标  
  27.             );  
  28.             //显示点击处的经纬度  
  29.             String latStr=Math.round(gp.getLatitudeE6()/1000.00)/1000.0+"";//纬度  
  30.             String longStr=Math.round(gp.getLongitudeE6()/1000.00)/1000.0+"";//经度             
  31.             //清除其他气球的showWindow标记  
  32.             List<Overlay> overlays = mv.getOverlays();   
  33.             for(Overlay ol:overlays){//清除其他气球的showWindow标记  
  34.                 if(ol instanceof MyBallonOverlay){  
  35.                     overlays.remove(ol);  
  36.                 }  
  37.             }   
  38.             //在点击位置添加新气球  
  39.             MyBallonOverlay mbo=new MyBallonOverlay(  
  40.                     gp,//气球的坐标  
  41.                     "店铺位置为:/n经度:"+longStr+"/n纬度:"+latStr//气球的信息  
  42.             );   
  43.             overlays.add(mbo);   
  44.             return true;  
  45.         }  
  46.         return false;  
  47.     }  
  48. }  

 

 

Java代码 
  1. package cn.cate.main;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4.   
  5. import android.view.MotionEvent;  
  6. import com.google.android.maps.GeoPoint;  
  7. import com.google.android.maps.MapView;  
  8. import com.google.android.maps.Overlay;  
  9. import com.google.android.maps.Projection;  
  10. import android.graphics.*;  
  11.   
  12. public class MyBallonOverlay extends Overlay{  
  13.     final static int picWidth=20;  //气球图的宽度  
  14.     final static int picHeight=34//气球图的高度  
  15.     final static int arcR=8;//信息窗口的圆角半径  
  16.       
  17.     static MyBallonOverlay currentBallon=null;//表示当前选中的气球  
  18.     String msg;   
  19.       
  20.     boolean showWindow=false;  
  21.       
  22.     GeoPoint gp;//此气球对应的经纬度  
  23.      
  24.     public MyBallonOverlay(GeoPoint gp,String msg){  
  25.         this.gp=gp;  
  26.         this.msg=msg;  
  27.     }  
  28.       
  29.     public GeoPoint getGeoPoint(){//获得该气球的经纬度GeoPoint  
  30.         return gp;  
  31.     }  
  32.       
  33.     @Override   
  34.     public boolean onTouchEvent(MotionEvent event, MapView mv) {  
  35.         if(currentBallon!=null&&currentBallon!=this){  
  36.             //若当前气球不为空且不是自己,什么都不做  
  37.             return false;  
  38.         }  
  39.         if(event.getAction() == MotionEvent.ACTION_DOWN){  
  40.             //若在气球上按下则设置当前气球为自己,且当前状态为在气球上            
  41.             int x=(int)event.getX();  
  42.             int y=(int)event.getY();  
  43.             Point p= getPoint(mv);   
  44.               
  45.             int xb=p.x-picWidth/2;  
  46.             int yb=p.y-picHeight;  
  47.               
  48.             if(x>=xb&&x<xb+picWidth&&y>=yb&&y<yb+picHeight){  
  49.                 //若在自己这个气球上按下则设置自己为当前气球               
  50.                 currentBallon=this;  
  51.                 return true;  
  52.             }  
  53.         }  
  54.         else if (event.getAction() == MotionEvent.ACTION_MOVE) {  
  55.             //移动事件返回当前气球状态 若当前在气球上则返回true 屏蔽其他移动事件  
  56.             return currentBallon!=null;  
  57.         }             
  58.         else if (event.getAction() == MotionEvent.ACTION_UP) {  
  59.             //获取触控笔位置  
  60.             int x=(int)event.getX();  
  61.             int y=(int)event.getY();  
  62.               
  63.             //获取气球在屏幕上的坐标范围  
  64.             Point p= getPoint(mv);               
  65.             int xb=p.x-picWidth/2;  
  66.             int yb=p.y-picHeight;             
  67.               
  68.             if(currentBallon==this&&x>=xb&&x<xb+picWidth&&y>=yb&&y<yb+picHeight){  
  69.                 //若当前气球为自己且在当前气球上抬起触控则显示当前气球内容    
  70.                 //显示完内容后清空当前气球  
  71.                 currentBallon=null;       
  72.                 showWindow=!showWindow;  
  73.                   
  74.                 List<Overlay> overlays = mv.getOverlays();  
  75.                 overlays.remove(this);//删除此气球再添加  
  76.                 overlays.add(this);//此气球就位于最上面了  
  77.                 for(Overlay ol:overlays){//清除其他气球的showWindow标记  
  78.                     if(ol instanceof MyBallonOverlay){  
  79.                         if(ol!=this){  
  80.                             ((MyBallonOverlay)ol).showWindow=false;  
  81.                         }                         
  82.                     }  
  83.                 }   
  84.                 return true;  
  85.             }  
  86.             else if(currentBallon==this){  
  87.                 //若当前气球为自己但抬起触控不再自己上则清空气球状态并返回true  
  88.                 currentBallon=null;  
  89.                 return true;                  
  90.             }              
  91.         }  
  92.         return false;  
  93.     }  
  94.     @Override  
  95.     public void draw(Canvas canvas, MapView mapView, boolean shadow) {            
  96.         //将经纬度翻译成屏幕上的XY坐标  
  97.         Point p= getPoint(mapView);               
  98.         //在坐标指定位置绘制气球  
  99.         canvas.drawBitmap(MainActivity.bitmap, p.x-picWidth/2, p.y-picHeight, null);  
  100.           
  101.         if(showWindow){//如果showWindow为true则显示信息窗口  
  102.             drawWindow(canvas,p,160);  
  103.         }  
  104.         //调用父类绘制  
  105.         super.draw(canvas, mapView, shadow);  
  106.     }  
  107.       
  108.     public Point getPoint(MapView mapView){//将经纬度翻译成屏幕上的XY坐标  
  109.         Projection projettion = mapView.getProjection();  
  110.         Point p = new Point();  
  111.         projettion.toPixels(gp, p);   
  112.         return p;  
  113.     }  
  114.       
  115.     public void drawWindow(Canvas canvas,Point p,int winWidth){//绘制信息窗口的方法  
  116.         int charSize=15;  
  117.         int textSize=16;  
  118.         int leftRightPadding=2;  
  119.           
  120.         //为信息分行  
  121.         int lineWidth=winWidth-leftRightPadding*2;//每行的宽度  
  122.         int lineCharCount=lineWidth/(charSize); //每行字符数  
  123.         //扫描整个信息字符串分行  
  124.         ArrayList<String> alRows=new ArrayList<String>();  
  125.         String currentRow="";  
  126.         for(int i=0;i<msg.length();i++){           
  127.             char c=msg.charAt(i);  
  128.             if(c!='/n'){  
  129.                 currentRow=currentRow+c;  
  130.             }  
  131.             else{  
  132.                 if(currentRow.length()>0){  
  133.                     alRows.add(currentRow);  
  134.                 }                 
  135.                 currentRow="";  
  136.             }  
  137.             if(currentRow.length()==lineCharCount){  
  138.                 alRows.add(currentRow);  
  139.                 currentRow="";  
  140.             }  
  141.         }  
  142.         if(currentRow.length()>0){  
  143.             alRows.add(currentRow);  
  144.         }  
  145.         int lineCount=alRows.size();//总行数  
  146.         int winHeight=lineCount*(charSize)+2*arcR;//窗体高度  
  147.         //创建paint对象  
  148.         Paint paint=new Paint();  
  149.         paint.setAntiAlias(true);  
  150.         paint.setTextSize(textSize);      
  151.         //绘制 信息窗体圆角矩形  
  152.         paint.setARGB(255255,251,239);  
  153.         int x1=p.x-winWidth/2;  
  154.         int y1=p.y-picHeight-winHeight-1;  
  155.         int x2=x1+winWidth;  
  156.         int y2=y1+winHeight;          
  157.         RectF r=new RectF(x1,y1,x2,y2);       
  158.         canvas.drawRoundRect(r, arcR, arcR, paint);  
  159.         paint.setARGB(255,198,195,198);  
  160.         paint.setStyle(Paint.Style.STROKE);  
  161.         paint.setStrokeWidth(2);  
  162.         canvas.drawRoundRect(r, arcR, arcR, paint);  
  163.           
  164.         //循环绘制每行文字  
  165.         paint.setStrokeWidth(0);  
  166.         paint.setARGB(255101010);       
  167.         int lineY=y1+arcR+charSize;  
  168.         for(String lineStr:alRows){           
  169.             for(int j=0;j<lineStr.length();j++){  
  170.                 String colChar=lineStr.charAt(j)+"";  
  171.                 canvas.drawText(colChar, x1+leftRightPadding+j*charSize, lineY, paint);  
  172.             }  
  173.             lineY=lineY+charSize;  
  174.         }  
  175.     }  
  176. }  

 

 

原创粉丝点击