1. 定义对象
LocationClient mLocationClient = null; BDLocationListener myListener = new MyLocationListener(); LocationData locData = null;
2. 新建MyLocationListener类 实现BDLocationListener接口
public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location == null) return; mLocationClient.stop(); 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()); sb.append("\nlocation.getAltitude():" + location.getAltitude()); 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("\naddr : "); sb.append(location.getAddrStr()); } System.out.println(sb.toString()); } public void onReceivePoi(BDLocation poiLocation) { // 将在下个版本中去除poi功能 } }
3.
public void MyLocation() { mLocationClient = new LocationClient(this.getApplicationContext()); locData = new LocationData(); mLocationClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);// 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(1000);// 设置发起定位间隔时间
mLocationClient.setLocOption(option); mLocationClient.start(); if (mLocationClient != null && mLocationClient.isStarted()) { mLocationClient.requestLocation(); } else Log.d("LocSDK3", "locClient is null or not started"); }