1 package com.jasgroup.cn.amhdeam;
2
3 import java.io.IOException;
4 import java.util.Iterator;
5
6 import android.Manifest;
7 import android.app.Activity;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.pm.ActivityInfo;
11 import android.content.pm.PackageManager;
12 import android.location.Criteria;
13 import android.location.GpsSatellite;
14 import android.location.GpsStatus;
15 import android.location.Location;
16 import android.location.LocationListener;
17 import android.location.LocationManager;
18 import android.location.LocationProvider;
19 import android.os.Bundle;
20 import android.provider.Settings;
21 import android.support.v4.app.ActivityCompat;
22 import android.util.Log;
23 import android.view.WindowManager;
24 import android.widget.EditText;
25 import android.widget.LinearLayout;
26 import android.widget.Toast;
27
28 import com.esri.android.map.GraphicsLayer;
29 import com.esri.android.map.Layer;
30 import com.esri.android.map.MapView;
31 import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
32 import com.esri.core.geometry.Envelope;
33 import com.esri.core.geometry.Point;
34 import com.esri.core.geometry.SpatialReference;
35 import com.esri.core.map.Graphic;
36 import com.esri.core.symbol.PictureMarkerSymbol;
37
38 import org.xmlpull.v1.XmlPullParserException;
39
40 public class GpsActivity extends Activity {
41 private EditText editText;
42 private LocationManager lm;
43 private GraphicsLayer graphicsLayer = null;
44 private String latitude;
45 private String longitude;
46 private String ip = null;
47 private String port = null;
48 private String userid = null;
49 private MapView mMapView;
50 private LinearLayout linearLayout;
51 private Layer layer;
52 private static final String TAG = "GpsActivity";
53
54 @Override
55 protected void onDestroy() {
56 // TODO Auto-generated method stub
57 super.onDestroy();
58 // lm.removeUpdates(locationListener);
59 }
60
61
62 @Override
63 public void onCreate(Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
65
66 setContentView(R.layout.locationutil);
67
68 editText = (EditText) findViewById(R.id.editText);
69 lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
70 SpatialReference S = SpatialReference.create(4326);
71 Envelope envelope = new Envelope(62.938320359601796, 37.37854569628149, 66.4928652149837, 39.6757739604943);
72 mMapView = new MapView(this, S, envelope);
73
74 // mMapView = (MapView) findViewById(R.id.map);
75
76 mMapView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
77 //添加地图
78 layer = new ArcGISDynamicMapServiceLayer("http://192.168.20.180:6080/arcgis/rest/services/gongquAB/MapServer");
79 mMapView.addLayer(layer);
80 mMapView.setExtent(envelope);
81 graphicsLayer = new GraphicsLayer();
82 mMapView.addLayer(graphicsLayer);
83 setContentView(mMapView);
84 /*
85 接受端口信息
86 */
87 Bundle bundle = this.getIntent().getExtras();
88 if (bundle != null) {
89 userid = bundle.getString("userid");
90 ip = bundle.getString("ip");
91 port = bundle.getString("port");
92 }
93 // tv = (TextView) findViewById(R.id.tv);
94 // mMapView = (MapView) findViewById(R.id.map);
95 linearLayout = (LinearLayout) findViewById(R.id.llMap);
96
97 //判断GPS是否正常启动
98 if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
99 Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show();
100 //返回开启GPS导航设置界面
101 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
102 startActivityForResult(intent, 0);
103 return;
104 }
105
106 //为获取地理位置信息时设置查询条件
107 String bestProvider = lm.getBestProvider(getCriteria(), true);
108 //获取位置信息
109 //如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER
110 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
111
112 return;
113 }
114 Location location = lm.getLastKnownLocation(bestProvider);
115 updateToNewLocation(location);
116 //监听状态
117 lm.addGpsStatusListener(listener);
118 //绑定监听,有4个参数
119 //参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种
120 //参数2,位置信息更新周期,单位毫秒
121 //参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息
122 //参数4,监听
123 //备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新
124
125 // 1秒更新一次,或最小位移变化超过1米更新一次;
126 //注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置
127 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
128 }
129
130 @Override
131 protected void onResume() {
132 /**
133 * 设置为横屏
134 */
135 if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
136 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
137
138 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
139
140 return;
141 }
142 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1,
143 locationListener);
144 }
145 super.onResume();
146 };
147
148 //位置监听
149 private LocationListener locationListener = new LocationListener() {
150
151 /**
152 * 位置信息变化时触发
153 */
154 public void onLocationChanged(Location location) {
155 updateToNewLocation(location);
156 Log.i(TAG, "时间:" + location.getTime());
157 Log.i(TAG, "经度:" + location.getLongitude());
158 Log.i(TAG, "纬度:" + location.getLatitude());
159 Log.i(TAG, "海拔:" + location.getAltitude());
160 }
161
162 /**
163 * GPS状态变化时触发
164 */
165 public void onStatusChanged(String provider, int status, Bundle extras) {
166 switch (status) {
167 //GPS状态为可见时
168 case LocationProvider.AVAILABLE:
169 Log.i(TAG, "当前GPS状态为可见状态");
170 break;
171 //GPS状态为服务区外时
172 case LocationProvider.OUT_OF_SERVICE:
173 Log.i(TAG, "当前GPS状态为服务区外状态");
174 break;
175 //GPS状态为暂停服务时
176 case LocationProvider.TEMPORARILY_UNAVAILABLE:
177 Log.i(TAG, "当前GPS状态为暂停服务状态");
178 break;
179 }
180 }
181
182 /**
183 * GPS开启时触发
184 */
185 public void onProviderEnabled(String provider) {
186 if (ActivityCompat.checkSelfPermission(GpsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(GpsActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
187 // TODO: Consider calling
188 // ActivityCompat#requestPermissions
189 // here to request the missing permissions, and then overriding
190 // public void onRequestPermissionsResult(int requestCode, String[] permissions,
191 // int[] grantResults)
192 // to handle the case where the user grants the permission. See the documentation
193 // for ActivityCompat#requestPermissions for more details.
194 return;
195 }
196 Location location = lm.getLastKnownLocation(provider);
197 updateView(location);
198 }
199
200 /**
201 * GPS禁用时触发
202 */
203 public void onProviderDisabled(String provider) {
204 updateView(null);
205 }
206
207
208 };
209
210 //状态监听
211 GpsStatus.Listener listener = new GpsStatus.Listener() {
212 public void onGpsStatusChanged(int event) {
213 switch (event) {
214 //第一次定位
215 case GpsStatus.GPS_EVENT_FIRST_FIX:
216 Log.i(TAG, "第一次定位");
217 break;
218 //卫星状态改变
219 case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
220 Log.i(TAG, "卫星状态改变");
221 //获取当前状态
222 if (ActivityCompat.checkSelfPermission(GpsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
223 // TODO: Consider calling
224 // ActivityCompat#requestPermissions
225 // here to request the missing permissions, and then overriding
226 // public void onRequestPermissionsResult(int requestCode, String[] permissions,
227 // int[] grantResults)
228 // to handle the case where the user grants the permission. See the documentation
229 // for ActivityCompat#requestPermissions for more details.
230 return;
231 }
232 GpsStatus gpsStatus = lm.getGpsStatus(null);
233 //获取卫星颗数的默认最大值
234 int maxSatellites = gpsStatus.getMaxSatellites();
235 //创建一个迭代器保存所有卫星
236 Iterator<GpsSatellite> iters = gpsStatus.getSatellites().iterator();
237 int count = 0;
238 while (iters.hasNext() && count <= maxSatellites) {
239 GpsSatellite s = iters.next();
240 count++;
241 }
242 System.out.println("搜索到:"+count+"颗卫星");
243 break;
244 //定位启动
245 case GpsStatus.GPS_EVENT_STARTED:
246 Log.i(TAG, "定位启动");
247 break;
248 //定位结束
249 case GpsStatus.GPS_EVENT_STOPPED:
250 Log.i(TAG, "定位结束");
251 break;
252 }
253 };
254 };
255
256 /**
257 * 实时更新文本内容
258 *
259 * @param location
260 */
261 private void updateView(Location location){
262 if(location!=null){
263 editText.setText("设备位置信息\n\n经度:");
264 editText.append(String.valueOf(location.getLongitude()));
265 editText.append("\n纬度:");
266 editText.append(String.valueOf(location.getLatitude()));
267 }else{
268 //清空EditText对象
269 editText.getEditableText().clear();
270 }
271 }
272
273 /**
274 * 返回查询条件
275 * @return
276 */
277 private Criteria getCriteria(){
278 Criteria criteria=new Criteria();
279 //设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
280 criteria.setAccuracy(Criteria.ACCURACY_FINE);
281 //设置是否要求速度
282 criteria.setSpeedRequired(true);
283 // 设置是否允许运营商收费
284 criteria.setCostAllowed(false);
285 //设置是否需要方位信息
286 criteria.setBearingRequired(false);
287 //设置是否需要海拔信息
288 criteria.setAltitudeRequired(false);
289 // 设置对电源的需求
290 criteria.setPowerRequirement(Criteria.POWER_LOW);
291 return criteria;
292 }
293 private Location updateToNewLocation(Location location) {
294 System.out.println("--------zhixing--2--------");
295 String latLongString;
296 if (location != null) {
297 //设置当前坐标
298 Point point=new Point();
299 point.setX(location.getLongitude());
300 point.setY(location.getLatitude());
301 PictureMarkerSymbol pictureMarkerSymbol=new PictureMarkerSymbol(getResources().getDrawable(R.drawable.location1));
302
303 Graphic graphic=new Graphic(point,pictureMarkerSymbol);
304 // Graphic graphic=new Graphic(point,new SimpleMarkerSymbol(Color.RED,25, SimpleMarkerSymbol.STYLE.CIRCLE));
305 graphicsLayer.addGraphic(graphic);//添加地图中
306
307 latitude = Double.toString(location.getLatitude());
308 longitude = Double.toString(location.getLongitude());
309 latLongString = "纬度:" + latitude + "\n经度:" + longitude;
310 System.out.println("经度:" + longitude + "纬度:" + latitude);
311 sendp();
312 } else {
313 latLongString = "无法获取地理信息,请稍后...";
314 }
315 if(latitude!=null){
316 System.out.println("--------反馈信息----------"+ String.valueOf(latitude));
317 }
318 Toast.makeText(getApplicationContext(), latLongString, Toast.LENGTH_SHORT).show();
319
320 return location;
321 }
322 public void sendp(){
323 new Thread(){
324 @Override
325 public void run(){
326 // while (!exit){
327 try {
328 AsyncTaskUtil.getData(userid, latitude, longitude, ip, port);
329
330 } catch (IOException e) {
331 e.printStackTrace();
332 } catch (XmlPullParserException e) {
333 e.printStackTrace();
334 }
335 }
336 // }
337 }.start();
338 }
339 }