· 9 years ago · Dec 08, 2016, 05:46 AM
1GoogleMap googleMap;
2double latti = 0, longii = 0;
3TextView tv_presentlocation, tv_currentlocationnew;
4Context ctx;
5private Polyline polyline;
6List<MapDto> rd_list;
7private ArrayList<LatLng> pathPts = new ArrayList<LatLng>();
8private double prev_lat = 0, prev_long = 0;
9private static final String TAG = "trakingActivity";
10
11Helper db;
12MapDto md;
13
14protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.tracking);
17 ctx = this;
18 db = Helper.Getinstance(getApplicationContext());
19 md = new MapDto();
20
21 googleMap = ((MapFragment) getFragmentManager().findFragmentById(
22 R.id.map)).getMap();
23
24 if (googleMap == null) {
25 return;
26 }
27 googleMap.setMyLocationEnabled(true);
28 googleMap.getUiSettings().setAllGesturesEnabled(true);
29 googleMap.getUiSettings().setMyLocationButtonEnabled(true);
30 googleMap.getUiSettings().setZoomControlsEnabled(true);
31 googleMap.getUiSettings().setCompassEnabled(true);
32
33 googleMap.setPadding(0, 100, 0, 0);
34
35 LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
36 lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
37
38}
39
40@Override
41public void onLocationChanged(Location location) {
42 googleMap.clear();
43
44 db = Helper.Getinstance(getApplicationContext());
45 md = new MapDto();
46
47 rd_list = db.getAllMapDto();
48
49 double latti = location.getLatitude();
50 double longii = location.getLongitude();
51
52 LatLng latlng = new LatLng(latti, longii);
53 md.setLat(latti);
54 md.setLongii(longii);
55 db.addMapDto(md);
56
57 pathPts.add(latlng);
58 polyline.setPoints(pathPts);
59
60 Moves the camera to users current longitude and latitude
61 googleMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
62
63 t
64 location.
65 googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng,
66 (float) 14.6));
67
68
69
70
71
72 for (int j = 0; j < polyline.getPoints().size(); j++) {
73 Log.i(TAG, " --- Polyline point : " + polyline.getPoints().get(j));
74 }
75
76}
77
78@Override
79protected void onResume() {
80 super.onResume();
81if (polyline != null) {
82 polyline = googleMap.addPolyline(new PolylineOptions()
83 .add(new LatLng(prev_lat, prev_long)).width(5)
84 .color(Color.GREEN));
85 googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
86 new LatLng(prev_lat, prev_long), 10.0f));
87
88 } else {
89
90 // LatLng latlong = new LatLng(latti,longii);
91 // pathPts.add(latlong);
92 polyline.setPoints(pathPts);
93 fixZoom();
94
95 for (int j = 0; j < polyline.getPoints().size(); j++) {
96 Log.i(TAG, " --- Polyline point : "
97 + polyline.getPoints().get(j));
98 }
99 }
100}
101
102
103@SuppressWarnings("unused")
104private class drawpath_async extends AsyncTask<Double, Object, String> {
105 LoadingDialog loading;
106
107 @Override
108 protected void onPreExecute() {
109 super.onPreExecute();
110 loading = new LoadingDialog(ctx);
111 loading.show();
112 if (polyline == null) {
113 polyline = googleMap.addPolyline(new PolylineOptions()
114 .add(new LatLng(prev_lat, prev_long)).width(5)
115 .color(Color.GREEN));
116 googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
117 new LatLng(prev_lat, prev_long), 12.0f));
118
119 }
120 }
121
122 @Override
123 protected String doInBackground(Double... params) {
124 if (android.os.Debug.isDebuggerConnected())
125 android.os.Debug.waitForDebugger();
126 /*
127 * JSONArray jarr; JSONObject jobj; try { jobj = params[0]; jarr =
128 * jobj.getJSONArray("result"); int f = jarr.length(); for (int i =
129 * 0; i < jarr.length(); i++) { Log.d("array length", "" +
130 * jarr.length());
131 *
132 * jobj = jarr.getJSONObject(i); LatLng latlong = new
133 * LatLng(jobj.getDouble("GpsLat"), jobj.getDouble("GpsLong"));
134 * pathPts.add(latlong); prev_lat = jobj.getDouble("GpsLat");
135 * prev_long = jobj.getDouble("GpsLong");
136 */
137 rd_list = new ArrayList<MapDto>();
138
139 for (int i = 0; i < rd_list.size(); i++) {
140
141 Toast.makeText(ctx, "entered", Toast.LENGTH_SHORT).show();
142
143 latti = rd_list.get(i).getLat();
144 longii = rd_list.get(i).getLongii();
145
146 LatLng latlng = new LatLng(latti, longii);
147
148
149 pathPts.add(latlng);
150
151 //prev_lat = latti;
152 // prev_long= longii;
153
154 /*
155 * for(int j=0;j<polyline.getPoints().size();j++) { Log.i(TAG,
156 * " --- Polyline point : " + polyline.getPoints().get(j)); }
157 */
158 }
159
160 return null;
161 }
162
163 @Override
164 protected void onPostExecute(String result) {
165 super.onPostExecute(result);
166 polyline.setPoints(pathPts);
167 fixZoom();
168 try {
169 mAddressofpresent(prev_lat, prev_long);
170 } catch (IOException e) {
171 e.printStackTrace();
172 }
173 loading.dismiss();
174 Log.i(TAG, "&&&& Polyline points count : "
175 + polyline.getPoints().size());
176 for (int i = 0; i < polyline.getPoints().size(); i++) {
177 Log.i(TAG, " --- Polyline point : "
178 + polyline.getPoints().get(i));
179 }
180 }
181}
182
183private void fixZoom() {
184 try {
185 LatLngBounds.Builder bc = new LatLngBounds.Builder();
186 if (pathPts.size() > 0) {
187 for (LatLng item : pathPts) {
188 bc.include(item);
189 }
190 googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
191 bc.build(), 100));
192 } else {
193 bc.include(new LatLng(prev_lat, prev_long));
194 googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
195 bc.build(), 100));
196 }
197 } catch (Exception e) {
198 e.printStackTrace();
199 }
200}
201
202public void mAddressofpresent(Double lat, Double longii) throws IOException {
203 Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
204
205 List<Address> addresses = geocoder.getFromLocation(lat, longii, 1);
206
207 String address = addresses.get(0).getAddressLine(1);
208
209 // tv_presentlocation.setText(address);
210
211}
212
213@Override
214public void onProviderEnabled(String provider) {
215
216}
217
218@Override
219public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
220
221}
222
223@Override
224public void onServiceResolved(NsdServiceInfo serviceInfo) {
225
226}
227
228@Override
229public void onStatusChanged(String provider, int status, Bundle extras) {
230
231}
232
233@Override
234public void onProviderDisabled(String provider) {
235
236}
237
238private static String DB_Name = "GoogleMap";
239public static final String TABLE_NAME = "Map";
240private final String key_id = "ID";
241private String lattitude = "lattitude";
242private String longitude = "longitude";
243static SQLiteDatabase db;
244private static Helper HelperDb;
245
246public Helper(Context context, String name, CursorFactory factory,
247 int version) {
248 super(context, name, factory, version);
249}
250
251@Override
252public void onCreate(SQLiteDatabase db) {
253 String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + key_id
254 + " INTEGER PRIMARY KEY AUTOINCREMENT," + lattitude + " TEXT,"
255 + longitude + " TEXT)";
256 db.execSQL(sql);
257
258}
259
260public static Helper Getinstance(Context mainctx) {
261 if (HelperDb == null) {
262 HelperDb = new Helper(mainctx, DB_Name, null, 1);
263 db = HelperDb.getWritableDatabase();
264 }
265 return HelperDb;
266}
267
268public void addMapDto(LatLng latlng) {
269 ContentValues values = new ContentValues();
270 values.put(lattitude, latlng.latitude);
271 values.put(longitude, latlng.longitude);
272 @SuppressWarnings("unused")
273 long id = db.insert(Helper.TABLE_NAME, null, values);
274
275}
276
277
278public List<MapDto> getAllMapDto() {
279 List<MapDto> md_list = new ArrayList<MapDto>();
280 Cursor cursor = db
281 .query(TABLE_NAME, null, null, null, null, null, null);
282 if (cursor.moveToFirst()) {
283 do {
284 MapDto md = new MapDto();
285 md.setLat(cursor.getDouble(cursor.getColumnIndex(lattitude)));
286 md.setLongii(cursor.getDouble(cursor.getColumnIndex(longitude)));
287 md_list.add(md);
288 } while (cursor.moveToNext());
289 if (cursor != null)
290 ;
291 {
292 cursor.close();
293 }
294 }
295 return md_list;
296
297}
298
299@Override
300public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
301 // TODO Auto-generated method stub
302
303}
304@Override
305protected void finalize() throws Throwable {
306 super.finalize();
307 if (db != null) {
308 db.close();
309
310
311 }
312 if (HelperDb != null) {
313 HelperDb.close();
314 }
315}