1. 注意 key 一定要在activity 前面
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > </service> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="6dsXhPU5WWHcBxGwQtHZKIGfZZBorMZ5" />
2 .xml 文件
<com.baidu.mapapi.map.MapView android:id="@+id/map_view" android:layout_width="wrap_content" android:layout_height="wrap_content"> </com.baidu.mapapi.map.MapView> 3. java文件
public class LoactionView extends AppCompatActivity { MapView mMapView; BaiduMap mBaiduMap; LocationClient mLocationClient; LocationClientOption mOption; boolean isFirstLoc = true;// 是否首次定位 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_loaction_view); mMapView = (MapView) findViewById(R.id.map_view); mBaiduMap = mMapView.getMap(); mBaiduMap.setMyLocationEnabled(true); mLocationClient = new LocationClient(getApplicationContext()); mLocationClient.setLocOption(getDefaultLocationClientOption()); mLocationClient.registerLocationListener(mBDLocationListener); mLocationClient.start(); mMapView.refreshDrawableState(); } /*** * * @return DefaultLocationClientOption */ public LocationClientOption getDefaultLocationClientOption(){ mOption = new LocationClientOption(); mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 mOption.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系,如果配合百度地图使用,建议设置为bd09ll; //mOption.setScanSpan(0);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 mOption.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 mOption.setIsNeedLocationDescribe(true);//可选,设置是否需要地址描述 mOption.setNeedDeviceDirect(false);//可选,设置是否需要设备方向结果 mOption.setLocationNotify(false);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果 mOption.setIgnoreKillProcess(true);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死 mOption.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” mOption.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 mOption.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集 return mOption; } private BDLocationListener mBDLocationListener = new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { if (location == null || mMapView == null) return; MyLocationData locData = new MyLocationData.Builder(). accuracy(location.getRadius()).direction(100).latitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); //地图标注 BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo); OverlayOptions options = new MarkerOptions().position(ll).icon(bitmapDescriptor); mBaiduMap.addOverlay(options); /* * 标绘圆 * */ CircleOptions circleOptions = new CircleOptions(); circleOptions.center(ll);//设置圆心坐标 circleOptions.fillColor(0Xaafaa355);//圆的填充颜色 circleOptions.fillColor(0Xaafaa355);//圆的填充颜色 circleOptions.radius(400);//设置半径 circleOptions.stroke(new Stroke(2, 0xAA00FF00));//设置边框 mBaiduMap.addOverlay(circleOptions); MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 16); //设置地图中心点以及缩放级别 //MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); mBaiduMap.animateMapStatus(u); } // TODO Auto-generated method stub if (null != location && location.getLocType() != BDLocation.TypeServerError) { StringBuffer sb = new StringBuffer(256); sb.append("time : "); /** * 时间也可以使用systemClock.elapsedRealtime()方法 获取的是自从开机以来,每次回调的时间; * location.getTime() 是指服务端出本次结果的时间,如果位置不发生变化,则时间不变 */ 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("\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("\nDescribe: "); sb.append(location.getLocationDescribe()); sb.append("\nDirection(not all devices have value): "); sb.append(location.getDirection()); sb.append("\nPoi: "); if (location.getPoiList() != null && !location.getPoiList().isEmpty()) { for (int i = 0; i < location.getPoiList().size(); i++) { Poi poi = (Poi) location.getPoiList().get(i); sb.append(poi.getName() + ";"); } } if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果 sb.append("\nspeed : "); sb.append(location.getSpeed());// 单位:km/h sb.append("\nsatellite : "); sb.append(location.getSatelliteNumber()); sb.append("\nheight : "); sb.append(location.getAltitude());// 单位:米 sb.append("\ndescribe : "); sb.append("gps定位成功"); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果 // 运营商信息 sb.append("\noperationers : "); sb.append(location.getOperators()); sb.append("\ndescribe : "); sb.append("网络定位成功"); } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果 sb.append("\ndescribe : "); sb.append("离线定位成功,离线定位结果也是有效的"); } else if (location.getLocType() == BDLocation.TypeServerError) { sb.append("\ndescribe : "); sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到[email protected],会有人追查原因"); } else if (location.getLocType() == BDLocation.TypeNetWorkException) { sb.append("\ndescribe : "); sb.append("网络不同导致定位失败,请检查网络是否通畅"); } else if (location.getLocType() == BDLocation.TypeCriteriaException) { sb.append("\ndescribe : "); sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机"); } logMsg(sb.toString()); } } }; /** * 显示请求字符串 * * @param str */ public void logMsg(String str) { Log.e("msg", str); /* try { if (LocationResult != null) LocationResult.setText(str); } catch (Exception e) { e.printStackTrace(); }*/ } // 三个状态实现地图生命周期管理 @Override protected void onResume() { super.onResume(); mMapView.onResume(); } @Override protected void onPause() { super.onPause(); mMapView.onPause(); } @Override protected void onDestroy() { mLocationClient.unRegisterLocationListener(mBDLocationListener); mLocationClient.stop(); mBaiduMap.setMyLocationEnabled(false); super.onDestroy(); mMapView.onDestroy(); mMapView = null; }}
时间: 2024-11-05 19:46:55