Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. package dk.beringtom.birdwatcher;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.location.Location;
  6. import android.location.LocationListener;
  7. import android.location.LocationManager;
  8. import android.os.Bundle;
  9. import android.support.v4.app.ActivityCompat;
  10. import android.support.v4.app.FragmentActivity;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.LinearLayout;
  16. import android.widget.Toast;
  17.  
  18. import com.google.android.gms.maps.CameraUpdateFactory;
  19. import com.google.android.gms.maps.GoogleMap;
  20. import com.google.android.gms.maps.OnMapReadyCallback;
  21. import com.google.android.gms.maps.SupportMapFragment;
  22. import com.google.android.gms.maps.model.LatLng;
  23. import com.google.android.gms.maps.model.MarkerOptions;
  24.  
  25. public class BirdMap extends FragmentActivity implements OnMapReadyCallback {
  26.  
  27.     private LocationManager locationManager;
  28.     private LocationListener locationListener;
  29.     private static final int minimumTime = 0;
  30.     private static final int minimumDistance = 0;
  31.  
  32.     private GoogleMap mMap;
  33.  
  34.     @Override
  35.     protected void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.bird_map);
  38.  
  39.         locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
  40.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  41.             Toast.makeText(this, "Missing Permissions\nfor Map!", Toast.LENGTH_LONG).show();
  42.             return;
  43.         }
  44.         Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  45.         getMyLocation(lastKnownLocation);
  46.  
  47.         locationListener = new LocationListener() {
  48.             public void onLocationChanged(Location location) {
  49.                 getMyLocation(location);
  50.             }
  51.  
  52.             public void onStatusChanged(String provider, int status, Bundle extras) {
  53.             }
  54.  
  55.             public void onProviderEnabled(String provider) {
  56.             }
  57.  
  58.             public void onProviderDisabled(String provider) {
  59.             }
  60.         };
  61.  
  62.         try {
  63.             locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minimumTime, minimumDistance, locationListener);
  64.         } catch (SecurityException ex) {
  65.             Log.e("SHIT", ex.toString());
  66.         }
  67.  
  68.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  69.                 .findFragmentById(R.id.mapView);
  70.         mapFragment.getMapAsync(this);
  71.     }
  72.  
  73.     public void getMyLocation(Location location) {
  74.         double myLat = location.getLatitude();
  75.         double myLng = location.getLongitude();
  76.     }
  77.  
  78.     @Override
  79.     protected void onPause() {
  80.         super.onPause();
  81.         try {
  82.             if (locationManager != null && locationListener != null)
  83.                 locationManager.removeUpdates(locationListener);
  84.         } catch (SecurityException ex) {
  85.             Log.e("SHIT", ex.toString());
  86.         }
  87.     }
  88.  
  89.     @Override
  90.     protected void onResume() {
  91.         super.onResume();
  92.         try {
  93.             if (locationManager != null && locationListener!=null)
  94.                 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minimumTime, minimumDistance, locationListener);
  95.         } catch (SecurityException ex) {
  96.             Log.e("SHIT", ex.toString());
  97.         }
  98.     }
  99.  
  100.     @Override
  101.     public boolean onCreateOptionsMenu(Menu menu)
  102.     {
  103.         getMenuInflater().inflate(R.menu.menu_main, menu);
  104.         return super.onCreateOptionsMenu(menu);
  105.     }
  106.  
  107.     @Override
  108.     public boolean onOptionsItemSelected(MenuItem item)
  109.     {
  110.         switch (item.getItemId())
  111.         {
  112.             case R.id.menuMain_Logout_Button:
  113.                 Toast.makeText(this, "Logout Button\nClicked", Toast.LENGTH_LONG).show();
  114.                 return true;
  115.             default:
  116.                 return super.onOptionsItemSelected(item);
  117.         }
  118.     }
  119.  
  120.  
  121.     @Override
  122.     public void onMapReady(GoogleMap googleMap) {
  123.  
  124.         mMap = googleMap;
  125.  
  126.         // Add a marker in Sydney and move the camera
  127.         LatLng sydney = new LatLng(-34, 151);
  128.         mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
  129.         mMap.moveCamera(CameraUpdateFactory.newLatLng(myLoc));
  130.     }
  131.  
  132.     public void MapBackClicked(View view)
  133.     {
  134.         finish();
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement