实例介绍
【实例简介】
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | package com.example.locationdemo; import java.util.Date; import android.app.Activity; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationListener; import com.amap.api.location.LocationManagerProxy; import com.amap.api.location.LocationProviderProxy; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.location.LocationClientOption.LocationMode; public class MainActivity extends Activity implements OnClickListener{ private TextView mTextView; private Button gpsBtn, baiduBtn, amapBtn; //gps private LocationManager gpsManager; //baidu private LocationClient baduduManager; //amap private LocationManagerProxy aMapManager; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.text); gpsBtn = (Button) findViewById(R.id.gps); baiduBtn = (Button) findViewById(R.id.baidu); amapBtn = (Button) findViewById(R.id.amap); gpsBtn.setOnClickListener( this ); baiduBtn.setOnClickListener( this ); amapBtn.setOnClickListener( this ); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.gps: if (gpsBtn.getText().toString().equals( "开启GPS定位" )) { startGps(); gpsBtn.setText( "停止GPS定位" ); } else { stopGps(); gpsBtn.setText( "开启GPS定位" ); } break ; case R.id.baidu: if (baiduBtn.getText().toString().equals( "开启百度定位" )) { startBaidu(); baiduBtn.setText( "停止百度定位" ); } else { stopBaidu(); baiduBtn.setText( "开启百度定位" ); } break ; case R.id.amap: if (amapBtn.getText().toString().equals( "开启高德定位" )) { startAmap(); amapBtn.setText( "停止高德定位" ); } else { stopAmap(); amapBtn.setText( "开启高德定位" ); } break ; default : break ; } } private void startAmap() { aMapManager = LocationManagerProxy.getInstance( this ); /* * mAMapLocManager.setGpsEnable(false); * 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location * API定位采用GPS和网络混合定位方式 * ,第一个参数是定位provider,第二个参数时间最短是2000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者 */ aMapManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 2000 , 10 , mAMapLocationListener); } private void stopAmap() { if (aMapManager != null ) { aMapManager.removeUpdates(mAMapLocationListener); aMapManager.destory(); } aMapManager = null ; } private void startBaidu() { if (baduduManager == null ) { baduduManager = new LocationClient( this ); //定位的配置 LocationClientOption option = new LocationClientOption(); //定位模式选择,高精度、省电、仅设备 option.setLocationMode(LocationMode.Hight_Accuracy); //定位坐标系类型选取, gcj02、bd09ll、bd09 option.setCoorType( "gcj02" ); //定位时间间隔 option.setScanSpan( 1000 ); //选择定位到地址 option.setIsNeedAddress( true ); baduduManager.setLocOption(option); //注册定位的成功的回调 baduduManager.registerLocationListener(mBdLocationListener); } baduduManager.start(); } private void stopBaidu() { baduduManager.stop(); } private void startGps() { // 获取到LocationManager对象 gpsManager = (LocationManager) getSystemService(LOCATION_SERVICE); //provider可为gps定位,也可为为基站和WIFI定位 String provider = gpsManager.getProvider(LocationManager.GPS_PROVIDER).getName(); //3000ms为定位的间隔时间,10m为距离变化阀值,gpsListener为回调接口 gpsManager.requestLocationUpdates(provider, 3000 , 10 , gpsListener); } private void stopGps() { gpsManager.removeUpdates(gpsListener); } // 创建位置监听器 private LocationListener gpsListener = new LocationListener() { // 位置发生改变时调用 @Override public void onLocationChanged(Location location) { Log.e( "Location" , "onLocationChanged" ); double latitude = location.getLatitude(); double longitude = location.getLongitude(); float speed = location.getSpeed(); long time = location.getTime(); String s = "latitude--->" latitude " longitude--->" longitude " speed--->" speed " time--->" new Date(time).toLocaleString(); mTextView.setText( "GPS定位\n" s); } // provider失效时调用 @Override public void onProviderDisabled(String provider) { Log.e( "Location" , "onProviderDisabled" ); } // provider启用时调用 @Override public void onProviderEnabled(String provider) { Log.e( "Location" , "onProviderEnabled" ); } // 状态改变时调用 @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.e( "Location" , "onStatusChanged" ); } }; private BDLocationListener mBdLocationListener = new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { //Receive Location StringBuffer sb = new StringBuffer( 256 ); sb.append( "time : " ); sb.append(location.getTime()); sb.append( "\nerror code : " ); sb.append(location.getLocType()); sb.append( "\nlatitude : " ); sb.append(location.getLatitude()); sb.append( "\nlontitude : " ); sb.append(location.getLongitude()); sb.append( "\nradius : " ); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation){ sb.append( "\nspeed : " ); sb.append(location.getSpeed()); sb.append( "\nsatellite : " ); sb.append(location.getSatelliteNumber()); sb.append( "\ndirection : " ); sb.append( "\naddr : " ); sb.append(location.getAddrStr()); sb.append(location.getDirection()); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ sb.append( "\naddr : " ); sb.append(location.getAddrStr()); sb.append( "\noperationers : " ); sb.append(location.getOperators()); } mTextView.setText( "百度定位\n" sb.toString()); } }; private AMapLocationListener mAMapLocationListener = new AMapLocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(Location location) { } @Override public void onLocationChanged(AMapLocation location) { if (location != null ) { Double geoLat = location.getLatitude(); Double geoLng = location.getLongitude(); String cityCode = "" ; String desc = "" ; Bundle locBundle = location.getExtras(); if (locBundle != null ) { cityCode = locBundle.getString( "citycode" ); desc = locBundle.getString( "desc" ); } String str = ( "定位成功:(" geoLng "," geoLat ")" "\n精 度 :" location.getAccuracy() "米" "\n定位方式:" location.getProvider() "\n定位时间:" new Date(location.getTime()).toLocaleString() "\n城市编码:" cityCode "\n位置描述:" desc "\n省:" location.getProvince() "\n市:" location.getCity() "\n区(县):" location.getDistrict() "\n区域编码:" location .getAdCode()); mTextView.setText( "高德定位\n" str); } } }; } |
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论