Search in sources :

Example 1 with MapViewModel

use of com.jamesfchen.map.model.MapViewModel in project spacecraft-android by JamesfChen.

the class MapActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    mapViewModel = ViewModelProviders.of(this).get(MapViewModel.class);
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        realRequestLocationForAmap(getIntent());
    }
    // 获取地图控件引用
    // mMapView = findViewById(R.id.map_view);
    // 在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
    // mMapView.onCreate(savedInstanceState);
    // AMap map = mMapView.getMap();
    textureMapView = findViewById(R.id.map_texture);
    textureMapView.onCreate(savedInstanceState);
    map = textureMapView.getMap();
    CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(// 参数1---要移动到的经纬度,参数2---地图的放缩级别zoom,参数3---地图倾斜度,参数4---地图的旋转角度
    new CameraPosition(myLocation, 4, 0, 0));
    map.moveCamera(cameraUpdate);
    myLocationStyle = new MyLocationStyle();
    // 设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
    // myLocationStyle.interval(2000);
    // 设置定位蓝点的Style
    // settings.setScrollGesturesEnabled(true);
    // settings.setAllGesturesEnabled(true);
    UiSettings settings = map.getUiSettings();
    settings.setMyLocationButtonEnabled(false);
    settings.setZoomControlsEnabled(false);
    settings.setRotateGesturesEnabled(false);
    // settings.setZoomInByScreenCenter(false);
    map.setMyLocationEnabled(true);
    map.setMyLocationStyle(myLocationStyle);
    // myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);//只定位一次。
    // myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);//定位一次,且将视角移动到地图中心点。
    // myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW);//连续定位、且将视角移动到地图中心点,定位蓝点跟随设备移动。(1秒1次定位)
    // myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE);//连续定位、且将视角移动到地图中心点,地图依照设备方向旋转,定位点会跟随设备移动。(1秒1次定位)
    // myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)默认执行此种模式。
    // //以下三种模式从5.1.0版本开始提供
    // myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);//连续定位、蓝点不会移动到地图中心点,定位点依照设备方向旋转,并且蓝点会跟随设备移动。
    // 连续定位、蓝点不会移动到地图中心点,并且蓝点会跟随设备移动。
    myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER);
    // myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE_NO_CENTER);//连续定位、蓝点不会移动到地图中心点,地图依照设备方向旋转,并且蓝点会跟随设备移动。
    myLocationStyle.showMyLocation(false);
    RecyclerView rv_cellinfos = findViewById(R.id.rv_cellinfos);
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {

        @Override
        public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
            return makeMovementFlags(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
        }

        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            int adapterPosition = viewHolder.getAdapterPosition();
            mapViewModel.delete(adapterPosition);
            stopAutoSmoothToEnd = true;
        }
    });
    itemTouchHelper.attachToRecyclerView(rv_cellinfos);
    MapAdapter adapter = new MapAdapter(this, map);
    rv_cellinfos.setAdapter(adapter);
    rv_cellinfos.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    SwipeRefreshLayout srf_cellinfos = findViewById(R.id.srf_cellinfos);
    bt_cellInfos = findViewById(R.id.bt_cellInfos);
    bt_cellInfos.setOnClickListener(v -> Toast.makeText(MapActivity.this, "是不是傻,不让点你还敢点", Toast.LENGTH_LONG).show());
    bt_mylocation = findViewById(R.id.bt_mylocation);
    bt_mylocation.setOnClickListener(v -> {
        Location myLocation = map.getMyLocation();
        double mylatitude = myLocation.getLatitude();
        double mylongitude = myLocation.getLongitude();
        Log.d(TAG, "mylocation: " + mylatitude + "," + mylongitude);
        this.myLocation = new LatLng(mylatitude, mylongitude);
        map.animateCamera(CameraUpdateFactory.newCameraPosition(// 参数1---要移动到的经纬度,参数2---地图的放缩级别zoom,参数3---地图倾斜度,参数4---地图的旋转角度
        new CameraPosition(this.myLocation, 9, 0, 0)));
    });
    bt_import = findViewById(R.id.bt_import);
    bt_import.setOnClickListener(view -> {
        if (is_import) {
            Toast.makeText(MapActivity.this, "蛋已经领取啦,咋还想领", Toast.LENGTH_SHORT).show();
            return;
        }
        is_import = true;
        Toast.makeText(MapActivity.this, "彩蛋,正在导入数据", Toast.LENGTH_SHORT).show();
        iLbsApiClient.sendImportMockJson();
    });
    mapViewModel.getAllLbsDatas().observe(this, p -> {
        bt_cellInfos.setText("不许点击喔:" + p.size());
        adapter.submitList(p);
        if (!stopAutoSmoothToEnd && p.size() > 5) {
            rv_cellinfos.smoothScrollToPosition(p.size() - 1);
        }
        stopAutoSmoothToEnd = false;
    });
    mapViewModel.getNeedUploadDatas().observe(this, p -> {
        if (p == null || p.size() == 0)
            return;
        Log.d(TAG, "getNeedUploadDatas: " + p.size());
        for (int i = 0; i < p.size(); i++) {
            LBS lbs = p.get(i);
        // ReportApi.reportLocation(auth, lbs, new ReportApi.Callback() {
        // @Override
        // public void onFailure() {
        // }
        // 
        // @Override
        // public void onResponse() {
        // lbs.setNeedUpload(false);
        // mapViewModel.update(lbs);
        // }
        // });
        }
    });
    srf_cellinfos.setOnRefreshListener(() -> srf_cellinfos.setRefreshing(false));
}
Also used : MyLocationStyle(com.amap.api.maps.model.MyLocationStyle) UiSettings(com.amap.api.maps.UiSettings) DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) SuppressLint(android.annotation.SuppressLint) MapViewModel(com.jamesfchen.map.model.MapViewModel) CameraPosition(com.amap.api.maps.model.CameraPosition) ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper) RecyclerView(androidx.recyclerview.widget.RecyclerView) LatLng(com.amap.api.maps.model.LatLng) CameraUpdate(com.amap.api.maps.CameraUpdate) LBS(com.jamesfchen.map.model.LBS) AMapLocation(com.amap.api.location.AMapLocation) Location(android.location.Location) AppLocation(com.jamesfchen.map.model.AppLocation)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Location (android.location.Location)1 DividerItemDecoration (androidx.recyclerview.widget.DividerItemDecoration)1 ItemTouchHelper (androidx.recyclerview.widget.ItemTouchHelper)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 SwipeRefreshLayout (androidx.swiperefreshlayout.widget.SwipeRefreshLayout)1 AMapLocation (com.amap.api.location.AMapLocation)1 CameraUpdate (com.amap.api.maps.CameraUpdate)1 UiSettings (com.amap.api.maps.UiSettings)1 CameraPosition (com.amap.api.maps.model.CameraPosition)1 LatLng (com.amap.api.maps.model.LatLng)1 MyLocationStyle (com.amap.api.maps.model.MyLocationStyle)1 AppLocation (com.jamesfchen.map.model.AppLocation)1 LBS (com.jamesfchen.map.model.LBS)1 MapViewModel (com.jamesfchen.map.model.MapViewModel)1