CompanyMainFragment.java 5.6 KB
package com.hh.xuetubao.fragment;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.hh.xuetubao.MyServer;
import com.hh.xuetubao.R;
import com.hh.xuetubao.Utils.BaseObsever;
import com.hh.xuetubao.Utils.HttpUtils;
import com.hh.xuetubao.Utils.NetConfig;
import com.hh.xuetubao.bean.CompanyDetailBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 公司主页
 */

public class CompanyMainFragment extends Fragment {


    @BindView(R.id.nature)
    TextView nature;
    @BindView(R.id.count)
    TextView count;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.web)
    TextView web;
    //    @BindView(R.id.img_hotel)
//    ImageView imgHotel;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_dingwei)
    TextView tvDingwei;
    private Float precision;
    private Float latitude;

    Unbinder unbinder;
    @BindView(R.id.map)
    MapView map;
    private MyServer mServers;
    private AMap aMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_company_main, null);
        unbinder = ButterKnife.bind(this, view);
        map = (MapView) view.findViewById(R.id.map);
        map.onCreate(savedInstanceState);

        String companyOid = getTag();
        doRequest(companyOid);

//        initMap();
        return view;
    }

    private void initMap(Float a, Float b) {
        if (aMap == null) {
            aMap = map.getMap();
        }

        CameraPosition cameraPosition = aMap.getCameraPosition();
        CameraPosition cpNew = CameraPosition.fromLatLngZoom(new LatLng(a, b), cameraPosition.zoom);
        CameraUpdate cu = CameraUpdateFactory.newCameraPosition(cpNew);
        aMap.moveCamera(cu);

        MyLocationStyle myLocationStyle;
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);//定位一次,且将视角移动到地图中心点
        myLocationStyle.showMyLocation(true);
        myLocationStyle.anchor(a, b);//设置定位蓝点图标的锚点方法
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
//aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示,非必需设置。
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。

    }


    private void doRequest(String companyOid) {
        mServers = HttpUtils.getInstance().getServer(NetConfig.zhaopinUrl);
        mServers.CompanyDetailInfo(companyOid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObsever() {
                    @Override
                    public void onNext(Object value) {
                        super.onNext(value);

                        CompanyDetailBean bean = (CompanyDetailBean) value;

                        latitude = Float.valueOf(bean.getLat());
                        precision = Float.valueOf(bean.getLng());

                        initMap(latitude, precision);

                        if (bean.getCompanyNature() != null)
                            nature.setText(bean.getCompanyNature());
                        if (bean.getCompanySize() != null)
                            count.setText(bean.getCompanySize());
                        if (bean.getAddress() != null)
                            address.setText(bean.getAddress());
                        if (bean.getWebSite() != null)
                            web.setText(bean.getWebSite());
                        if (bean.getCompanyLogoPic() != null)
//                            Glide.with(getActivity()).load(bean.getCompanyLogoPic()).into(imgHotel);
                            if (bean.getIntroduction() != null)
                                tvContent.setText(Html.fromHtml(bean.getIntroduction()));
                        if (bean.getFullAddress() != null)
                            tvDingwei.setText(bean.getFullAddress());
                    }

                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onResume() {
        super.onResume();
        map.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        map.onPause();
    }


    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        map.onSaveInstanceState(outState);
    }
}