MainActivity.java
15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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");
}
}
}