Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. public class GetDirectionsAsyncTask extends AsyncTask<Map<String, String>, Object, ArrayList<LatLng>>
  2. {
  3. public static final String USER_CURRENT_LAT = "user_current_lat";
  4. public static final String USER_CURRENT_LONG = "user_current_long";
  5. public static final String DESTINATION_LAT = "destination_lat";
  6. public static final String DESTINATION_LONG = "destination_long";
  7. public static final String DIRECTIONS_MODE = "directions_mode";
  8. private Context context;
  9. private Exception exception;
  10. private ProgressDialog progressDialog;
  11.  
  12. public GetDirectionsAsyncTask(Context context)
  13. {
  14. this.context = context;
  15. }
  16.  
  17. public void onPreExecute()
  18. {
  19. progressDialog = new ProgressDialog(context);
  20. progressDialog.setMessage("Calculating directions");
  21. progressDialog.show();
  22. }
  23.  
  24. @Override
  25. public void onPostExecute(ArrayList result)
  26. {
  27. progressDialog.dismiss();
  28. if (exception == null)
  29. {
  30. context.handleGetDirectionsResult(result);
  31. }
  32. else
  33. {
  34. processException();
  35. }
  36. }
  37.  
  38. @Override
  39. protected ArrayList<LatLng> doInBackground(Map<String, String>... params)
  40. {
  41. Map<String, String> paramMap = params[0];
  42. try
  43. {
  44. LatLng fromPosition = new LatLng(Double.valueOf(paramMap.get(USER_CURRENT_LAT)) , Double.valueOf(paramMap.get(USER_CURRENT_LONG)));
  45. LatLng toPosition = new LatLng(Double.valueOf(paramMap.get(DESTINATION_LAT)) , Double.valueOf(paramMap.get(DESTINATION_LONG)));
  46. GMapV2Direction md = new GMapV2Direction();
  47. Document doc = md.getDocument(fromPosition, toPosition, paramMap.get(DIRECTIONS_MODE));
  48. ArrayList<LatLng> directionPoints = md.getDirection(doc);
  49. return directionPoints;
  50. }
  51. catch (Exception e)
  52. {
  53. exception = e;
  54. return null;
  55. }
  56. }
  57.  
  58. private void processException()
  59. {
  60. Toast.makeText(context, "Error retriving data", 3000).show();
  61. }
  62. }
  63.  
  64. public class NavigationFragment extends Fragment {
  65.  
  66. private static final LatLng AMSTERDAM = new LatLng(2.095892, 102.507584);
  67. private static final LatLng PARIS = new LatLng(2.190484, 102.253525);
  68.  
  69. private GoogleMap map;
  70. private SupportMapFragment fragment;
  71. private LatLngBounds latlngBounds;
  72. private Button bNavigation;
  73. private Polyline newPolyline;
  74. private boolean isTravelingToParis = false;
  75. private boolean marker1 = false;
  76. private int width, height;
  77.  
  78. @Override
  79. public void onCreate(Bundle savedInstanceState) {
  80. super.onCreate(savedInstanceState);
  81. }
  82.  
  83. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  84.  
  85. View rootView = inflater.inflate(R.layout.activity_navigation, container, false);
  86.  
  87. getSreenDimanstions();
  88. fragment = ((SupportMapFragment) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.map));
  89. map = fragment.getMap();
  90. map.setMyLocationEnabled(true);
  91.  
  92. bNavigation = (Button) rootView.findViewById(R.id.bNavigation);
  93. bNavigation.setOnClickListener(new View.OnClickListener() {
  94.  
  95. @Override
  96. public void onClick(View v) {
  97.  
  98. Location myLocation = map.getMyLocation();
  99. double dLatitude = myLocation.getLatitude();
  100. double dLongitude = myLocation.getLongitude();
  101.  
  102.  
  103. LatLng FRANKFURT = new LatLng(dLatitude, dLongitude);
  104. Marker mkrFRANKFURT = map.addMarker(new MarkerOptions()
  105. .position(FRANKFURT)
  106. .title("You are Here")
  107. .snippet("Aloha!")
  108. .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
  109.  
  110. Marker mkr2 = map.addMarker(new MarkerOptions()
  111. .position(AMSTERDAM)
  112. .title("This is AMSTERDAM")
  113. .snippet("Aloha!")
  114. .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
  115.  
  116. latlngBounds = createLatLngBoundsObject(AMSTERDAM, FRANKFURT);
  117. findDirections( AMSTERDAM.latitude, AMSTERDAM.longitude, dLatitude, dLongitude, GMapV2Direction.MODE_DRIVING );
  118. isTravelingToParis = true;
  119. }});
  120.  
  121. return rootView;
  122. }
  123.  
  124. @Override
  125. public void onResume() {
  126.  
  127. super.onResume();
  128.  
  129. Marker mkr2 = map.addMarker(new MarkerOptions()
  130. .position(AMSTERDAM)
  131. .title("This is AMSTERDAM")
  132. .snippet("Aloha!")
  133. .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
  134.  
  135. map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(2.095892, 102.507584) , 14.0f) );
  136.  
  137. }
  138.  
  139. public void handleGetDirectionsResult(ArrayList<LatLng> directionPoints) {
  140. PolylineOptions rectLine = new PolylineOptions().width(5).color(Color.RED);
  141.  
  142. for(int i = 0 ; i < directionPoints.size() ; i++)
  143. {
  144. rectLine.add(directionPoints.get(i));
  145. }
  146. if (newPolyline != null)
  147. {
  148. newPolyline.remove();
  149. }
  150. newPolyline = map.addPolyline(rectLine);
  151. if (isTravelingToParis)
  152. {
  153. Location myLocation = map.getMyLocation();
  154. double dLatitude = myLocation.getLatitude();
  155. double dLongitude = myLocation.getLongitude();
  156.  
  157. LatLng FRANKFURT = new LatLng(dLatitude, dLongitude);
  158. latlngBounds = createLatLngBoundsObject(AMSTERDAM, FRANKFURT);
  159. map.animateCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds, width, height, 150));
  160.  
  161. }
  162. else
  163. {
  164. Location myLocation = map.getMyLocation();
  165. double dLatitude = myLocation.getLatitude();
  166. double dLongitude = myLocation.getLongitude();
  167.  
  168. LatLng FRANKFURT = new LatLng(dLatitude, dLongitude);
  169. latlngBounds = createLatLngBoundsObject(AMSTERDAM, FRANKFURT);
  170. map.animateCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds, width, height, 150));
  171. }
  172. }
  173.  
  174. private void getSreenDimanstions()
  175. {
  176. Display display = this.getActivity().getWindowManager().getDefaultDisplay();
  177. width = display.getWidth();
  178. height = display.getHeight();
  179. }
  180.  
  181. private LatLngBounds createLatLngBoundsObject(LatLng firstLocation, LatLng secondLocation)
  182. {
  183. if (firstLocation != null && secondLocation != null)
  184. {
  185. LatLngBounds.Builder builder = new LatLngBounds.Builder();
  186. builder.include(firstLocation).include(secondLocation);
  187.  
  188. return builder.build();
  189. }
  190. return null;
  191. }
  192.  
  193. public void findDirections(double fromPositionDoubleLat, double fromPositionDoubleLong, double toPositionDoubleLat, double toPositionDoubleLong, String mode)
  194. {
  195. Map<String, String> map = new HashMap<String, String>();
  196. map.put(GetDirectionsAsyncTask.USER_CURRENT_LAT, String.valueOf(fromPositionDoubleLat));
  197. map.put(GetDirectionsAsyncTask.USER_CURRENT_LONG, String.valueOf(fromPositionDoubleLong));
  198. map.put(GetDirectionsAsyncTask.DESTINATION_LAT, String.valueOf(toPositionDoubleLat));
  199. map.put(GetDirectionsAsyncTask.DESTINATION_LONG, String.valueOf(toPositionDoubleLong));
  200. map.put(GetDirectionsAsyncTask.DIRECTIONS_MODE, mode);
  201.  
  202. GetDirectionsAsyncTask asyncTask = new GetDirectionsAsyncTask(this.getActivity());
  203. asyncTask.execute(map);
  204. }
  205. }
  206.  
  207. if(exception == null){
  208. context.handleGetDirectionsResult(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement