Mobile Application Development
Practical-6
#code-
[Link]
activity_main.xml
fragment_first.xml
#output-
Practical-10
#code-
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap mMap;
LocationManager locationManager;
private static final int REQUEST_LOCATION_PERMISSION = 1;
Marker marker;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById([Link]);
[Link](this);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if ([Link](this,
[Link].ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
[Link](this, new String[]
{[Link].ACCESS_FINE_LOCATION},
REQUEST_LOCATION_PERMISSION);
}
else{
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double latitude = [Link]();
double longitude = [Link]();
//get the location name from latitude and longitude
Geocoder geocoder = new Geocoder(getApplicationContext());
try {
List<Address> addresses =
[Link](latitude, longitude, 1);
String result = [Link](0).getLocality()+":";
result += [Link](0).getCountryName();
LatLng latLng = new LatLng(latitude, longitude);
if (marker != null){
[Link]();
marker = [Link](new MarkerOptions().position(latLng).title(result));
[Link](20);
[Link]([Link](latLng, 12.0f));
}
else{
marker = [Link](new MarkerOptions().position(latLng).title(result));
[Link](20);
[Link]([Link](latLng, 21.0f));
}
} catch (IOException e) {
[Link]();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
@Override
public void onProviderDisabled(String provider) {
}
};
[Link](LocationManager.NETWORK_PROVIDER, 0, 0,
locationListener);
[Link](LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
grantResults) {
if ([Link](this,
[Link].ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED){
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double latitude = [Link]();
double longitude = [Link]();
//get the location name from latitude and longitude
Geocoder geocoder = new Geocoder(getApplicationContext());
try {
List<Address> addresses =
[Link](latitude, longitude, 1);
String result = [Link](0).getLocality()+":";
result += [Link](0).getCountryName();
LatLng latLng = new LatLng(latitude, longitude);
if (marker != null){
[Link]();
marker = [Link](new MarkerOptions().position(latLng).title(result));
[Link](20);
[Link]([Link](latLng, 12.0f));
}
else{
marker = [Link](new MarkerOptions().position(latLng).title(result));
[Link](20);
[Link]([Link](latLng, 21.0f));
}
} catch (IOException e) {
[Link]();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
@Override
public void onProviderDisabled(String provider) {
}
};
[Link](LocationManager.NETWORK_PROVIDER, 0, 0,
locationListener);
[Link](LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
@Override
protected void onStop() {
[Link]();
[Link](locationListener);
}
}
#Output-