喜欢的童鞋可点击下载:http://www.apkbus.com/android-95636-1-1.html
主程序:
jzdw包:
1 package com.xmb.jzdw; 2 3 import java.text.DateFormat; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 import android.app.Activity; 8 import android.location.Location; 9 import android.os.Bundle; 10 import android.os.Handler; 11 import android.os.Message; 12 import android.util.Log; 13 import android.widget.ProgressBar; 14 import android.widget.TextView; 15 16 import com.baidu.mapapi.BMapManager; 17 import com.baidu.mapapi.GeoPoint; 18 import com.baidu.mapapi.LocationListener; 19 import com.baidu.mapapi.MKAddrInfo; 20 import com.baidu.mapapi.MKBusLineResult; 21 import com.baidu.mapapi.MKDrivingRouteResult; 22 import com.baidu.mapapi.MKGeneralListener; 23 import com.baidu.mapapi.MKPoiResult; 24 import com.baidu.mapapi.MKSearch; 25 import com.baidu.mapapi.MKSearchListener; 26 import com.baidu.mapapi.MKSuggestionResult; 27 import com.baidu.mapapi.MKTransitRouteResult; 28 import com.baidu.mapapi.MKWalkingRouteResult; 29 30 31 32 public class Jzdw extends Activity { 33 public TextView myDate, jingdu,mylocation; 34 public BMapManager mapManager; 35 private String lng1; 36 private String lat1; 37 private String date; 38 private String Precision; 39 private Handler handler = new Handler() { 40 @Override 41 public void handleMessage(Message msg) { 42 if (msg.what == 1) { 43 Log.i("out","handlemessenge"); 44 DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 45 date = format1.format(new Date()); 46 myDate.setText(date); 47 Bundle data = msg.getData(); 48 // lng1 = data.getString("Longltude"); 49 lng1 = data.getString("Longltude"); 50 lat1 = data.getString("Latitude"); 51 //jzdwaddress = data.getString("jzdwaddress"); 52 Precision = data.getString("accuracy"); 53 jingdu.setText(Precision); 54 mylocation.setText("经度为:" + lng1 + "\n维度为:" + lat1); 55 56 }} 57 }; 58 59 @Override 60 public void onCreate(Bundle savedInstanceState) { 61 super.onCreate(savedInstanceState); 62 setContentView(R.layout.main); 63 myDate=(TextView)findViewById(R.id.t1); 64 jingdu=(TextView)findViewById(R.id.t3); 65 mylocation = (TextView) this.findViewById(R.id.t2); 66 new Thread(new Jzdw1(handler, this)).start(); 67 68 /* 69 mapManager=new BMapManager(this); 70 71 mapManager.init("471066e711cc4128a04b3e3b1930a422", new MyMKGeneralListener()); 72 73 74 mapManager.getLocationManager().setNotifyInternal(20, 5); 75 76 77 mapManager.getLocationManager().requestLocationUpdates(new MyLocationListener()); 78 79 mapManager.start(); 80 */ 81 82 // mapManager = new BMapManager(this); 83 // mapManager.init("EDB67AD764D300895C95ABA02A4DDC58D5485CCD", 84 // new MyMKGeneralListener()); 85 // // 设置通知间隔:iMaxSecond - 最大通知间隔,单位:秒;iMinSecond - 最小通知间隔,单位:秒 86 // mapManager.getLocationManager().setNotifyInternal(20, 5); 87 // 88 // mapManager.getLocationManager().requestLocationUpdates( 89 // new MyLocationListener()); 90 // mapManager.start(); 91 92 } 93 /* 94 // 定位自己的位置,只定位一次 95 class MyLocationListener implements LocationListener { 96 97 @Override 98 public void onLocationChanged(Location arg0) { 99 100 double jindu1 = arg0.getLatitude(); 101 double weidu1 = arg0.getLongitude(); 102 103 int jindu = (int) (arg0.getLatitude()*1000000); 104 int weidu = (int) (arg0.getLongitude()*1000000); 105 mylocation.setText("经度:" + jindu1 + ",纬度:" + weidu1); 106 System.out.println("1111经度:" + jindu1 + ",111纬度:" + weidu1); 107 MKSearch search = new MKSearch(); 108 search.init(mapManager, new MyMKSearchListener()); 109 search.reverseGeocode(new GeoPoint(jindu, weidu)); 110 } 111 112 } 113 114 class MyMKSearchListener implements MKSearchListener { 115 116 @Override 117 public void onGetAddrResult(MKAddrInfo arg0, int arg1) { 118 if (arg0 == null) { 119 mylocation.setText("没有获取想要的位置"); 120 } else { 121 GeoPoint point = arg0.geoPt; 122 mylocation.setText("地址:" + arg0.strAddr + ",坐标:" 123 + point.getLatitudeE6() + "," + point.getLongitudeE6()); 124 } 125 } 126 127 @Override 128 public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) { 129 // TODO Auto-generated method stub 130 131 } 132 133 @Override 134 public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) { 135 // TODO Auto-generated method stub 136 137 } 138 139 @Override 140 public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) { 141 // TODO Auto-generated method stub 142 143 } 144 145 @Override 146 public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) { 147 // TODO Auto-generated method stub 148 149 } 150 151 @Override 152 public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) { 153 // TODO Auto-generated method stub 154 155 } 156 157 @Override 158 public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) { 159 // TODO Auto-generated method stub 160 161 } 162 163 164 165 } 166 167 // 常用事件监听,用来处理通常的网络错误,授权验证错误等 168 class MyMKGeneralListener implements MKGeneralListener { 169 170 @Override 171 public void onGetNetworkState(int arg0) { 172 173 } 174 175 @Override 176 public void onGetPermissionState(int arg0) { 177 178 } 179 180 }*/ 181 }
jzdw1包:
1 package com.xmb.jzdw; 2 3 4 5 import com.baidu.mapapi.BMapManager; 6 import com.baidu.mapapi.GeoPoint; 7 import com.baidu.mapapi.LocationListener; 8 import com.baidu.mapapi.MKAddrInfo; 9 import com.baidu.mapapi.MKBusLineResult; 10 import com.baidu.mapapi.MKDrivingRouteResult; 11 import com.baidu.mapapi.MKGeneralListener; 12 import com.baidu.mapapi.MKPoiResult; 13 import com.baidu.mapapi.MKSearchListener; 14 import com.baidu.mapapi.MKSuggestionResult; 15 import com.baidu.mapapi.MKTransitRouteResult; 16 import com.baidu.mapapi.MKWalkingRouteResult; 17 18 import android.content.Context; 19 import android.location.Location; 20 import android.os.Bundle; 21 import android.os.Handler; 22 import android.os.Message; 23 import android.telephony.TelephonyManager; 24 import android.util.Log; 25 26 27 public class Jzdw1 extends Thread{ 28 private TelephonyManager tm; 29 private Handler handler; 30 private Context context; 31 Double lat; 32 Double lng; 33 public BMapManager mapManager; 34 35 public Jzdw1(Handler handler, Context context) { 36 37 this.handler = handler; 38 this.context = context; 39 40 mapManager=new BMapManager(context); 41 Log.i("out","gouzao"); 42 mapManager.init("471066e711cc4128a04b3e3b1930a422", new MyMKGeneralListener()); 43 mapManager.getLocationManager().setNotifyInternal(20, 5); 44 } 45 46 @Override 47 public void run() { 48 Log.i("out","run"); 49 mapManager.getLocationManager().requestLocationUpdates(new MyLocationListener()); 50 mapManager.start(); 51 } 52 53 class MyLocationListener implements LocationListener { 54 55 @Override 56 public void onLocationChanged(Location arg0) { 57 Integer jingdu = (int) (arg0.getLatitude()*1000000); 58 Integer weidu = (int) (arg0.getLongitude()*1000000); 59 Log.i("out","listener"); 60 // Integer jingdu = (int) (arg0.getLatitude()*1440000); 61 // Integer weidu = (int) (arg0.getLongitude()*1440000); 62 63 64 String Accuray=String.valueOf(arg0.getAccuracy()); 65 Bundle mydata=new Bundle(); 66 Message msg=new Message(); 67 mydata.putString("Longltude",String.valueOf(jingdu)); 68 mydata.putString("Latitude",String.valueOf(weidu)); 69 mydata.putString("accuracy",Accuray); 70 71 msg.setData(mydata); 72 msg.what=1; 73 handler.sendMessage(msg); 74 } 75 76 } 77 78 class MyMKSearchListener implements MKSearchListener { 79 80 @Override 81 public void onGetAddrResult(MKAddrInfo arg0, int arg1) { 82 if (arg0 == null) { 83 //tv2.setText("没有获取想要的位置"); 84 } else { 85 GeoPoint point = arg0.geoPt; 86 // t1.append("\n" + arg0.strAddr); 87 } 88 } 89 90 @Override 91 public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) { 92 93 } 94 95 @Override 96 public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) { 97 98 } 99 100 @Override 101 public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) { 102 103 } 104 105 @Override 106 public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) { 107 108 } 109 110 @Override 111 public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) { 112 113 } 114 115 @Override 116 public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) { 117 118 } 119 120 // @Override 121 // public void onGetRGCShareUrlResult(String arg0, int arg1) { 122 // // TODO Auto-generated method stub 123 // 124 // } 125 126 } 127 128 129 // 常用事件监听,用来处理通常的网络错误,授权验证错误等 130 class MyMKGeneralListener implements MKGeneralListener { 131 132 @Override 133 public void onGetNetworkState(int arg0) { 134 135 } 136 137 @Override 138 public void onGetPermissionState(int arg0) { 139 140 } 141 142 } 143 }
xml文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:id="@+id/t1" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="@string/hello" 12 /> 13 <TextView 14 android:id="@+id/t2" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:text="@string/hello" 18 /> 19 <TextView 20 android:id="@+id/t3" 21 android:layout_width="fill_parent" 22 android:layout_height="wrap_content" 23 android:text="@string/hello" 24 /> 25 </LinearLayout>
记得在清单文件中添加权限哦(万能版):
1 <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"></uses-permission> 2 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 3 <uses-permission android:name="android.permission.INTERNET"></uses-permission> 4 <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> 5 <uses-permission android:name="android.permission.SEND_SMS"></uses-permission> 6 <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> 7 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 8 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 9 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 10 <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission> 11 <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission> 12 <uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"></uses-permission> 13 <uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission> 14 <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"></uses-permission> 15 <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission> 16 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 17 <uses-permission android:name="android.permission.PERMISSION_NAME" /> 18 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 19 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 20 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 21 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 22 <uses-permission android:name="android.permission.VIBRATE" /> 23 <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" /> 24 <uses-permission android:name="android.permission.GET_TASKS" /> 25 <uses-permission android:name="android.permission.RESTART_PACKAGES" /> 26 <uses-permission android:name="android.permission.WAKE_LOCK" /> 27 28 <uses-permission android:name="android.permission.DEVICE_POWER" /> 29 30 31 <!-- 在SDCard中创建与删除文件权限 --> 32 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 33 <!-- 往SDCard写入数据权限 --> 34 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 35 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 36 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 37 <uses-permission android:name="android.permission.INTERNET"></uses-permission> 38 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 39 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 40 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> 41 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>