MainActivity.java 7.92 KB
package com.pashanhoo.landsurvey;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.esri.android.map.LocationDisplayManager;
import com.esri.android.map.MapView;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.runtime.ArcGISRuntime;
import com.esri.core.geometry.Point;
import com.pashanhoo.landsurvey.layers.tianditu.TDTTileinfo;
import com.pashanhoo.landsurvey.layers.tianditu.TianDiTuLocalTiledMapServiceLayer;
import com.pashanhoo.landsurvey.layers.tianditu.TianDiTuTiledMapServiceLayer;
import com.pashanhoo.landsurvey.layers.tianditu.TianDiTuTiledMapServiceType;
import com.pashanhoo.landsurvey.utils.AppInfo;

public class MainActivity extends AppCompatActivity {

    private MapView mapView;
    private LocationDisplayManager locationDisplayManager;
    private TianDiTuTiledMapServiceLayer t_vec;
    private TianDiTuTiledMapServiceLayer t_cva;
    private TianDiTuTiledMapServiceLayer t_img;
    private TianDiTuTiledMapServiceLayer t_cia;
    // mbtiles式天地图数据
    private TianDiTuLocalTiledMapServiceLayer t_local;

    private String mapType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //初始化-------------------------------------------------------------------
        AppInfo.Settings();
        //按钮动作-------------------------------------------------------------------
        FloatingActionButton zoomin = (FloatingActionButton) findViewById(R.id.zoomin);
        zoomin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(locationDisplayManager!=null){
                    locationDisplayManager.stop();
                }
                mapView.zoomin(true);
            }
        });
        FloatingActionButton zoomout = (FloatingActionButton) findViewById(R.id.zoomout);
        zoomout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(locationDisplayManager!=null){
                    locationDisplayManager.stop();
                }
                mapView.zoomout(true);
            }
        });
        FloatingActionButton fullmap = (FloatingActionButton) findViewById(R.id.fullmap);
        fullmap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                locationDisplayManager.stop();
                mapView.zoomToResolution(AppInfo.initPoint, AppInfo.initRes);
            }
        });
        FloatingActionButton gps = (FloatingActionButton) findViewById(R.id.gps);
        gps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(locationDisplayManager.isStarted()){
                    locationDisplayManager.stop();
                }else{
                    locationDisplayManager.start();
                    mapView.zoomToResolution(locationDisplayManager.getPoint(), TDTTileinfo.getRes4490()[18]);
                }
            }
        });
        //地图初始化-------------------------------------------------------------------
        ArcGISRuntime.setClientId("1eFHW78avlnRUPHm");
        mapView = (MapView)findViewById(R.id.map);
        mapView.setEsriLogoVisible(false);
        mapView.enableWrapAround(false);

        t_vec = new TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType.VEC_C);
        mapView.addLayer(t_vec);
        t_cva = new TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType.CVA_C);
        mapView.addLayer(t_cva);

//        t_local = new TianDiTuLocalTiledMapServiceLayer("my.mbtiles");
//        mapView.addLayer(t_local);

        t_img = new TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType.IMG_C);
        t_img.setVisible(false);
        mapView.addLayer(t_img);
        t_cia = new TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType.CIA_C);
        t_cia.setVisible(false);
        mapView.addLayer(t_cia);
        mapType = "VEC";

        mapView.setMaxResolution(TDTTileinfo.getRes4490()[0]);
        mapView.setMinResolution(TDTTileinfo.getRes4490()[18]);

        mapView.setOnStatusChangedListener(new OnStatusChangedListener() {
            private static final long serialVersionUID = 1L;
            @Override
            public void onStatusChanged(Object o, STATUS status) {
                if(status == STATUS.INITIALIZED){
                    mapView.zoomToResolution(AppInfo.initPoint, AppInfo.initRes);
                }
            }
        });

        // GPS
        locationDisplayManager =  mapView.getLocationDisplayManager();
        locationDisplayManager.setShowLocation(true);
        locationDisplayManager.setAutoPanMode(LocationDisplayManager.AutoPanMode.LOCATION);//设置模式
        locationDisplayManager.setShowPings(true);
        locationDisplayManager.setLocationListener(new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                mapView.zoomToResolution(new Point(location.getLongitude(), location.getLatitude()), mapView.getResolution());
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
            }

            @Override
            public void onProviderEnabled(String s) {
            }

            @Override
            public void onProviderDisabled(String s) {
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.unpause();
        if(locationDisplayManager!=null){
            locationDisplayManager.resume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.pause();
        if(locationDisplayManager!=null){
            locationDisplayManager.pause();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if(locationDisplayManager!=null){
            locationDisplayManager.stop();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            if(mapType == "VEC"){
                setLayer("IMG");
                mapType = "IMG";
            }else{
                setLayer("VEC");
                mapType = "VEC";
            }
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    // 切换地图形式
    private void setLayer(String type){
        switch (type){
            case "VEC":
                t_vec.setVisible(true);
                t_cva.setVisible(true);
                t_img.setVisible(false);
                t_cia.setVisible(false);
                break;
            case "IMG":
                t_vec.setVisible(false);
                t_cva.setVisible(false);
                t_img.setVisible(true);
                t_cia.setVisible(true);
                break;
            default:
                Log.i("keymapinfo", "map");
        }
    }
}