Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GetDirectionsAsyncTask extends AsyncTask<Map<String, String>, Object, ArrayList<LatLng>>
- {
- public static final String USER_CURRENT_LAT = "user_current_lat";
- public static final String USER_CURRENT_LONG = "user_current_long";
- public static final String DESTINATION_LAT = "destination_lat";
- public static final String DESTINATION_LONG = "destination_long";
- public static final String DIRECTIONS_MODE = "directions_mode";
- private Context context;
- private Exception exception;
- private ProgressDialog progressDialog;
- public GetDirectionsAsyncTask(Context context)
- {
- this.context = context;
- }
- public void onPreExecute()
- {
- progressDialog = new ProgressDialog(context);
- progressDialog.setMessage("Calculating directions");
- progressDialog.show();
- }
- @Override
- public void onPostExecute(ArrayList result)
- {
- progressDialog.dismiss();
- if (exception == null)
- {
- context.handleGetDirectionsResult(result);
- }
- else
- {
- processException();
- }
- }
- @Override
- protected ArrayList<LatLng> doInBackground(Map<String, String>... params)
- {
- Map<String, String> paramMap = params[0];
- try
- {
- LatLng fromPosition = new LatLng(Double.valueOf(paramMap.get(USER_CURRENT_LAT)) , Double.valueOf(paramMap.get(USER_CURRENT_LONG)));
- LatLng toPosition = new LatLng(Double.valueOf(paramMap.get(DESTINATION_LAT)) , Double.valueOf(paramMap.get(DESTINATION_LONG)));
- GMapV2Direction md = new GMapV2Direction();
- Document doc = md.getDocument(fromPosition, toPosition, paramMap.get(DIRECTIONS_MODE));
- ArrayList<LatLng> directionPoints = md.getDirection(doc);
- return directionPoints;
- }
- catch (Exception e)
- {
- exception = e;
- return null;
- }
- }
- private void processException()
- {
- Toast.makeText(context, "Error retriving data", 3000).show();
- }
- }
- public class NavigationFragment extends Fragment {
- private static final LatLng AMSTERDAM = new LatLng(2.095892, 102.507584);
- private static final LatLng PARIS = new LatLng(2.190484, 102.253525);
- private GoogleMap map;
- private SupportMapFragment fragment;
- private LatLngBounds latlngBounds;
- private Button bNavigation;
- private Polyline newPolyline;
- private boolean isTravelingToParis = false;
- private boolean marker1 = false;
- private int width, height;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.activity_navigation, container, false);
- getSreenDimanstions();
- fragment = ((SupportMapFragment) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.map));
- map = fragment.getMap();
- map.setMyLocationEnabled(true);
- bNavigation = (Button) rootView.findViewById(R.id.bNavigation);
- bNavigation.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Location myLocation = map.getMyLocation();
- double dLatitude = myLocation.getLatitude();
- double dLongitude = myLocation.getLongitude();
- LatLng FRANKFURT = new LatLng(dLatitude, dLongitude);
- Marker mkrFRANKFURT = map.addMarker(new MarkerOptions()
- .position(FRANKFURT)
- .title("You are Here")
- .snippet("Aloha!")
- .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
- Marker mkr2 = map.addMarker(new MarkerOptions()
- .position(AMSTERDAM)
- .title("This is AMSTERDAM")
- .snippet("Aloha!")
- .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
- latlngBounds = createLatLngBoundsObject(AMSTERDAM, FRANKFURT);
- findDirections( AMSTERDAM.latitude, AMSTERDAM.longitude, dLatitude, dLongitude, GMapV2Direction.MODE_DRIVING );
- isTravelingToParis = true;
- }});
- return rootView;
- }
- @Override
- public void onResume() {
- super.onResume();
- Marker mkr2 = map.addMarker(new MarkerOptions()
- .position(AMSTERDAM)
- .title("This is AMSTERDAM")
- .snippet("Aloha!")
- .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
- map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(2.095892, 102.507584) , 14.0f) );
- }
- public void handleGetDirectionsResult(ArrayList<LatLng> directionPoints) {
- PolylineOptions rectLine = new PolylineOptions().width(5).color(Color.RED);
- for(int i = 0 ; i < directionPoints.size() ; i++)
- {
- rectLine.add(directionPoints.get(i));
- }
- if (newPolyline != null)
- {
- newPolyline.remove();
- }
- newPolyline = map.addPolyline(rectLine);
- if (isTravelingToParis)
- {
- Location myLocation = map.getMyLocation();
- double dLatitude = myLocation.getLatitude();
- double dLongitude = myLocation.getLongitude();
- LatLng FRANKFURT = new LatLng(dLatitude, dLongitude);
- latlngBounds = createLatLngBoundsObject(AMSTERDAM, FRANKFURT);
- map.animateCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds, width, height, 150));
- }
- else
- {
- Location myLocation = map.getMyLocation();
- double dLatitude = myLocation.getLatitude();
- double dLongitude = myLocation.getLongitude();
- LatLng FRANKFURT = new LatLng(dLatitude, dLongitude);
- latlngBounds = createLatLngBoundsObject(AMSTERDAM, FRANKFURT);
- map.animateCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds, width, height, 150));
- }
- }
- private void getSreenDimanstions()
- {
- Display display = this.getActivity().getWindowManager().getDefaultDisplay();
- width = display.getWidth();
- height = display.getHeight();
- }
- private LatLngBounds createLatLngBoundsObject(LatLng firstLocation, LatLng secondLocation)
- {
- if (firstLocation != null && secondLocation != null)
- {
- LatLngBounds.Builder builder = new LatLngBounds.Builder();
- builder.include(firstLocation).include(secondLocation);
- return builder.build();
- }
- return null;
- }
- public void findDirections(double fromPositionDoubleLat, double fromPositionDoubleLong, double toPositionDoubleLat, double toPositionDoubleLong, String mode)
- {
- Map<String, String> map = new HashMap<String, String>();
- map.put(GetDirectionsAsyncTask.USER_CURRENT_LAT, String.valueOf(fromPositionDoubleLat));
- map.put(GetDirectionsAsyncTask.USER_CURRENT_LONG, String.valueOf(fromPositionDoubleLong));
- map.put(GetDirectionsAsyncTask.DESTINATION_LAT, String.valueOf(toPositionDoubleLat));
- map.put(GetDirectionsAsyncTask.DESTINATION_LONG, String.valueOf(toPositionDoubleLong));
- map.put(GetDirectionsAsyncTask.DIRECTIONS_MODE, mode);
- GetDirectionsAsyncTask asyncTask = new GetDirectionsAsyncTask(this.getActivity());
- asyncTask.execute(map);
- }
- }
- if(exception == null){
- context.handleGetDirectionsResult(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement