Android 获取经纬度,地理位置,省市区
申請百度key:http://lbsyun.baidu.com/
?
?
1、jar包下載地址:https://pan.baidu.com/s/1J-boj0ct9oJ8YjXMR8X4KA
下載并復制到libs下,Add As Library
?
如需獲取SHA1值:https://blog.csdn.net/meixi_android/article/details/72547966
2、jar包復制到libs文件下,并Add As library
3、Java代碼
?
static BDLocation lastLocation = null; private LocationClient mLocClient; public MyLocationListenner myListener = new MyLocationListenner();//自定義方法 public class MyLocationListenner implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {if (location == null) {return;}if (lastLocation != null) {if (lastLocation.getLatitude() == location.getLatitude() && lastLocation.getLongitude() == location.getLongitude()) {Log.d("map", "same location, skip refresh");// mMapView.refresh(); //need this refresh?return;}}String addrlg; //定位結果lastLocation = location;if (!TextUtils.isEmpty(lastLocation.getLocationDescribe())){addrlg = lastLocation.getLocationDescribe();}else if (lastLocation.hasAddr()) {addrlg = lastLocation.getAddrStr();}else {addrlg = "定位失敗...";return;} // String city = lastLocation.getCity();double lat = lastLocation.getLatitude();double lot = lastLocation.getLongitude();ShareUtil.sharedPstring("nowla",String.valueOf(lat));ShareUtil.sharedPstring("nowlo",String.valueOf(lot));ShareUtil.sharedPstring("nowad",addrlg);Log.i("lgq","............"+addrlg+"........"+lat+"......."+lot); // Toast.makeText(MainActivity.this, addrlg+"........"+lat, Toast.LENGTH_SHORT).show(); // tv_bottom_bar_me.setText(addrlg); // mBaiduMap.animateMapStatus(u);}}?附:
?sb.append("\nlocType : ");// 定位類型
? ? ? ? ?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());
? ? ? ? ?sb.append("\nCountryCode : ");// 國家碼
? ? ? ? ?sb.append(location.getCountryCode());
? ? ? ? ?sb.append("\nCountry : ");// 國家名稱
? ? ? ? ?sb.append(location.getCountry());
? ? ? ? ?sb.append("\ncitycode : ");// 城市編碼
? ? ? ? ?sb.append(location.getCityCode());
? ? ? ? ?sb.append("\ncity : ");// 城市
? ? ? ? ?sb.append(location.getCity());
? ? ? ? ?sb.append("\nDistrict : ");// 區
? ? ? ? ?sb.append(location.getDistrict());
? ? ? ? ?sb.append("\nStreet : ");// 街道
? ? ? ? ?sb.append(location.getStreet());
? ? ? ? ?sb.append("\naddr : ");// 地址信息
? ? ? ? ?sb.append(location.getAddrStr());
? ? ? ? ?sb.append("\nDirection(not all devices have value): ");
? ? ? ? ?sb.append(location.getDirection());// 方向
? ? ? ? ?sb.append("\nlocationdescribe: ");
? ? ? ? ?sb.append(location.getLocationDescribe());// 位置語義化信息
? ? ? ? ?sb.append("\nPoi: ");// POI信息
?
4、運行
?
@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showMapWithLocationClient(); }權限
?
<uses-permission android:name="android.permission.ACCESS_GPS" /> <!-- 獲取精確gps位置 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 獲取粗略位置 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 允許程序訪問額外的定位提供者指令獲取模擬定位信息 --> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <!-- 網絡鏈接 --> <uses-permission android:name="android.permission.INTERNET" />?6.0以上版本獲取權限:https://blog.csdn.net/meixi_android/article/details/82114026
?
配置百度服務到manifest
<serviceandroid:name="com.baidu.location.f" android:enabled="true"android:process=":remote"> </service>配置key
<meta-dataandroid:name="com.baidu.lbsapi.API_KEY"android:value="qn3ZT2xT7yDgbIgkaPooGpDdMjziASZj" />?
?
谷歌API也可以實現啦:https://blog.csdn.net/meixi_android/article/details/84955589
總結
以上是生活随笔為你收集整理的Android 获取经纬度,地理位置,省市区的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视频教程-使用 Pandas 与 Mat
- 下一篇: Android 使用ViewPager实