Blue-Vision 2015-08-11 08:52 采纳率: 0%
浏览 2910

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

    }
  • 写回答

1条回答 默认 最新

  • uncle爱吃毛毛虫 2015-11-28 08:01
    关注

    可以 SupportMapFragment和MapFragment

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作