Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.stayfound;
- import android.app.Activity;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.location.Criteria;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Build;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.app.FragmentActivity;
- import android.os.Bundle;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.widget.Toast;
- import com.google.android.gms.maps.CameraUpdate;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.MapFragment;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.MarkerOptions;
- public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
- GoogleMap mGoogleMap;
- SupportMapFragment mapFrag;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_maps);
- if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- checkLocationPermission();
- }
- mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
- mapFrag.getMapAsync(this);
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mGoogleMap = googleMap;
- mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
- //mGoogleMap.setTrafficEnabled(true);
- mGoogleMap.setIndoorEnabled(true);
- mGoogleMap.setBuildingsEnabled(true);
- mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
- mGoogleMap.getCameraPosition();
- // Get LocationManager object from System Service LOCATION_SERVICE
- LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
- // Create a criteria object to retrieve provider
- Criteria criteria = new Criteria();
- // Get the name of the best provider
- String provider = locationManager.getBestProvider(criteria, true);
- // Get Current Location
- Location myLocation = locationManager.getLastKnownLocation(provider);
- //set map type
- googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
- // Get latitude of the current location
- double latitude = myLocation.getLatitude();
- // Get longitude of the current location
- double longitude = myLocation.getLongitude();
- // Create a LatLng object for the current location
- LatLng latLng = new LatLng(latitude, longitude);
- // Show the current location in Google Map
- googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
- // Zoom in the Google Map
- googleMap.animateCamera(CameraUpdateFactory.zoomTo(8));
- googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));
- if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- //User has previously accepted this permission
- if (ActivityCompat.checkSelfPermission(this,
- android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
- mGoogleMap.setMyLocationEnabled(true);
- }
- } else {
- //Not in api-23, no need to prompt
- mGoogleMap.setMyLocationEnabled(true);
- }
- }
- public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
- public boolean checkLocationPermission() {
- if (ContextCompat.checkSelfPermission(this,
- android.Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED) {
- // Should we show an explanation?
- if (ActivityCompat.shouldShowRequestPermissionRationale(this,
- android.Manifest.permission.ACCESS_FINE_LOCATION)) {
- // Show an expanation to the user *asynchronously* -- don't block
- // this thread waiting for the user's response! After the user
- // sees the explanation, try again to request the permission.
- // TODO: Prompt with explanation!
- //Prompt the user once explanation has been shown
- ActivityCompat.requestPermissions(this,
- new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
- MY_PERMISSIONS_REQUEST_LOCATION);
- } else {
- // No explanation needed, we can request the permission.
- ActivityCompat.requestPermissions(this,
- new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
- MY_PERMISSIONS_REQUEST_LOCATION);
- }
- return false;
- } else {
- return true;
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode,
- String permissions[], int[] grantResults) {
- switch (requestCode) {
- case MY_PERMISSIONS_REQUEST_LOCATION: {
- // If request is cancelled, the result arrays are empty.
- if (grantResults.length > 0
- && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // permission was granted, yay!
- if (ActivityCompat.checkSelfPermission(this,
- android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
- mGoogleMap.setMyLocationEnabled(true);
- }
- } else {
- // permission denied, boo! Disable the
- // functionality that depends on this permission.
- Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
- }
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement