高德地图在Fragment中展示,并实现定位功能
来源:互联网 发布:黑暗之光魔翼升阶数据 编辑:程序博客网 时间:2024/06/06 01:07
高德地图在Fragment中展示,并实现定位功能
注:
Fragment之间进行切换的时候,会出现不能定位
在Fragment生命周期方法,onDestroy中这样写
mLocationOption = null;mLocationClient = null;mMap = null;mapView.onDestroy()
示例:
public class CurrentLocationFragment extends Fragment implements AMapLocationListener, LocationSource { private Activity mActivity; private AMap mMap; private MapView mapView; private OnLocationChangedListener mListener; private AMapLocationClient mLocationClient; private AMapLocationClientOption mLocationOption; private LatLng myLocation; private BitmapDescriptor successDescripter; private EditText etAddress; private TextView tvAddressName; private LinearLayout llOk; private View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LogUtils.d("zh", "onCreateView进来了"); mActivity = getActivity(); view = View.inflate(mActivity, R.layout.fragment_current_location, null); mapView = (MapView) view.findViewById(R.id.map_view); etAddress = (EditText) view.findViewById(R.id.et_address); tvAddressName = (TextView) view.findViewById(R.id.tv_address_name); llOk = (LinearLayout) view.findViewById(R.id.ll_ok); mapView.onCreate(savedInstanceState); initMap(); setUpLocationStyle(); initListener(); return view; } /** * 监听 */ private void initListener() { llOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String signedAddress = etAddress.getText().toString().trim();//签到地址 String addressName = tvAddressName.getText().toString().trim();//定位地址名称 if (TextUtils.isEmpty(signedAddress)){ ToastUtils.showToast("请输入签到地点"); return; } } }); } private void initMap() { if (mMap == null) { mMap = mapView.getMap(); } mMap.setLocationSource(this);// 设置定位监听 mMap.setMyLocationEnabled(true); UiSettings uiSettings = mMap.getUiSettings(); uiSettings.setZoomControlsEnabled(true); CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(15); mMap.moveCamera(cameraUpdate); successDescripter = BitmapDescriptorFactory.fromResource(R.drawable.gps_point); } private void setUpLocationStyle() { // 自定义系统定位蓝点 MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.gps_point)); myLocationStyle.strokeWidth(0); myLocationStyle.radiusFillColor(Color.TRANSPARENT); mMap.setMyLocationStyle(myLocationStyle); } @Override public void onResume() { super.onResume(); LogUtils.d("zh", "onResume进来了"); mapView.onResume(); } @Override public void onPause() { super.onPause(); LogUtils.d("zh", mapView + ":onPause"); mapView.onPause(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); LogUtils.d("zh", mapView + ":onSaveInstanceState"); mapView.onSaveInstanceState(outState); } @Override public void onDestroy() { super.onDestroy(); LogUtils.d("zh", mapView + ":onDestroy进来了"); mLocationOption = null; mLocationClient = null; mMap = null; mapView.onDestroy(); } @Override public void onLocationChanged(AMapLocation aMapLocation) { LogUtils.d("zh", "onLocationChanged进来了"); LogUtils.d("zh", aMapLocation + ""); LogUtils.d("zh", aMapLocation.getErrorCode() + ""); if (aMapLocation != null && aMapLocation.getErrorCode() == 0) { if (mListener != null) { mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点 } //获取当前经纬度坐标 String address = aMapLocation.getAddress(); LogUtils.d("zh",address); myLocation = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude()); //fixedMarker(); tvAddressName.setText(address); } } @Override public void activate(OnLocationChangedListener onLocationChangedListener) { LogUtils.d("zh", "activate进来了"); mListener = onLocationChangedListener; if (mLocationClient == null) { mLocationClient = new AMapLocationClient(mActivity); mLocationOption = new AMapLocationClientOption(); //设置定位监听 mLocationClient.setLocationListener(this); //设置为高精度定位模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位参数 mLocationOption.setOnceLocation(true);//只定位一次 mLocationOption.setHttpTimeOut(2000); mLocationClient.setLocationOption(mLocationOption); // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗, // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求 // 在定位结束后,在合适的生命周期调用onDestroy()方法 // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除 mLocationClient.startLocation();//开始定位 } } @Override public void deactivate() { LogUtils.d("zh", "deactivate我是什么时候进来的"); mListener = null; if (mLocationClient != null) { mLocationClient.stopLocation(); mLocationClient.onDestroy(); } mLocationClient = null; }}
0 0
- 高德地图在Fragment中展示,并实现定位功能
- 利用高德地图实现定位功能
- 高德地图定位功能
- 高德地图定位功能
- 高德地图显示在fragment中并添加按钮点击事件
- 高德地图显示在fragment中并添加按钮点击事件
- 高德地图的定位功能的实现
- Android 高德地图定位功能的实现步骤
- android高德地图定位功能的实现
- iOS获取高德地图实现持续定位功能
- Android百度地图定位收索取周边在列表中展示并选择
- [代码片段] Android百度地图定位收索取周边在列表中展示并选择
- 实现高德地图定位
- 高德地图实现定位
- 高德地图01---在activity中展示高德地图
- 设置高德地图在Fragment中显示
- 设置高德地图在Fragment中显示
- 高德地图定位功能(笔记)
- Libsvm的安装使用--MATLAB总结
- 学习一下MVP模式
- linux下进度条
- Freemaker FTL指令常用标签及语法
- 代码很烂,所以离职?
- 高德地图在Fragment中展示,并实现定位功能
- 解决 A child container failed during start
- 快速原型设计工具--Axure RP
- Centos7系统修改主机名
- R语言学习笔记(二)
- git 查看远程分支、本地分支、创建分支、把分支推到远程repository、删除本地分支
- Linux--RH124---unit 5 用户
- eclipse的项目分组功能
- hibernate 使用HQL语句进行多表联查