Advertisement
Mirnijat

Maps Activity

Apr 4th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.64 KB | None | 0 0
  1. package com.dadashova.aytaj.bakubustest;
  2.  
  3. import android.Manifest;
  4. import android.content.Context;
  5. import android.content.pm.PackageManager;
  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.location.LocationManager;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.support.annotation.NonNull;
  12. import android.support.v4.app.ActivityCompat;
  13. import android.support.v4.content.ContextCompat;
  14. import android.support.v7.app.AppCompatActivity;
  15.  
  16. import com.google.android.gms.maps.CameraUpdateFactory;
  17. import com.google.android.gms.maps.GoogleMap;
  18. import com.google.android.gms.maps.OnMapReadyCallback;
  19. import com.google.android.gms.maps.SupportMapFragment;
  20. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  21. import com.google.android.gms.maps.model.LatLng;
  22. import com.google.android.gms.maps.model.MarkerOptions;
  23.  
  24. public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
  25.  
  26.     LocationManager locationManager;
  27.     LocationListener locationListener;
  28.     LatLng userLocation;
  29.     MarkerOptions userMarker;
  30.  
  31.     private GoogleMap mMap;
  32.     public static final int MY_LOCATION_REQUEST_CODE = 1;
  33.     //https://developers.google.com/maps/documentation/android-sdk/location
  34.     //https://google-developer-training.gitbooks.io/android-developer-advanced-course-practicals/unit-4-add-geo-features-to-your-apps/lesson-9-mapping/9-1-p-add-a-google-map-to-your-app/9-1-p-add-a-google-map-to-your-app.html#chapterstart
  35. //https://google-developer-training.gitbooks.io/android-developer-advanced-course-practicals/unit-4-add-geo-features-to-your-apps/lesson-9-mapping/9-1-p-add-a-google-map-to-your-app/9-1-p-add-a-google-map-to-your-app.html
  36.  
  37.     @Override
  38.     public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[]grantResults) {
  39.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  40.  
  41.         if (requestCode == 1){
  42.  
  43.             if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
  44.                 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  45.                         == PackageManager.PERMISSION_GRANTED) {
  46.  
  47.                     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 25, locationListener);
  48.  
  49.                 }
  50.  
  51.             }
  52.  
  53.         }
  54.  
  55.     }
  56.  
  57.     @Override
  58.     protected void onCreate(Bundle savedInstanceState) {
  59.         super.onCreate(savedInstanceState);
  60.         setContentView(R.layout.activity_maps);
  61.  
  62.  
  63.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  64.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  65.                 .findFragmentById(R.id.map);
  66.         mapFragment.getMapAsync(this);
  67.     }
  68.  
  69.  
  70.     /**
  71.      * Manipulates the map once available.
  72.      * This callback is triggered when the map is ready to be used.
  73.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
  74.      * we just add a marker near Sydney, Australia.
  75.      * If Google Play services is not installed on the device, the user will be prompted to install
  76.      * it inside the SupportMapFragment. This method will only be triggered once the user has
  77.      * installed Google Play services and returned to the app.
  78.      */
  79.  
  80.     @Override
  81.     public void onMapReady(GoogleMap googleMap) {
  82.  
  83.         mMap = googleMap;
  84.  
  85.         userMarker = new MarkerOptions();
  86.         userMarker.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_bus));
  87.         userMarker.title("Your Location");
  88.  
  89.  
  90.         locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  91.         locationListener = new LocationListener() {
  92.             @Override
  93.             public void onLocationChanged(Location location) {
  94.  
  95.                 userLocation = new LatLng(location.getLatitude(), location.getLongitude());
  96.                 mMap.addMarker(userMarker.position(userLocation).snippet("Lat: " + location.getLatitude() + ", Long:" + location.getLongitude()));
  97.                 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
  98.                 mMap.moveCamera(CameraUpdateFactory.newLatLng    (userLocation));
  99.  
  100.             }
  101.  
  102.             @Override
  103.             public void onStatusChanged(String provider, int status, Bundle extras) {
  104.  
  105.             }
  106.  
  107.             @Override
  108.             public void onProviderEnabled(String provider) {
  109.  
  110.             }
  111.  
  112.             @Override
  113.             public void onProviderDisabled(String provider) {
  114.  
  115.             }
  116.         };
  117.  
  118.  
  119.         if (Build.VERSION.SDK_INT >= 23) {
  120.             if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  121.                     != PackageManager.PERMISSION_GRANTED) {
  122.  
  123.                 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
  124.  
  125.             } else {
  126.  
  127.                 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 25, locationListener);
  128.  
  129.             }
  130.         }
  131.         else {
  132.             locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 25, locationListener);
  133.         }
  134.  
  135.         // Add a marker in Sydney and move the camera
  136.         userLocation = new LatLng(40.369611, 49.822672);
  137.         mMap.addMarker(userMarker.position(userLocation).snippet("Lat: 40.369611, Long: 49.822672"));
  138.         mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
  139.         mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
  140.  
  141.  
  142.     }
  143.  
  144.  
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement