问题描述
- Fragment无法进行高德定位,求解决!
-
同样的方法在Activity里可以正常显示,但在Fragment里只加载出了地图,无法显示定位。应该是Fragment的问题。求大神教育!
public class ParkFragment extends Fragment implements
LocationSource AMapLocationListener OnCheckedChangeListener{private MapView mapView; private AMap aMap; private OnLocationChangedListener mListener; private LocationManagerProxy mAMapLocationManager; private ImageView imglist; @Override public View onCreateView(LayoutInflater inflater ViewGroup container Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab01 container false); imglist = (ImageView) view.findViewById(R.id.imglist); imglist.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity() ParklistActivity.class); startActivity(intent); } }); //在onCreate方法中给aMap对象赋值 mapView = (MapView) view.findViewById(R.id.map); mapView.onCreate(savedInstanceState);// 必须要写 aMap = mapView.getMap(); return view; } @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); init(); } /** * 初始化AMap对象 */ public void init() { if (aMap == null) { aMap = mapView.getMap(); setUpMap(); } } public void setUpMap() { aMap.setLocationSource((LocationSource) getActivity());// 设置定位监听 aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示 aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE); } /** * 此方法需存在 */ @Override public void onResume() { super.onResume(); mapView.onResume(); } /** * 此方法需存在 */ @Override public void onPause() { super.onPause(); mapView.onPause(); deactivate(); } /** * 方法必须重写 */ @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } /** * 此方法需存在 */ @Override public void onDestroy() { super.onDestroy(); mapView.onDestroy(); } @Override public void onLocationChanged(Location location) { } /** * 定位成功后回调函数 */ @Override public void onLocationChanged(AMapLocation amapLocation) { if (mListener != null && amapLocation != null) { if (amapLocation!=null&&amapLocation.getAMapException().getErrorCode() == 0) { mListener.onLocationChanged(amapLocation);// 显示系统小蓝点 } } } /** * 激活定位 */ @Override public void activate(OnLocationChangedListener listener) { mListener = listener; if (mAMapLocationManager == null) { mAMapLocationManager = LocationManagerProxy.getInstance(getActivity()); //此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗, //注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求 //在定位结束后,在合适的生命周期调用destroy()方法 //其中如果间隔时间为-1,则定位只定一次 mAMapLocationManager.requestLocationData( LocationProviderProxy.AMapNetwork 60*1000 10 this); } } /** * 停止定位 */ @Override public void deactivate() { mListener = null; if (mAMapLocationManager != null) { mAMapLocationManager.removeUpdates(this); mAMapLocationManager.destroy(); } mAMapLocationManager = null; } @Override public void onStatusChanged(String provider int status Bundle extras) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onCheckedChanged(RadioGroup group int checkedId) { // TODO Auto-generated method stub }
解决方案
可以 SupportMapFragment和MapFragment
时间: 2024-09-21 20:25:58