Advertisement
Guest User

code1

a guest
May 15th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.38 KB | None | 0 0
  1. package com.example.mysqltest;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Timer;
  13. import java.util.TimerTask;
  14.  
  15.  
  16. import org.json.JSONObject;
  17.  
  18. import android.content.Context;
  19. import android.graphics.Color;
  20. import android.location.Criteria;
  21. import android.location.Location;
  22. import android.location.LocationListener;
  23. import android.os.AsyncTask;
  24. import android.os.Bundle;
  25. import android.support.v4.app.Fragment;
  26. import android.support.v4.app.FragmentActivity;
  27. import android.util.Log;
  28. import android.view.LayoutInflater;
  29. import android.view.Menu;
  30. import android.view.View;
  31. import android.view.ViewGroup;
  32. import android.widget.RadioButton;
  33. import android.widget.RadioGroup;
  34. import android.widget.RadioGroup.OnCheckedChangeListener;
  35. import android.widget.Toast;
  36.  
  37. import com.google.android.gms.maps.GoogleMap;
  38. import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
  39. import com.google.android.gms.maps.SupportMapFragment;
  40. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  41. import com.google.android.gms.maps.model.LatLng;
  42. import com.google.android.gms.maps.model.MarkerOptions;
  43. import com.google.android.gms.maps.model.PolylineOptions;
  44.  
  45. import android.location.Criteria;
  46. import android.location.Location;
  47. import android.location.LocationListener;
  48. import android.location.LocationManager;
  49.  
  50.  
  51.  
  52. public class googleXdon extends Fragment implements LocationListener {
  53.  
  54. static GoogleMap map;
  55. static LatLng myPosition;
  56. static RadioButton rbDriving;
  57. static RadioButton rbBiCycling;
  58. static RadioButton rbWalking;
  59. RadioGroup rgModes;
  60. static ArrayList<LatLng> markerPoints;
  61. static int mMode=0;
  62. final static int MODE_DRIVING=0;
  63. final static int MODE_BICYCLING=1;
  64. final static int MODE_WALKING=2;
  65. int fragVal;
  66.  
  67. static Context ontext2;
  68.  
  69. static googleXdon init(int val, Context contexts) {
  70. googleXdon X = new googleXdon();
  71.  
  72. ontext2 = contexts;
  73. // Supply val input as an argument.
  74. Bundle args = new Bundle();
  75. args.putInt("val", val);
  76. X.setArguments(args);
  77. return X;
  78. }
  79.  
  80. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  81. Bundle savedInstanceState) {
  82.  
  83. View layoutView = inflater.inflate(R.layout.actgooglex, container,
  84. false);
  85.  
  86. fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
  87.  
  88. // Getting reference to rb_driving
  89. rbDriving = (RadioButton) layoutView.findViewById(R.id.rb_driving);
  90.  
  91. // Getting reference to rb_bicylcing
  92. rbBiCycling = (RadioButton) layoutView.findViewById(R.id.rb_bicycling);
  93.  
  94. // Getting reference to rb_walking
  95. rbWalking = (RadioButton) layoutView.findViewById(R.id.rb_walking);
  96.  
  97. // Getting Reference to rg_modes
  98. rgModes = (RadioGroup) layoutView.findViewById(R.id.rg_modes);
  99.  
  100. rgModes.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  101.  
  102.  
  103. @Override
  104. public void onCheckedChanged(RadioGroup group, int checkedId) {
  105.  
  106. // Checks, whether start and end locations are captured
  107. if(markerPoints.size() >= 2){
  108. LatLng origin = markerPoints.get(0);
  109. LatLng dest = markerPoints.get(1);
  110.  
  111. // Getting URL to the Google Directions API
  112. String url = getDirectionsUrl(origin, dest);
  113.  
  114. DownloadTask downloadTask = new DownloadTask();
  115.  
  116. // Start downloading json data from Google Directions API
  117. downloadTask.execute(url);
  118. }
  119. }
  120. });
  121.  
  122. // Initializing
  123. markerPoints = new ArrayList<LatLng>();
  124.  
  125. // Getting reference to SupportMapFragment of the activity_main
  126. SupportMapFragment fm = (SupportMapFragment)getFragmentManager().findFragmentById(R.id.map);
  127.  
  128. // Getting Map for the SupportMapFragment
  129. map = fm.getMap();
  130.  
  131. // Enable MyLocation Button in the Map
  132. map.setMyLocationEnabled(true);
  133.  
  134. // Getting LocationManager object from System Service LOCATION_SERVICE
  135. /*LocationManager locationManager = (LocationManager) ontext2.getSystemService(Context.LOCATION_SERVICE);
  136. // Creating a criteria object to retrieve provider
  137. Criteria criteria = new Criteria();
  138.  
  139. // Getting the name of the best provider
  140. String provider = locationManager.getBestProvider(criteria, true);
  141.  
  142. // Getting Current Location
  143. Location location = locationManager.getLastKnownLocation(provider);
  144.  
  145. if(location!=null) {
  146. // Getting latitude of the current location
  147. double latitude = location.getLatitude();
  148.  
  149. // Getting longitude of the current location
  150. double longitude = location.getLongitude();
  151.  
  152. // Creating a LatLng object for the current location
  153. LatLng latLng = new LatLng(latitude, longitude);
  154.  
  155. myPosition = new LatLng(latitude, longitude);
  156.  
  157. markerPoints.add(myPosition);*/
  158.  
  159. // Setting onclick event listener for the map
  160. /* map.setOnMapClickListener(new OnMapClickListener() {
  161.  
  162. @Override
  163. public void onMapClick(LatLng point) {
  164.  
  165. // Already two locations value is 1
  166. if(markerPoints.size()>1){
  167. markerPoints.clear();
  168. map.clear();
  169. }
  170.  
  171. // Adding new item to the ArrayList
  172. markerPoints.add(point);
  173.  
  174. // Draws Start and Stop markers on the Google Map
  175. drawStartStopMarkers();
  176.  
  177. // Checks, whether start and end locations are captured
  178. if(markerPoints.size() >= 2){
  179. LatLng origin = markerPoints.get(0);
  180. LatLng dest = markerPoints.get(1);
  181.  
  182. // Getting URL to the Google Directions API
  183. String url = getDirectionsUrl(origin, dest);
  184.  
  185. DownloadTask downloadTask = new DownloadTask();
  186.  
  187. // Start downloading json data from Google Directions API
  188. downloadTask.execute(url);
  189. }
  190.  
  191. }
  192. });
  193.  
  194.  
  195. }*/
  196. return layoutView;
  197. }
  198.  
  199.  
  200.  
  201. //este antes no era final
  202.  
  203.  
  204. public static void agregarMarket(LatLng point) {
  205.  
  206.  
  207. // Already two locations
  208. if(markerPoints.size()>1){
  209. markerPoints.clear();
  210. map.clear();
  211. }
  212.  
  213. // Getting LocationManager object from System Service LOCATION_SERVICE
  214. LocationManager locationManager = (LocationManager) ontext2.getSystemService(Context.LOCATION_SERVICE);
  215.  
  216. //LocationManager mlocManager = (LocationManager) ontext2.getSystemService(Context.LOCATION_SERVICE);
  217. googleXdon mlocListener = new googleXdon();
  218. locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 0, mlocListener);
  219.  
  220.  
  221. // Creating a criteria object to retrieve provider
  222. Criteria criteria = new Criteria();
  223.  
  224. // Getting the name of the best provider
  225. String provider = locationManager.getBestProvider(criteria, true);
  226.  
  227. // Getting Current Location
  228. Location location = locationManager.getLastKnownLocation(provider);
  229.  
  230.  
  231.  
  232. if(location!=null) {
  233. // Getting latitude of the current location
  234. double latitude = location.getLatitude();
  235.  
  236. // Getting longitude of the current location
  237. double longitude = location.getLongitude();
  238.  
  239. // Creating a LatLng object for the current location
  240. LatLng latLng = new LatLng(latitude, longitude);
  241.  
  242. myPosition = new LatLng(latitude, longitude);
  243.  
  244. markerPoints.add(myPosition);
  245.  
  246. // Adding new item to the ArrayList
  247. markerPoints.add(point);
  248.  
  249. // Draws Start and Stop markers on the Google Map
  250. drawStartStopMarkers();
  251.  
  252. // Checks, whether start and end locations are captured
  253. if(markerPoints.size() >= 2){
  254. LatLng origin = markerPoints.get(0);
  255. LatLng dest = markerPoints.get(1);
  256.  
  257. // Getting URL to the Google Directions API
  258. String url = getDirectionsUrl(origin, dest);
  259.  
  260. DownloadTask downloadTask = new DownloadTask();
  261.  
  262. // Start downloading json data from Google Directions API
  263. downloadTask.execute(url);
  264. }
  265. }
  266.  
  267. }
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274. // handler.postDelayed(r, 300000 );
  275.  
  276. // Drawing Start and Stop locations
  277. private static void drawStartStopMarkers(){
  278.  
  279. for(int i=0;i<markerPoints.size();i++){
  280.  
  281. // Creating MarkerOptions
  282. MarkerOptions options = new MarkerOptions();
  283.  
  284.  
  285. // Setting the position of the marker
  286. options.position(markerPoints.get(i) );
  287.  
  288. /**
  289. * For the start location, the color of marker is GREEN and
  290. * for the end location, the color of marker is RED.
  291. */
  292. if(i==0){
  293. options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
  294. }else if(i==1){
  295. options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
  296. }
  297.  
  298. // Add new marker to the Google Map Android API V2
  299. map.addMarker(options);
  300. }
  301. }
  302.  
  303.  
  304. private static String getDirectionsUrl(LatLng origin,LatLng dest){
  305.  
  306. // Origin of route
  307. String str_origin = "origin="+origin.latitude+","+origin.longitude;
  308.  
  309. // Destination of route
  310. String str_dest = "destination="+dest.latitude+","+dest.longitude;
  311.  
  312. // Sensor enabled
  313. String sensor = "sensor=false";
  314.  
  315. // Travelling Mode
  316. String mode = "mode=driving";
  317.  
  318. if(rbDriving.isChecked()){
  319. mode = "mode=driving";
  320. mMode = 0 ;
  321. }else if(rbBiCycling.isChecked()){
  322. mode = "mode=bicycling";
  323. mMode = 1;
  324. }else if(rbWalking.isChecked()){
  325. mode = "mode=walking";
  326. mMode = 2;
  327. }
  328.  
  329.  
  330. // Building the parameters to the web service
  331. String parameters = str_origin+"&"+str_dest+"&"+sensor+"&"+mode;
  332.  
  333. // Output format
  334. String output = "json";
  335.  
  336. // Building the url to the web service
  337. String url = "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters;
  338. return url;
  339. }
  340.  
  341. /** A method to download json data from url */
  342. private static String downloadUrl(String strUrl) throws IOException{
  343. String data = "";
  344. InputStream iStream = null;
  345. HttpURLConnection urlConnection = null;
  346. try{
  347. URL url = new URL(strUrl);
  348.  
  349. // Creating an http connection to communicate with url
  350. urlConnection = (HttpURLConnection) url.openConnection();
  351.  
  352. // Connecting to url
  353. urlConnection.connect();
  354.  
  355. // Reading data from url
  356. iStream = urlConnection.getInputStream();
  357.  
  358. BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
  359.  
  360. StringBuffer sb = new StringBuffer();
  361.  
  362. String line = "";
  363. while( ( line = br.readLine()) != null){
  364. sb.append(line);
  365. }
  366.  
  367. data = sb.toString();
  368.  
  369. br.close();
  370.  
  371. }catch(Exception e){
  372. Log.d("Exception while downloading url", e.toString());
  373. }finally{
  374. iStream.close();
  375. urlConnection.disconnect();
  376. }
  377. return data;
  378. }
  379.  
  380.  
  381.  
  382. // Fetches data from url passed
  383. static class DownloadTask extends AsyncTask<String, Void, String>{
  384.  
  385. // Downloading data in non-ui thread
  386. @Override
  387. protected String doInBackground(String... url) {
  388.  
  389. // For storing data from web service
  390. String data = "";
  391.  
  392. try{
  393. // Fetching the data from web service
  394. data = downloadUrl(url[0]);
  395. }catch(Exception e){
  396. Log.d("Background Task",e.toString());
  397. }
  398. return data;
  399. }
  400.  
  401. // Executes in UI thread, after the execution of
  402. // doInBackground()
  403. @Override
  404. protected void onPostExecute(String result) {
  405. super.onPostExecute(result);
  406.  
  407. ParserTask parserTask = new ParserTask();
  408.  
  409. // Invokes the thread for parsing the JSON data
  410. parserTask.execute(result);
  411.  
  412. }
  413. }
  414.  
  415. /** A class to parse the Google Places in JSON format */
  416. static class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>> >{
  417.  
  418. // Parsing the data in non-ui thread
  419. @Override
  420. protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {
  421.  
  422. JSONObject jObject;
  423. List<List<HashMap<String, String>>> routes = null;
  424.  
  425. try{
  426. jObject = new JSONObject(jsonData[0]);
  427. DirectionsJSONParser parser = new DirectionsJSONParser();
  428.  
  429. // Starts parsing data
  430. routes = parser.parse(jObject);
  431. }catch(Exception e){
  432. e.printStackTrace();
  433. }
  434. return routes;
  435. }
  436.  
  437. // Executes in UI thread, after the parsing process
  438. @Override
  439. protected void onPostExecute(List<List<HashMap<String, String>>> result) {
  440. ArrayList<LatLng> points = null;
  441. PolylineOptions lineOptions = null;
  442. MarkerOptions markerOptions = new MarkerOptions();
  443.  
  444. // Traversing through all the routes
  445. for(int i=0;i<result.size();i++){
  446. points = new ArrayList<LatLng>();
  447. lineOptions = new PolylineOptions();
  448.  
  449. // Fetching i-th route
  450. List<HashMap<String, String>> path = result.get(i);
  451.  
  452. // Fetching all the points in i-th route
  453. for(int j=0;j<path.size();j++){
  454. HashMap<String,String> point = path.get(j);
  455.  
  456. double lat = Double.parseDouble(point.get("lat"));
  457. double lng = Double.parseDouble(point.get("lng"));
  458. LatLng position = new LatLng(lat, lng);
  459.  
  460. points.add(position);
  461. }
  462.  
  463. // Adding all the points in the route to LineOptions
  464. lineOptions.addAll(points);
  465. lineOptions.width(2);
  466.  
  467. // Changing the color polyline according to the mode
  468. if(mMode==MODE_DRIVING)
  469. lineOptions.color(Color.RED);
  470. else if(mMode==MODE_BICYCLING)
  471. lineOptions.color(Color.GREEN);
  472. else if(mMode==MODE_WALKING)
  473. lineOptions.color(Color.BLUE);
  474. }
  475.  
  476. if(result.size()<1){
  477. Toast.makeText(ontext2, "No Points", Toast.LENGTH_SHORT).show();
  478. return;
  479. }
  480.  
  481. // Drawing polyline in the Google Map for the i-th route
  482. map.addPolyline(lineOptions);
  483.  
  484. }
  485. }
  486.  
  487. /*@Override
  488. public boolean onCreateOptionsMenu(Menu menu) {
  489. // Inflate the menu; this adds items to the action bar if it is present.
  490. getMenuInflater().inflate(R.menu.main, menu);
  491. return true;
  492. } */
  493.  
  494. @Override
  495. public void onLocationChanged(Location location)
  496. {
  497.  
  498. LatLng location2 = new LatLng(location.getLatitude(), location.getLongitude());
  499. agregarMarket(location2);
  500.  
  501. System.out.println(location.toString());
  502. Toast.makeText(ontext2, location.toString(), Toast.LENGTH_SHORT).show();
  503. //Set marker here
  504. // LatLng pos=new LatLng(location.getLatitude(), location.getLongitude());
  505. // map.addMarker(new MarkerOptions().position(pos).icon(BitmapDescriptorFactory.fromResource(markericon)));
  506.  
  507. }
  508.  
  509. @Override
  510. public void onProviderDisabled(String provider)
  511. {
  512.  
  513. }
  514.  
  515. @Override
  516. public void onProviderEnabled(String provider)
  517. {
  518.  
  519. }
  520.  
  521. @Override
  522. public void onStatusChanged(String provider, int status, Bundle extras)
  523. {
  524.  
  525. }
  526.  
  527.  
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement