目前在国内使用定位的方法主要是

1. Android系统提供的LocationManagerlocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

2. 百度提供的api(需要在应用程序中加入相应的.so,.jar包)百度提供的定位api,与你本机是否有装百度地图程序程序没有关系


下面简单介绍一下使用方法


Android系统提供的API使用:

LocationManager locationManager ;void getLocationInfo() {if(locationManager == null){locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);}Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_COARSE); // 设置为最大精度criteria.setAltitudeRequired(false); // 不要求海拔信息criteria.setCostAllowed(true);//是否允许付费criteria.setPowerRequirement(Criteria.POWER_LOW); // 对电量的要求criteria.setBearingRequired(false); // 不要求Bearing信息String bestProvider = locationManager.getBestProvider(criteria, true);IWLog.i(TAG, "bestProvider=" + bestProvider);Location location = locationManager.getLastKnownLocation(bestProvider);updateWithNewLocation(location);    locationManager.requestLocationUpdates(bestProvider, 1000, 2, mLocationListener);//1秒,2米}LocationListener mLocationListener = new LocationListener() {@Overridepublic void onLocationChanged(Location location) {if(locationManager != null){locationManager.removeUpdates(mLocationListener);//我这里,只需要定位一次就可以了}updateWithNewLocation(location);}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {IWLog.i(TAG, "onStatusChanged");}@Overridepublic void onProviderEnabled(String provider) {IWLog.i(TAG, "onProviderEnabled");}@Overridepublic void onProviderDisabled(String provider) {IWLog.i(TAG, "onProviderDisabled");}};private void updateWithNewLocation(Location location){if (location != null) {double latitude = location.getLatitude(); // 经度double longitude = location.getLongitude(); // 纬度//double altitude = location.getAltitude(); // 海拔///mMainFragment.navLoadUrl(String.format(NavigationUrl.NEARBY_URL,longitude,latitude));IWLog.v(TAG, "latitude " + latitude + "  longitude:" + longitude);///UtilWidget.showToast(this, "Latitude :" + location.getLatitude()+""+"Longitude :" +  location.getLatitude());}else{IWLog.v(TAG, "don't know location info");UtilWidget.showToast(this, "无法获取位置信息");}}


百度定位API

百度官方说明:http://api.map.baidu.com/lbsapi/cloud/geosdk-android.htm

下载包及demo:http://api.map.baidu.com/lbsapi/cloud/geosdk-android-download.htm


可以直接参考百度提供的demo来实现自己的定位

MainActivity中的代码:

/** * 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中 */public class MyLocationListenner implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {if (location == null)return ;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());} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){//sb.append("\n省:");//sb.append(location.getProvince());//sb.append("\n市:");//sb.append(location.getCity());//sb.append("\n区/县:");//sb.append(location.getDistrict());sb.append("\naddr : ");sb.append(location.getAddrStr());}sb.append("\nsdk version : ");sb.append(mLocationClient.getVersion());sb.append("\nisCellChangeFlag : ");sb.append(location.isCellChangeFlag());//logMsg(sb.toString());Log.i(TAG, sb.toString());UtilWidget.cancelProgressDialog();updateWithNewLocation(location);}public void onReceivePoi(BDLocation poiLocation) {if (poiLocation == null){return ; }StringBuffer sb = new StringBuffer(256);sb.append("Poi time : ");sb.append(poiLocation.getTime());sb.append("\nerror code : "); sb.append(poiLocation.getLocType());sb.append("\nlatitude : ");sb.append(poiLocation.getLatitude());sb.append("\nlontitude : ");sb.append(poiLocation.getLongitude());sb.append("\nradius : ");sb.append(poiLocation.getRadius());if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){sb.append("\naddr : ");sb.append(poiLocation.getAddrStr());} if(poiLocation.hasPoi()){sb.append("\nPoi:");sb.append(poiLocation.getPoi());}else{sb.append("noPoi information");}//logMsg(sb.toString());}}//设置相关参数public void setLocationOption() {LocationClientOption option = new LocationClientOption();option.setOpenGps(true);// 打开gprs// option.setAddrType("all");// 返回的定位结果包含地址信息option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02option.setScanSpan(5);// 设置发起定位请求的间隔时间为5000ms// //设置定位模式,小于1秒则一次定位;大于等于1秒则定时定位option.disableCache(true);// 禁止启用缓存定位// option.setPoiNumber(5); // 最多返回POI个数// option.setPoiDistance(1000); // poi查询距离// /option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息// option.setPriority(LocationClientOption.NetWorkFirst); //设置网络优先// option.setPriority(LocationClientOption.GpsFirst); //不设置,默认是gps优先mLocationClient.setLocOption(option);}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main_layout);//百度定位服务mLocationClient = new LocationClient(this);//myListener = new MyLocationListenner();setLocationOption();mLocationClient.registerLocationListener( new MyLocationListenner());}@Overrideprotected void onDestroy() {super.onDestroy();mLocationClient.stop();}在需要开户定位的地方执行 mLocationClient.start();


   
   
   
详细参数请到官试api上参考

最后想说的一句是,在国内可能使用百度定位更好一些。因为我用系统的api很难达到想要的结果

更多相关文章

  1. 【Android】悬浮按钮(FloatingActionButton)
  2. Android中文API——DatePicker&TimePicker
  3. 【ArcGIS for Android】使用自定义Callout显示地图信息
  4. Android测试之Monkey原理及源码分析(一)
  5. Android(安卓)GridView 滑动条设置一直显示状态(推荐)
  6. Android——camera摄像头
  7. 定义Window进入和退出效果(及Window,Activity,View的理解)
  8. 学习笔记:Android中使用网络技术的方法(webview、httpurlconnectio
  9. android app设置亮屏的几种方式以及功耗对比

随机推荐

  1. 用ndk环境编译android可用的so库
  2. android进行主题切换不重启整个应用(styl
  3. 【Android(安卓)内存优化】Bitmap 硬盘缓
  4. 【Android】Android江湖
  5. android告别篇-对于源码我的一些看法
  6. 2019零基础学Android第0课——零基础怎么
  7. 谁说Android的动画不廉价(一)之项目分层
  8. android 中Xml里面的id重名问题
  9. Android中Drawable适配介绍
  10. 【Android实战之旅 001】AsyncTask异步操