百度地图定位模块

来源:互联网 发布:快速软件开发平台 编辑:程序博客网 时间:2024/06/05 10:28


本文转自http://blog.csdn.net/jwzhangjie/article/details/8924198

最近使用百度地图,查看了官方的说明,然后做了一个Demo,作为入门,如果看了我之前的代码,有一个习惯就是使用代码写布局,感觉这样比较快,习惯而已。

源码如下:

[java] view plain copy print?
  1. package com.zhangjie.local;  
  2.   
  3. import android.os.Bundle;  
  4. import android.os.Vibrator;  
  5. import android.app.Activity;  
  6. import android.app.Service;  
  7. import android.util.DisplayMetrics;  
  8. import android.util.Log;  
  9. import android.view.Menu;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.RelativeLayout;  
  15. import android.widget.TextView;  
  16.   
  17. import com.baidu.location.BDLocation;  
  18. import com.baidu.location.BDLocationListener;  
  19. import com.baidu.location.LocationClient;  
  20. import com.baidu.location.LocationClientOption;  
  21. import com.baidu.location.BDNotifyListener;//假如用到位置提醒功能,需要import该类  
  22. public class Local extends Activity implements OnClickListener{  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         getDisplayMetrics();  
  28.         initLayout();  
  29.         setContentView(Parent);  
  30.         myListener = new MyLocationListener();  
  31.         mLocationClient = new LocationClient(getApplicationContext());  
  32.         mLocationClient.registerLocationListener(myListener);  
  33.         //设置定位参数包括:定位模式(单次定位,定时定位),返回坐标类型,是否打开GPS等等  
  34.         option = new LocationClientOption();  
  35.         option.setOpenGps(true);  
  36.         option.setAddrType("all");//返回定位结果包含地址信息  
  37.         option.setCoorType("bd0911");//返回的定位结果是百度经纬度,默认值gcj02  
  38.         option.setScanSpan(5000);//设置发起请求的时间间隔为5000ms  
  39.         option.disableCache(true);//禁止开启缓存定位  
  40.         option.setPoiNumber(5);//最多返回POI个数  
  41.         option.setPoiDistance(1000);//poi查询距离  
  42.         option.setPoiExtraInfo(true);//是否需要POI的电话和地址等详细信息  
  43.         mLocationClient.setLocOption(option);  
  44.         mLocationClient.start();  
  45.     }  
  46.       
  47.     /** 
  48.      * 初始化布局 
  49.      */  
  50.     public void initLayout(){  
  51.         Parent = new RelativeLayout(this);  
  52.         bottomLayout = new LinearLayout(this);  
  53.         bottomLayout.setId(10);  
  54.         contentTextView = new TextView(this);  
  55.         contentTextView.setText(R.string.content);  
  56.         localButton = new Button(this);  
  57.         localButton.setText(R.string.localrequest);  
  58.         localButton.setId(11);  
  59.         localButton.setOnClickListener(this);  
  60.         poiButton = new Button(this);  
  61.         poiButton.setText(R.string.poirequest);  
  62.         poiButton.setId(12);  
  63.         poiButton.setOnClickListener(this);  
  64.         notifyButton = new Button(this);  
  65.         notifyButton.setText(R.string.notify);  
  66.         notifyButton.setId(13);  
  67.         notifyButton.setOnClickListener(this);  
  68.         offlineButton = new Button(this);  
  69.         offlineButton.setText(R.string.offine);  
  70.         offlineButton.setId(14);  
  71.         offlineButton.setOnClickListener(this);  
  72.           
  73.         //設置底部佈局的button  
  74.         int disten = (Screen_width - dip2px(buttonWidth) * 4) / 5;  
  75.         LinearLayout.LayoutParams buttonInBottomLayoutParams = new LinearLayout.LayoutParams(dip2px(buttonWidth), dip2px(buttonHeight));  
  76.         buttonInBottomLayoutParams.leftMargin = disten;  
  77.         bottomLayout.addView(localButton, buttonInBottomLayoutParams);  
  78.         bottomLayout.addView(poiButton, buttonInBottomLayoutParams);  
  79.         bottomLayout.addView(notifyButton, buttonInBottomLayoutParams);  
  80.         bottomLayout.addView(offlineButton, buttonInBottomLayoutParams);  
  81.         RelativeLayout.LayoutParams bottomInParentLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);  
  82.         bottomInParentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
  83.         Parent.addView(bottomLayout, bottomInParentLayoutParams);  
  84.         //設置contentTextView佈局  
  85.         RelativeLayout.LayoutParams contentInParentLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);  
  86.         contentInParentLayoutParams.addRule(RelativeLayout.ABOVE, 10);  
  87.         Parent.addView(contentTextView, contentInParentLayoutParams);  
  88.           
  89.     }  
  90.       
  91.     @Override  
  92.     public void onClick(View v) {  
  93.         switch (v.getId()) {  
  94.         case 11:  
  95.             //发起定位请求。请求过程是异步的,定位结果在上面的监听函数onReceiveLocation中获取。  
  96.             if (mLocationClient != null && mLocationClient.isStarted()) {  
  97.                 mLocationClient.requestLocation();  
  98.             }else {  
  99.                 Log.e("LocSDK3""locClient is null or not started");  
  100.             }  
  101.             break;  
  102.         case 12:  
  103.             //发起POI查询请求。请求过程是异步的,定位结果在上面的监听函数onReceivePoi中获取  
  104.             if (mLocationClient != null && mLocationClient.isStarted()) {  
  105.                 mLocationClient.requestPoi();  
  106.             }  
  107.             break;  
  108.         case 13:  
  109.             if (!clickNotify) {  
  110.                     clickNotify = true;  
  111.                     //位置提醒最多提醒3次,3次过后将不再提醒。 假如需要再次提醒,或者要修改提醒点坐标,都可通过函数SetNotifyLocation()来实现  
  112.                     //位置提醒相关代码  
  113.                     mNotifyer = new NotifyLister();  
  114.                     mNotifyer.SetNotifyLocation(42.03249652949337,113.3129895882556,3000,"gps");//4个参数代表要位置提醒的点的坐标,具体含义依次为:纬度,经度,距离范围,坐标系类型(gcj02,gps,bd09,bd09ll)  
  115.                     mLocationClient.registerNotify(mNotifyer);  
  116.             }else {  
  117.                 clickNotify = false;  
  118.                 //取消位置提醒  
  119.                 mLocationClient.removeNotifyEvent(mNotifyer);  
  120.             }  
  121.             break;  
  122.         case 14:  
  123.             /* 
  124.              *  发起离线定位请求。请求过程是异步的,定位结果在上面的监听函数onReceiveLocation中获取。 
  125.              *  getLocTypte = BDLocation.TypteOfflineLocation || BDLocation.TypeOfflineLocationFail 
  126.              *  表示是离线定位请求返回的定位结果 
  127.              */  
  128.             if (mLocationClient != null && mLocationClient.isStarted()) {  
  129.                 mLocationClient.requestOfflineLocation();  
  130.             }  
  131.             break;  
  132.         }  
  133.     }  
  134.       
  135.     //获取屏幕的宽度,高度和密度以及dp / px  
  136.      public void getDisplayMetrics() {  
  137.         DisplayMetrics dm = new DisplayMetrics();  
  138.         dm = getApplicationContext().getResources().getDisplayMetrics();  
  139.         Screen_width = dm.widthPixels;  
  140.         Screen_height = dm.heightPixels;  
  141.         scale = getResources().getDisplayMetrics().density;  
  142.     }  
  143.            
  144.      public int dip2px(float dpValue) {    
  145.         return (int)(dpValue * scale + 0.5f);  
  146.      }  
  147.   
  148.      @Override  
  149.     public boolean onCreateOptionsMenu(Menu menu) {  
  150.         getMenuInflater().inflate(R.menu.local, menu);  
  151.         return true;  
  152.     }  
  153.   
  154.     @Override  
  155.     protected void onStop() {  
  156.         super.onStop();  
  157.         if (mLocationClient != null) {  
  158.             mLocationClient.stop();  
  159.             mLocationClient = null;  
  160.         }  
  161.     }  
  162.   
  163.     public class MyLocationListener implements BDLocationListener{  
  164.   
  165.         //接收异步返回的定位结果,参数是BDLocation类型参数  
  166.         @Override  
  167.         public void onReceiveLocation(BDLocation location) {  
  168.             if (location == null) {  
  169.                 return;  
  170.             }  
  171.             StringBuffer sb = new StringBuffer(256);  
  172.             sb.append("time: ");  
  173.             sb.append(location.getTime());  
  174.             sb.append("\nerror code: ");  
  175.             sb.append(location.getLocType());  
  176.             sb.append("\nlontitude: ");  
  177.             sb.append(location.getLongitude());  
  178.             sb.append("\nradius: ");  
  179.             sb.append(location.getRadius());  
  180.             if (location.getLocType() == BDLocation.TypeGpsLocation) {  
  181.                 sb.append("\nspedd: ");  
  182.                 sb.append(location.getSpeed());  
  183.                 sb.append("\nsatellite: ");  
  184.                 sb.append(location.getSatelliteNumber());  
  185.             }else if(location.getLocType() == BDLocation.TypeNetWorkLocation){  
  186.                 sb.append("\naddr: ");  
  187.                 sb.append(location.getAddrStr());  
  188.             }else if(location.getLocType() == BDLocation.TypeOffLineLocation || location.getLocType() == BDLocation.TypeOffLineLocationNetworkFail){  
  189.                   
  190.             }  
  191.             if (contentTextView != null) {  
  192.                 contentTextView.setText(sb.toString());  
  193.             }  
  194.         }  
  195.   
  196.         //接收异步返回的POI查询结果,参数是BDLocation类型参数  
  197.         @Override  
  198.         public void onReceivePoi(BDLocation arg0) {  
  199.               
  200.         }  
  201.     }  
  202.       
  203.     //BDNotifyListener实现  
  204.     public class NotifyLister extends BDNotifyListener{  
  205.         public void onNotify(BDLocationListener mListener, float distance){  
  206.             if (mVibrator == null) {  
  207.                 mVibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);  
  208.             }  
  209.             mVibrator.vibrate(1000);//振动提醒已到设定位置附近  
  210.         }  
  211.     }  
  212.     public LocationClient mLocationClient = null;  
  213.     LocationClientOption option;  
  214.     public BDLocationListener myListener;  
  215.     public NotifyLister mNotifyer;  
  216.     public Vibrator mVibrator;  
  217.       
  218.     private TextView contentTextView;  
  219.     private Button localButton;  
  220.     private Button poiButton;  
  221.     private Button notifyButton;  
  222.     private Button offlineButton;  
  223.     private RelativeLayout Parent;  
  224.     private LinearLayout bottomLayout;  
  225.       
  226.     public int Screen_width;  
  227.     public int Screen_height;  
  228.     public float scale;  
  229.     public int buttonWidth = 130;//dp  
  230.     public int buttonHeight = 50;//dp  
  231.       
  232.     public boolean clickNotify = false;  
  233.   
  234. }  

界面如下:



    
1 0
原创粉丝点击