forked from pu20720/2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocationMAP.java
358 lines (278 loc) · 9.5 KB
/
LocationMAP.java
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
package andbas.Ch11TabHost2;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.xml.LoginXMLStruct;
import com.xml.OnLineListHandler;
import com.xml.OnlineStruct;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
public class LocationMAP extends FragmentActivity{
private static final int MSG_UPDATE = 1;
//MapView mapView;
GoogleMap mapView = null;
TextView textView1;
SupportMapFragment fragment;
LatLng Pune;
Marker pune;
EditText fileedit;
int start=0;
int press=0;
private Timer timer;
Marker lastOpenned = null;
String IPAddress;
ArrayList<OnlineStruct> slatrec;
public LocationManager locationManager1;
public Location location1 = null;
public String locationProvider1;
int gindex;
String groupname[];
String user;
LoginXMLStruct data;
String id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview1);
Bundle bData = this.getIntent().getExtras();
if (bData != null)
{
id = bData.getString( "id" );
}
setMainThread();
if(location1 !=null)
{
locationManager1.requestLocationUpdates(locationProvider1,
100, 100, locationListener1);
}
timer = new Timer();
timer.schedule(new DateTask(), 0, 5000);
}
void setMainThread()
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
IPAddress = (String) this.getResources().getText(R.string.url);
if (mapView == null)
{
mapView = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview)).getMap();
if (mapView != null)
mapView.setMyLocationEnabled(true);
}
locationManager1 = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
location1 = getMyLocationProvider(locationManager1);
location1 = locationManager1.getLastKnownLocation(provider);
}
private void MP()
{
PolylineOptions rectOptions = new PolylineOptions();
rectOptions.width(10);
rectOptions.color(0xff659d32);
if (mapView == null) return;
for (int i=0; i<slatrec.size(); i++)
{
double lat = Double.valueOf(slatrec.get(i).lat);
double lng = Double.valueOf(slatrec.get(i).lng);
LatLng newlanlng = new LatLng(lat, lng);
mapView.addMarker(new MarkerOptions().position(newlanlng).title(slatrec.get(i).name + " 在這裡哦").visible(true)).showInfoWindow();
}
mapView.setOnMarkerClickListener(new OnMarkerClickListener() {
public boolean onMarkerClick(Marker marker) {
// Check if there is an open info window
if (lastOpenned != null) {
// Close the info window
lastOpenned.hideInfoWindow();
// Is the marker the same marker that was already open
if (lastOpenned.equals(marker)) {
// Nullify the lastOpenned object
lastOpenned = null;
// Return so that the info window isn't openned again
return true;
}
}
// Open the info window for the marker
marker.showInfoWindow();
// Re-assign the last openned such that we can close it later
lastOpenned = marker;
// Event was handled by our code do not launch default behaviour.
return true;
}
});
//當點開時,就可看到資訊
mapView.setInfoWindowAdapter(new InfoWindowAdapter()
{
private final View contents = getLayoutInflater().inflate(R.layout.content, null);
public View getInfoWindow(Marker marker)
{
return null;
}
public View getInfoContents(Marker marker)
{
//int index = Integer.valueOf(marker.getTitle());
TextView tvTitle = ((TextView)contents.findViewById(R.id.title));
tvTitle.setText(marker.getSnippet());
return contents;
}
});
}
public class DateTask extends TimerTask {
public void run()
{
String uriAPI = IPAddress + "getgpslist.php?user=" + id;
Log.i("TAG", uriAPI);
URL url = null;
try {
url = new URL(uriAPI);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
OnLineListHandler myHandler = new OnLineListHandler();
xr.setContentHandler(myHandler);
// open connection
xr.parse(new InputSource(url.openStream()));
slatrec = myHandler.getContainer().getListItems();
} catch (Exception e) {
e.printStackTrace();
return;
}
if (slatrec != null)
{
//GPS更新
Message msg = new Message();
msg.what = MSG_UPDATE;
myHandler.sendMessage(msg);
}
}
}
public Handler myHandler = new Handler(){
public void handleMessage(Message msg) {
switch(msg.what)
{
case MSG_UPDATE:
MP();
break;
}
}
};
public void setMapCenter(Location location1)
{
}
public LocationListener locationListener1 =
new LocationListener()
{
//@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
//@Override
public void onProviderEnabled(String provider)
{
}
//@Override
public void onProviderDisabled(String provider)
{
location1 = null;
}
//@Override
public void onLocationChanged(Location location)
{
location1 = location;
if (location1 != null)
mapView.animateCamera(CameraUpdateFactory.newLatLngZoom(Pune, 17));
}
};
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
{
if (timer != null)
timer.cancel();
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
public Location getMyLocationProvider(LocationManager locationManager1)
{
Location currentLocation = null;
try
{
Criteria Criteria1 = new Criteria();
Criteria1.setAccuracy(Criteria.ACCURACY_FINE);
Criteria1.setPowerRequirement(Criteria.POWER_LOW);
Criteria1.setAltitudeRequired(false);
Criteria1.setBearingRequired(false);
Criteria1.setCostAllowed(true);
locationProvider1 = locationManager1.getBestProvider(Criteria1, true);
currentLocation = locationManager1.getLastKnownLocation(locationProvider1);
}
catch (Exception e) {
{
e.printStackTrace();
}
return currentLocation;
}
return currentLocation;
}
}