Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. public class TrafficInformation extends Fragment implements LocationListener{
  2.  
  3. private MapView mapView;
  4. private GoogleMap googleMap;
  5. private double myLatitude,myLongitude;
  6. @Override
  7. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  8. Bundle savedInstanceState) {
  9. View view = inflater.inflate(R.layout.traffic_information_fragment, container, false);
  10. mapView = (MapView) view.findViewById(R.id.mapview);
  11. mapView.onCreate(savedInstanceState);
  12. mapView.onResume();
  13. try {
  14. MapsInitializer.initialize(getActivity().getApplicationContext());
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. mapView.getMapAsync(new OnMapReadyCallback() {
  19. @Override
  20. public void onMapReady(GoogleMap mMap) {
  21. googleMap = mMap;
  22. //part of android 6.0 permission
  23. int flag = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION);
  24. if (flag != PackageManager.PERMISSION_GRANTED) {
  25. ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
  26. } else {
  27. googleMap.setMyLocationEnabled(true);
  28. }
  29. // For dropping a marker at a point on the Map
  30.  
  31. //i set my current location over here-------------------
  32. LatLng sydney = new LatLng(myLatitude, myLongitude);
  33. googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));
  34.  
  35. //For zooming automatically to the location of the marker
  36.  
  37. CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(16).build();
  38. googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
  39. }
  40. });
  41.  
  42. return view;
  43. }
  44.  
  45. @Override
  46. public void onLocationChanged(Location location) {
  47. myLatitude=location.getLatitude();
  48. myLongitude=location.getLongitude();
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement