1、下载jar
https://pan.baidu.com/s/1Y_OEs3hFrqsa7Vn7fUKzZw
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 { @Override public 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); } }
private void showMapWithLocationClient() { mLocClient = new LocationClient(this); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);// open gps option.setCoorType("bd09ll"); //可选,默认0,即仅定位一次,设置定时发起定位请求的间隔需要大于等于1000ms才是有效的 /* * 定位sdk提供2种定位模式,定时定位和app主动请求定位。 * setScanSpan < 1000 则为 app主动请求定位; * setScanSpan >=1000,则为定时定位模式(setScanSpan的值就是定时定位的时间间隔)) * 定时定位模式中,定位sdk会按照app设定的时间定位进行位置更新,定时回调定位结果。此种定位模式适用于希望获得连续定位结果的情况。 * 对于单次定位类应用,或者偶尔需要一下位置信息的app,可采用app主动请求定位这种模式。*/ //option.setScanSpan(2000); //可选,设置是否需要地址信息,默认不需要 option.setIsNeedAddress(true); //可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationDescribe(true); //设置是否需要返回位置POI信息,可以在BDLocation.getPoiList()中得到数据 option.setIsNeedLocationPoiList(true); //在网络定位时,是否需要设备方向 true:需要 ; false:不需要 option.setNeedDeviceDirect(true); //可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死 option.setIgnoreKillProcess(false); //可选,默认false,设置是否收集CRASH信息,默认收集 option.SetIgnoreCacheException(false); //可选,默认false,设置是否需要过滤gps仿真结果,默认需要 option.setEnableSimulateGps(false); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); mLocClient.setLocOption(option); mLocClient.start(); }
4、运行
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showMapWithLocationClient(); }