Practical No 31 - 32
Practical No 31 - 32
specific location and retrieving the user's current location using the
LocationManager and Geocoder.”
MapsActivity.java
@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);
locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
geocoder = new Geocoder(this, Locale.getDefault());
@Override
public void onProviderEnabled(@NonNull String provider) {
}
@Override
public void onProviderDisabled(@NonNull String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
@Override
protected void onResume() {
super.onResume();
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATI
ON) == PackageManager.PERMISSION_GRANTED) {
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
} else {
ActivityCompat.requestPermissions(this,new
String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
}
}
@Override
protected void onPause() {
super.onPause();
locMgr.removeUpdates(locationListener);
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MapsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
google_maps_api.xml
<resources>
<String name= "google_maps_key" templateMergeStrategy= "preserve" translatable= "false"
>YOUR_API_KEY_HERE</string>
</resources>
activity_maps.xml
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
activity_maps.xml:
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
MapsActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
MapsActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
geocoder = new Geocoder(this, Locale.getDefault());
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, locationListener);
}
@Override
protected void onDestroy() {
super.onDestroy();
// Remove location updates when activity is destroyed
locationManager.removeUpdates(locationListener);
}
}