Created
March 31, 2019 21:07
-
-
Save MohammadSamandari/fe275547d952542635f57e90e2189421 to your computer and use it in GitHub Desktop.
Learning : Maps and Getting Location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { | |
private GoogleMap mMap; | |
LocationManager locationManager; | |
LocationListener locationListener; | |
LatLng newLocation; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_maps); | |
// Obtain the SupportMapFragment and get notified when the map is ready to be used. | |
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() | |
.findFragmentById(R.id.map); | |
mapFragment.getMapAsync(this); | |
// Defining The LocationManager and LocationListener. | |
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); | |
locationListener = new LocationListener() { | |
@Override | |
public void onLocationChanged(Location location) { | |
// Removing Previous Markers Before adding the new one. | |
mMap.clear(); | |
// When Location Changes, a new marker is made on the location and the Camera Moves There. | |
newLocation = new LatLng(location.getLatitude(), location.getLongitude()); | |
mMap.addMarker(new MarkerOptions().position(newLocation).title("You are Here")); | |
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(newLocation, 14)); | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
} | |
}; | |
} | |
@Override | |
public void onMapReady(GoogleMap googleMap) { | |
mMap = googleMap; | |
// Checking For Permission, if we have it, then the request for location is sent, Else if we don't have permission, request for permission is made. | |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); | |
return; | |
} | |
Log.i("Lord", "onMapReady Happens"); | |
// Requesting Location. | |
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); | |
// Requesting Users Last Known Location. and Move Camera to that location. | |
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
mMap.addMarker(new MarkerOptions().position(new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude())).title("You are Here")); | |
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()), 14)); | |
} | |
// this happens After request permission dialog is shown and user has allowed or denied access. | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
// Checking to see if user has granted the permission or not, | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { | |
Log.i("Lord", "onRequestPermissionsResult Happens"); | |
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment