0% found this document useful (0 votes)
27 views9 pages

Practical No 31 - 32

The document describes an Android application that displays a map with a marker at a specific location and retrieves the user's current location using the LocationManager and Geocoder APIs. It includes code snippets for the MapsActivity class, AndroidManifest file, google_maps_api file, and activity_maps layout file to implement these features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views9 pages

Practical No 31 - 32

The document describes an Android application that displays a map with a marker at a specific location and retrieves the user's current location using the LocationManager and Geocoder APIs. It includes code snippets for the MapsActivity class, AndroidManifest file, google_maps_api file, and activity_maps layout file to implement these features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

“Android application that involves displaying a map with a marker at a

specific location and retrieving the user's current location using the
LocationManager and Geocoder.”

Note : Separate Codes Are Also Given Below

MapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {


private GoogleMap mMap;
private static final int REQUEST_LOCATION = 101;
LocationManager locMgr = null;
LocationListener locationListener = null;
Geocoder geocoder;

@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());

locationListener = new LocationListener() {


@Override
public void onLocationChanged(@NonNull Location location) {
if (location != null) {
try {
List<Address>addressList = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
if (addressList.size() > 0) {
Toast.makeText(MapsActivity.this, "Your Current Location Is: " +
addressList.get(0).getAddressLine(0), Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

@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

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapsdemo">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
/>

<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" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

google_maps_api.xml

<resources>
<String name= "google_maps_key" templateMergeStrategy= "preserve" translatable= "false"
>YOUR_API_KEY_HERE</string>
</resources>
activity_maps.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

Displaying a Map with a Marker:

activity_maps.xml:

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">

<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

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {


private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);

SupportMapFragment mapFragmen= (SupportMapFragment) getSupportFragmentManager()


.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in a specific location and move the camera


LatLng markerLocation = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(markerLocation).title("Marker in
Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(markerLocation));
}
}

User's Current Location

MapsActivity.java

public class MapsActivity extends AppCompatActivity {

private LocationManager locationManager;


private LocationListener locationListener;
private Geocoder geocoder;

@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());

locationListener = new LocationListener() {


@Override
public void onLocationChanged(Location location) {
if (location != null) {
try {
List<Address> addressList =
geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (!addressList.isEmpty()) {
String address = addressList.get(0).getAddressLine(0);
Toast.makeText(MapsActivity.this, "Current Location: " +
address, Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

// Other overridden methods of LocationListener


};

// Request location updates

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0, locationListener);
}

@Override
protected void onDestroy() {
super.onDestroy();
// Remove location updates when activity is destroyed
locationManager.removeUpdates(locationListener);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy