Advertisement
Guest User

Untitled

a guest
May 15th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.94 KB | None | 0 0
  1. package com.example.mario.afinal;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.content.pm.PackageManager;
  5. import android.graphics.Color;
  6. import android.location.Location;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.os.StrictMode;
  10. import android.support.v4.app.ActivityCompat;
  11. import android.support.v4.app.FragmentActivity;
  12. import android.support.v4.content.ContextCompat;
  13. import android.util.Log;
  14. import android.widget.Button;
  15. import android.widget.SimpleAdapter;
  16. import android.widget.Toast;
  17.  
  18. import com.google.android.gms.common.ConnectionResult;
  19. import com.google.android.gms.common.api.GoogleApiClient;
  20. import com.google.android.gms.location.LocationListener;
  21. import com.google.android.gms.location.LocationRequest;
  22. import com.google.android.gms.location.LocationServices;
  23. import com.google.android.gms.maps.CameraUpdateFactory;
  24. import com.google.android.gms.maps.GoogleMap;
  25. import com.google.android.gms.maps.OnMapReadyCallback;
  26. import com.google.android.gms.maps.SupportMapFragment;
  27. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  28. import com.google.android.gms.maps.model.LatLng;
  29. import com.google.android.gms.maps.model.Marker;
  30. import com.google.android.gms.maps.model.MarkerOptions;
  31. import com.google.android.gms.maps.model.Polyline;
  32. import com.google.android.gms.maps.model.PolylineOptions;
  33.  
  34. import java.sql.Connection;
  35. import java.sql.DriverManager;
  36. import java.sql.ResultSet;
  37. import java.sql.SQLException;
  38. import java.sql.Statement;
  39. import java.util.ArrayList;
  40. import java.util.HashMap;
  41. import java.util.List;
  42. import java.util.Map;
  43.  
  44. public class PlanYourRouteMap extends FragmentActivity implements OnMapReadyCallback,
  45. GoogleApiClient.ConnectionCallbacks,
  46. GoogleApiClient.OnConnectionFailedListener,
  47. LocationListener {
  48.  
  49. Button btn;
  50.  
  51. SimpleAdapter ADAhere;
  52.  
  53. private GoogleMap mMap;
  54.  
  55. GoogleApiClient mGoogleApiClient;
  56. LocationRequest mLocationRequest;
  57. Location mLastLocation;
  58. Marker mCurrLocationMarker;
  59. public double longitude,latitude;
  60.  
  61.  
  62.  
  63.  
  64. /****************************Database Connection Variables*************************************/
  65.  
  66. ConnectionClass connectionclass;
  67. String usernameS;
  68. String datets;
  69. String call, db, un, passwords;
  70. Connection connect;
  71. ResultSet rs;
  72.  
  73. @SuppressLint("NewApi")
  74. private Connection CONN(String _user, String _pass, String _DB, String _server)
  75. {
  76. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  77. StrictMode.setThreadPolicy(policy);
  78. Connection conn = null;
  79. String ConnURL = null;
  80. try{
  81. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  82. ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"
  83. + "databaseName=" + _DB + ";user=" + _user + ";password=" + _pass + ";";
  84. conn = DriverManager.getConnection(ConnURL);
  85. }catch (SQLException se){
  86. Log.e("ERROR", se.getMessage());
  87. }catch (ClassNotFoundException e){
  88. Log.e("ERROR", e.getMessage());
  89. }catch (Exception e){
  90. Log.e("ERROR", e.getMessage());
  91. }
  92. return conn;
  93. }
  94.  
  95.  
  96. @Override
  97. protected void onCreate(Bundle savedInstanceState) {
  98. super.onCreate(savedInstanceState);
  99. setContentView(R.layout.activity_plan_your_route_map);
  100.  
  101. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  102. checkLocationPermission();
  103. }
  104. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  105. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  106. .findFragmentById(R.id.map);
  107. mapFragment.getMapAsync(this);
  108.  
  109. }
  110.  
  111.  
  112.  
  113.  
  114. /**
  115. * Manipulates the map once available.
  116. * This callback is triggered when the map is ready to be used.
  117. * This is where we can add markers or lines, add listeners or move the camera. In this case,
  118. * we just add a marker near Sydney, Australia.
  119. * If Google Play services is not installed on the device, the user will be prompted to install
  120. * it inside the SupportMapFragment. This method will only be triggered once the user has
  121. * installed Google Play services and returned to the app.
  122. */
  123. @Override
  124. public void onMapReady(GoogleMap googleMap) {
  125.  
  126. Double lat, log;
  127.  
  128.  
  129.  
  130. mMap = googleMap;
  131.  
  132. mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  133.  
  134.  
  135. //Initialize Google Play Services
  136. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  137. if (ContextCompat.checkSelfPermission(this,
  138. android.Manifest.permission.ACCESS_FINE_LOCATION)
  139. == PackageManager.PERMISSION_GRANTED) {
  140. buildGoogleApiClient();
  141. mMap.setMyLocationEnabled(true);
  142. }
  143. }
  144. else {
  145. buildGoogleApiClient();
  146. mMap.setMyLocationEnabled(true);
  147. }
  148.  
  149.  
  150. /***********************DATABASE CONNECTION VARIABLE***************************************/
  151. connectionclass = new ConnectionClass();
  152. call = connectionclass.getIp();
  153. un = connectionclass.getUn();
  154. passwords = connectionclass.getPassword();
  155. db = connectionclass.getDb();
  156. connect = CONN(un, passwords, db, call);
  157. String querycmd = "select * from trafficNews2";
  158. try {
  159. Statement statement = connect.createStatement();
  160. rs = statement.executeQuery(querycmd);
  161. List<Map<String, String>> data = null;
  162. data = new ArrayList<Map<String, String>>();
  163.  
  164. while (rs.next()) {
  165. Map<String, String> datanum = new HashMap<String, String>();
  166. datanum.put("A", rs.getString("streetName"));
  167. datanum.put("B", rs.getString("startTime"));
  168. datanum.put("C", rs.getString("stopTime"));
  169. datanum.put("D", rs.getString("notes"));
  170. datanum.put("E", rs.getString("loc_long"));
  171. datanum.put("F", rs.getString("loc_lat"));
  172. datanum.put("G", rs.getString("start_long"));
  173. datanum.put("H", rs.getString("start_lat"));
  174. datanum.put("I", rs.getString("stop_long"));
  175. datanum.put("J", rs.getString("stop_lat"));
  176.  
  177. data.add(datanum);
  178.  
  179. String name = rs.getString("streetName");
  180. String longitude = rs.getString("loc_long");
  181. String latitude = rs.getString("loc_lat");
  182.  
  183. String startLon = rs.getString("start_long");
  184. String startLat = rs.getString("start_lat");
  185.  
  186. String stopLon = rs.getString("stop_long");
  187. String stopLat = rs.getString("stop_lat");
  188.  
  189.  
  190. Double longi = Double.parseDouble(longitude);
  191. Double lati = Double.parseDouble(latitude);
  192.  
  193. Double strLongi = Double.parseDouble(startLon);
  194. Double strLati = Double.parseDouble(startLat);
  195. Double stpLongi = Double.parseDouble(stopLon);
  196. Double stpLati = Double.parseDouble(stopLat);
  197.  
  198. mMap = googleMap;
  199.  
  200. // Add a marker in Sydney and move the camera
  201. LatLng loc = new LatLng(longi,lati);
  202. mMap.addMarker(new MarkerOptions().position(loc).title(name));
  203. mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
  204. mMap.moveCamera(CameraUpdateFactory.zoomTo(7.5f));
  205.  
  206. LatLng start = new LatLng(strLongi,strLati);
  207. mMap.addMarker(new MarkerOptions().position(start).title("Starting Point"));
  208. mMap.moveCamera(CameraUpdateFactory.newLatLng(start));
  209.  
  210. LatLng stop = new LatLng(stpLongi,stpLati);
  211. mMap.addMarker(new MarkerOptions().position(stop).title("End Point"));
  212. mMap.moveCamera(CameraUpdateFactory.newLatLng(stop));
  213.  
  214. Polyline line = mMap.addPolyline(new PolylineOptions()
  215. .add(start,loc,stop)
  216. .width(60)
  217. .color(Color.RED));
  218.  
  219.  
  220. }
  221.  
  222.  
  223. } catch (SQLException e) {
  224. e.printStackTrace();
  225. }
  226.  
  227.  
  228.  
  229. }
  230.  
  231.  
  232.  
  233. protected synchronized void buildGoogleApiClient() {
  234. mGoogleApiClient = new GoogleApiClient.Builder(this)
  235. .addConnectionCallbacks(this)
  236. .addOnConnectionFailedListener(this)
  237. .addApi(LocationServices.API)
  238. .build();
  239. mGoogleApiClient.connect();
  240. }
  241.  
  242. @Override
  243. public void onConnected(Bundle bundle) {
  244.  
  245. mLocationRequest = new LocationRequest();
  246. mLocationRequest.setInterval(1000);
  247. mLocationRequest.setFastestInterval(1000);
  248. mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  249. if (ContextCompat.checkSelfPermission(this,
  250. android.Manifest.permission.ACCESS_FINE_LOCATION)
  251. == PackageManager.PERMISSION_GRANTED) {
  252. LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
  253. }
  254.  
  255. }
  256.  
  257. @Override
  258. public void onConnectionSuspended(int i) {
  259.  
  260. }
  261.  
  262. @Override
  263. public void onLocationChanged(Location location) {
  264.  
  265. mLastLocation = location;
  266. if (mCurrLocationMarker != null) {
  267. mCurrLocationMarker.remove();
  268. }
  269.  
  270. //Place current location marker
  271. LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
  272. longitude = location.getLongitude();
  273. latitude = location.getLatitude();
  274. MarkerOptions markerOptions = new MarkerOptions();
  275. markerOptions.position(latLng);
  276. markerOptions.title("Current Position");
  277. markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
  278. mCurrLocationMarker = mMap.addMarker(markerOptions);
  279.  
  280. //move map camera
  281. mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  282.  
  283. //stop location updates
  284. if (mGoogleApiClient != null) {
  285. LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
  286. }
  287.  
  288. }
  289.  
  290. @Override
  291. public void onConnectionFailed(ConnectionResult connectionResult) {
  292.  
  293. }
  294.  
  295. public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
  296. public boolean checkLocationPermission(){
  297. if (ContextCompat.checkSelfPermission(this,
  298. android.Manifest.permission.ACCESS_FINE_LOCATION)
  299. != PackageManager.PERMISSION_GRANTED) {
  300.  
  301. // Asking user if explanation is needed
  302. if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  303. android.Manifest.permission.ACCESS_FINE_LOCATION)) {
  304.  
  305. // Show an explanation to the user *asynchronously* -- don't block
  306. // this thread waiting for the user's response! After the user
  307. // sees the explanation, try again to request the permission.
  308.  
  309. //Prompt the user once explanation has been shown
  310. ActivityCompat.requestPermissions(this,
  311. new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
  312. MY_PERMISSIONS_REQUEST_LOCATION);
  313.  
  314.  
  315. } else {
  316. // No explanation needed, we can request the permission.
  317. ActivityCompat.requestPermissions(this,
  318. new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
  319. MY_PERMISSIONS_REQUEST_LOCATION);
  320. }
  321. return false;
  322. } else {
  323. return true;
  324. }
  325. }
  326.  
  327. @Override
  328. public void onRequestPermissionsResult(int requestCode,
  329. String permissions[], int[] grantResults) {
  330. switch (requestCode) {
  331. case MY_PERMISSIONS_REQUEST_LOCATION: {
  332. // If request is cancelled, the result arrays are empty.
  333. if (grantResults.length > 0
  334. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  335.  
  336. // permission was granted. Do the
  337. // contacts-related task you need to do.
  338. if (ContextCompat.checkSelfPermission(this,
  339. android.Manifest.permission.ACCESS_FINE_LOCATION)
  340. == PackageManager.PERMISSION_GRANTED) {
  341.  
  342. if (mGoogleApiClient == null) {
  343. buildGoogleApiClient();
  344. }
  345. mMap.setMyLocationEnabled(true);
  346. }
  347.  
  348. } else {
  349.  
  350. // Permission denied, Disable the functionality that depends on this permission.
  351. Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
  352. }
  353. return;
  354. }
  355.  
  356. // other 'case' lines to check for other permissions this app might request.
  357. // You can add here other case statements according to your requirement.
  358. }
  359. }
  360.  
  361.  
  362. }
  363.  
  364.  
  365.  
  366. $$$$$$$$$$$$$$$$$$$$$$$$
  367.  
  368.  
  369. package com.example.mario.afinal;
  370.  
  371. import android.os.Bundle;
  372. import android.support.design.widget.FloatingActionButton;
  373. import android.support.design.widget.Snackbar;
  374. import android.view.View;
  375. import android.support.design.widget.NavigationView;
  376. import android.support.v4.view.GravityCompat;
  377. import android.support.v4.widget.DrawerLayout;
  378. import android.support.v7.app.ActionBarDrawerToggle;
  379. import android.support.v7.app.AppCompatActivity;
  380. import android.support.v7.widget.Toolbar;
  381. import android.view.Menu;
  382. import android.view.MenuItem;
  383.  
  384. public class PlanYourRouteMapNavigation extends AppCompatActivity
  385. implements NavigationView.OnNavigationItemSelectedListener {
  386.  
  387. @Override
  388. protected void onCreate(Bundle savedInstanceState) {
  389. super.onCreate(savedInstanceState);
  390. setContentView(R.layout.activity_plan_your_route_map_navigation);
  391. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  392. setSupportActionBar(toolbar);
  393.  
  394. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  395. fab.setOnClickListener(new View.OnClickListener() {
  396. @Override
  397. public void onClick(View view) {
  398. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  399. .setAction("Action", null).show();
  400. }
  401. });
  402.  
  403. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  404. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  405. this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  406. drawer.setDrawerListener(toggle);
  407. toggle.syncState();
  408.  
  409. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  410. navigationView.setNavigationItemSelectedListener(this);
  411. }
  412.  
  413. @Override
  414. public void onBackPressed() {
  415. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  416. if (drawer.isDrawerOpen(GravityCompat.START)) {
  417. drawer.closeDrawer(GravityCompat.START);
  418. } else {
  419. super.onBackPressed();
  420. }
  421. }
  422.  
  423. @Override
  424. public boolean onCreateOptionsMenu(Menu menu) {
  425. // Inflate the menu; this adds items to the action bar if it is present.
  426. getMenuInflater().inflate(R.menu.plan_your_route_map_navigation, menu);
  427. return true;
  428. }
  429.  
  430. @Override
  431. public boolean onOptionsItemSelected(MenuItem item) {
  432. // Handle action bar item clicks here. The action bar will
  433. // automatically handle clicks on the Home/Up button, so long
  434. // as you specify a parent activity in AndroidManifest.xml.
  435. int id = item.getItemId();
  436.  
  437. //noinspection SimplifiableIfStatement
  438. if (id == R.id.action_settings) {
  439. return true;
  440. }
  441.  
  442. return super.onOptionsItemSelected(item);
  443. }
  444.  
  445. @SuppressWarnings("StatementWithEmptyBody")
  446. @Override
  447. public boolean onNavigationItemSelected(MenuItem item) {
  448. // Handle navigation view item clicks here.
  449. int id = item.getItemId();
  450.  
  451. if (id == R.id.nav_camera) {
  452. // Handle the camera action
  453. } else if (id == R.id.nav_gallery) {
  454.  
  455. } else if (id == R.id.nav_slideshow) {
  456.  
  457. } else if (id == R.id.nav_manage) {
  458.  
  459. } else if (id == R.id.nav_share) {
  460.  
  461. } else if (id == R.id.nav_send) {
  462.  
  463. }
  464.  
  465. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  466. drawer.closeDrawer(GravityCompat.START);
  467. return true;
  468. }
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement