一个百度定位例子,把自己的经纬度转换为具体位置

喜欢的童鞋点击下载安装:http://www.apkbus.com/android-95644-1-1.html

一个百度定位例子,把自己的经纬度转换为具体位置
程序:

BaiduMap主包:

  1 package com.xmb.BaiduMap;

  2 

  3 import com.baidu.mapapi.BMapManager;

  4 import com.baidu.mapapi.GeoPoint;

  5 import com.baidu.mapapi.LocationListener;

  6 import com.baidu.mapapi.MKAddrInfo;

  7 import com.baidu.mapapi.*;

  8 import com.baidu.mapapi.MKDrivingRouteResult;

  9 import com.baidu.mapapi.MKGeneralListener;

 10 import com.baidu.mapapi.MKPoiResult;

 11 import com.baidu.mapapi.MKSearch;

 12 import com.baidu.mapapi.MKSearchListener;

 13 

 14 import com.baidu.mapapi.MKTransitRouteResult;

 15 import com.baidu.mapapi.MKWalkingRouteResult;

 16 import com.baidu.mapapi.MapActivity;

 17 import com.baidu.mapapi.MapController;

 18 import com.baidu.mapapi.MapView;

 19 

 20 

 21 

 22 

 23 

 24 

 25 import android.app.Activity;

 26 import android.graphics.drawable.Drawable;

 27 import android.location.Location;

 28 import android.os.Bundle;

 29 import android.widget.TextView;

 30 import android.widget.Toast;

 31 

 32 public class BaiduMap extends Activity {      

 33      public TextView tv1, tv2;

 34     public BMapManager mapManager;

 35     @Override  

 36     public void onCreate(Bundle savedInstanceState) {  

 37         super.onCreate(savedInstanceState);  

 38         setContentView(R.layout.mylocation);  

 39         tv1=(TextView)findViewById(R.id.result1);

 40         tv2 = (TextView) this.findViewById(R.id.result2);

 41         

 42         mapManager=new BMapManager(this);

 43         

 44         mapManager.init("53351EE4BDE7BD870F41A0B4AF1480F1CA97DAF9", new MyMKGeneralListener());

 45         

 46         

 47         mapManager.getLocationManager().setNoitifyInternal(20, 5);

 48 

 49 

 50         mapManager.getLocationManager().requestLocationUpdates(new MyLocationListener());

 51         

 52         mapManager.start();

 53         

 54         

 55 //      mapManager = new BMapManager(this);

 56 //        mapManager.init("EDB67AD764D300895C95ABA02A4DDC58D5485CCD",

 57 //                new MyMKGeneralListener());

 58 //        // 设置通知间隔:iMaxSecond - 最大通知间隔,单位:秒;iMinSecond - 最小通知间隔,单位:秒

 59 //        mapManager.getLocationManager().setNotifyInternal(20, 5);

 60 //

 61 //        mapManager.getLocationManager().requestLocationUpdates(

 62 //                new MyLocationListener());

 63 //        mapManager.start();

 64         

 65     }  

 66     // 定位自己的位置,只定位一次  

 67     class MyLocationListener implements LocationListener {

 68 

 69         @Override

 70         public void onLocationChanged(Location arg0) {

 71             

 72             double jindu1 = arg0.getLatitude();

 73             double weidu1 = arg0.getLongitude();

 74             

 75             int jindu = (int) (arg0.getLatitude()*1000000);

 76             int weidu = (int) (arg0.getLongitude()*1000000);

 77             tv1.setText("经度:" + jindu1 + ",纬度:" + weidu1);

 78             System.out.println("经度:" + jindu1 + ",纬度:" + weidu1);

 79             MKSearch search = new MKSearch();

 80             search.init(mapManager, new MyMKSearchListener());

 81             search.reverseGeocode(new GeoPoint(jindu, weidu));

 82         }

 83 

 84     }

 85 

 86     class MyMKSearchListener implements MKSearchListener {

 87 

 88         @Override

 89         public void onGetAddrResult(MKAddrInfo arg0, int arg1) {

 90             if (arg0 == null) {

 91                 tv2.setText("没有获取想要的位置");

 92             } else {

 93                 GeoPoint point = arg0.geoPt;

 94                 tv2.setText("地址:" + arg0.strAddr + ",坐标:"

 95                         + point.getLatitudeE6() + "," + point.getLongitudeE6());

 96             }

 97         }

 98 

 99         @Override

100         public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {

101             // TODO Auto-generated method stub

102             

103         }

104 

105         @Override

106         public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {

107             // TODO Auto-generated method stub

108             

109         }

110 

111         @Override

112         public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {

113             // TODO Auto-generated method stub

114             

115         }

116 

117         @Override

118         public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {

119             // TODO Auto-generated method stub

120             

121         }

122 

123         

124 

125     }

126 

127     // 常用事件监听,用来处理通常的网络错误,授权验证错误等

128     class MyMKGeneralListener implements MKGeneralListener {

129 

130         @Override

131         public void onGetNetworkState(int arg0) {

132 

133         }

134 

135         @Override

136         public void onGetPermissionState(int arg0) {

137 

138         }

139 

140     }

141     }

BMapApiDemoApp包:

 1 package com.xmb.BaiduMap;

 2 

 3 import android.app.Application;

 4 import android.widget.Toast;

 5 

 6 import com.baidu.mapapi.*;

 7 

 8 public class BMapApiDemoApp extends Application {

 9     

10     static BMapApiDemoApp mDemoApp;

11     

12     //百度MapAPI的管理类

13     BMapManager mBMapMan = null;    

14     // 授权Key

15     // TODO: 请输入您的Key,

16     // 申请地址:http://dev.baidu.com/wiki/static/imap/key/

17     String mStrKey = "53351EE4BDE7BD870F41A0B4AF1480F1CA97DAF9";

18     boolean m_bKeyRight = true;    // 授权Key正确,验证通过

19     

20     // 常用事件监听,用来处理通常的网络错误,授权验证错误等

21     static class MyGeneralListener implements MKGeneralListener {

22         @Override

23         public void onGetNetworkState(int iError) {

24             Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(), "您的网络出错啦!",

25                     Toast.LENGTH_LONG).show();

26         }

27 

28         @Override

29         public void onGetPermissionState(int iError) {

30             if (iError ==  MKEvent.ERROR_PERMISSION_DENIED) {

31                 // 授权Key错误:

32                 Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(), 

33                         "请在BMapApiDemoApp.java文件输入正确的授权Key!",

34                         Toast.LENGTH_LONG).show();

35                 BMapApiDemoApp.mDemoApp.m_bKeyRight = false;

36             }

37         }        

38     }    

39     @Override

40     public void onCreate() {

41         mDemoApp = this;

42         mBMapMan = new BMapManager(this);

43         mBMapMan.init(this.mStrKey, new MyGeneralListener());

44         super.onCreate();

45     }

46     @Override

47     //建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗

48     public void onTerminate() {

49         // TODO Auto-generated method stub

50         if (mBMapMan != null) {

51             mBMapMan.destroy();

52             mBMapMan = null;

53         }

54         super.onTerminate();

55     }

56 

57 }

xml文件就只是一个TextView控件,清单注意加权限的哦

你可能感兴趣的:(百度)