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

import android.annotation.SuppressLint;
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 android.widget.TextView;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import com.arlib.floatingsearchview.FloatingSearchView;
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.LocationDisplayManager;
import com.esri.android.map.MapView;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.map.event.OnZoomListener;
import com.esri.android.runtime.ArcGISRuntime;
import com.esri.core.geometry.Point;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.PictureMarkerSymbol;
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.search.POISuggestion;
import com.pashanhoo.landsurvey.utils.AppInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    // 地图控件
    private MapView mapView;
    // 地图中的GraphicsLayer对象
    private GraphicsLayer graphicsLayer;
    // GPS定位器对象
    private LocationDisplayManager locationDisplayManager;
    // 四个天地图图层对象
    private TianDiTuTiledMapServiceLayer t_vec;
    private TianDiTuTiledMapServiceLayer t_cva;
    private TianDiTuTiledMapServiceLayer t_img;
    private TianDiTuTiledMapServiceLayer t_cia;
    // mbtiles式天地图数据
    private TianDiTuLocalTiledMapServiceLayer t_local;
    // 左下角LGOGO信息
    private TextView mapinfoView;
    // 浮动查询条
    private FloatingSearchView floatingSearchView;
    // 当前地图层级
    private int curLevel = 0;
    // 地图类型名称
    private String mapType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 分别获取控件
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        mapinfoView = (TextView) findViewById(R.id.mapinfo);
        floatingSearchView = (FloatingSearchView) findViewById(R.id.floating_search_view);
        setSupportActionBar(toolbar);

        //初始化-------------------------------------------------------------------
        AppInfo.Settings();

        //按钮动作-------------------------------------------------------------------
        // 放大按钮
        FloatingActionButton zoomin = (FloatingActionButton) findViewById(R.id.zoomin);
        zoomin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 停止GPS
                if (locationDisplayManager != null) {
                    locationDisplayManager.stop();
                }
                // 保证地图级别小于20
                if (curLevel < 20) {
                    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();
                }
                // 保证地图级别大于0
                if (curLevel > 0) {
                    mapView.zoomout(true);
                }
            }
        });
        // 回到初始页面
        FloatingActionButton fullmap = (FloatingActionButton) findViewById(R.id.fullmap);
        fullmap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                locationDisplayManager.stop();
                graphicsLayer.removeAll();
                // 恢复初始设置地图范围
                mapView.zoomToResolution(AppInfo.initPoint, AppInfo.initRes);
                curLevel = TDTTileinfo.getCurrentLevel(AppInfo.initRes);
                mapinfoView.setText("当前地图级别为:" + String.valueOf(curLevel));
            }
        });
        // GPS按钮
        FloatingActionButton gps = (FloatingActionButton) findViewById(R.id.gps);
        gps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 设置GPS功能
                if (locationDisplayManager.isStarted()) {
                    locationDisplayManager.stop();
                } else {
                    locationDisplayManager.start();
                    mapView.zoomToResolution(locationDisplayManager.getPoint(), TDTTileinfo.getRes4490()[18]);
                }
            }
        });
        final FloatingActionButton search = (FloatingActionButton) findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (floatingSearchView.getVisibility() == View.INVISIBLE) {
                    floatingSearchView.setVisibility(View.VISIBLE);
                    search.setImageResource(R.mipmap.ic_find_replace_white_48dp);
                } else {
                    search.setImageResource(R.mipmap.ic_search_white_48dp);
                    graphicsLayer.removeAll();
                    floatingSearchView.clearSuggestions();
                    floatingSearchView.setVisibility(View.INVISIBLE);
                }
            }
        });
        //地图初始化-------------------------------------------------------------------
        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);
        // 一个本地sqlite数据库,注意其只有8-9级数据,因此在数据层中予以了显示控制
        t_local = new TianDiTuLocalTiledMapServiceLayer("my.db", "IMG_C");
        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";
        // 添加GraphicsLayer
        graphicsLayer = new GraphicsLayer();
        mapView.addLayer(graphicsLayer);
        // 设置地图的最小和最大分辨率
        mapView.setMaxResolution(TDTTileinfo.getRes4490()[1]);
        mapView.setMinResolution(TDTTileinfo.getRes4490()[19]);
        // 监听地图状态变化事件
        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);
                    curLevel = TDTTileinfo.getCurrentLevel(AppInfo.initRes);
                    mapinfoView.setText("当前地图级别为:" + String.valueOf(curLevel));
                }
            }
        });
        mapView.setOnZoomListener(new OnZoomListener() {
            @Override
            public void preAction(float v, float v1, double v2) {
            }

            @Override
            public void postAction(float v, float v1, double v2) {
                curLevel = TDTTileinfo.getCurrentLevel(mapView.getResolution());
                mapinfoView.setText("当前地图级别为:" + String.valueOf(curLevel));
                // t_local只有8和9级数据
                if (curLevel > 9 | curLevel < 8) {
                    t_local.setVisible(false);
                } else {
                    t_local.setVisible(true);
                }
            }
        });

        // 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()), TDTTileinfo.getRes4490()[18]);
                mapinfoView.setText("当前地图级别为:18");
            }

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

            @Override
            public void onProviderEnabled(String s) {
            }

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

        // 处理浮动查询条
        floatingSearchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() {
            @Override
            public void onSuggestionClicked(SearchSuggestion searchSuggestion) {
                graphicsLayer.removeAll();

                POISuggestion colorSuggestion = (POISuggestion) searchSuggestion;
                Point poipt = new Point(colorSuggestion.getLng(), colorSuggestion.getLat());
                mapView.zoomToResolution(poipt, TDTTileinfo.getRes4490()[19]);
                graphicsLayer.addGraphic(new Graphic(poipt,
                        new PictureMarkerSymbol(getResources().getDrawable(R.mipmap.locator)).setOffsetY(16)));
//                floatingSearchView.clearSuggestions();
                mapinfoView.setText("当前地图级别为:19");
            }

            @Override
            public void onSearchAction(String currentQuery) {
                if (currentQuery.length() > 3) {
                    floatingSearchView.clearSuggestions();
                    // 查询天地图API
                    String str = "{\"keyWord\":\"" + currentQuery + "\",\"level\":\"11\",\"mapBound\":\"76.24832,30.1129,156.40458,49.97618\",\"queryType\":\"1\",\"count\":\"10\",\"start\":\"0\",\"queryTerminal\":\"1000\"}";
                    AndroidNetworking.post("http://map.tianditu.com/query.shtml")
                            .addBodyParameter("type", "query")
                            .addBodyParameter("postStr", str)
                            .setTag("test")
                            .build()
                            .getAsJSONObject(new JSONObjectRequestListener() {
                                @Override
                                public void onResponse(JSONObject response) {
                                    //解析响应结果
                                    JSONArray Jarray = null;
                                    try {
                                        Jarray = response.getJSONArray("pois");
                                        ArrayList<POISuggestion> poiSuggestions = new ArrayList<POISuggestion>();
                                        for (int i = 0; i < Jarray.length(); i++) {
                                            JSONObject Jasonobject = Jarray.getJSONObject(i);
                                            poiSuggestions.add(new POISuggestion(Jasonobject.getString("name"),
                                                    Jasonobject.getString("address"),
                                                    Jasonobject.getString("lonlat")));
                                        }
                                        floatingSearchView.swapSuggestions(poiSuggestions);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onError(ANError anError) {

                                }
                            });
                }
            }
        });
    }

    @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");
        }
    }
}