X.
Exercise
1. Write a program to locate user’s current location.
activity_main.xml import
com.google.android.gms.maps.model.MarkerOpti
ons;
<?xml version="1.0" encoding="utf-8"?>
public class MapsActivity extends
<fragment FragmentActivity implements
xmlns:android="http://schemas.android.com/apk/r OnMapReadyCallback {
es/android"
private GoogleMap mMap;
xmlns:map="http://schemas.android.com/apk/res-
@Override
auto"
protected void onCreate(Bundle
xmlns:tools="http://schemas.android.com/tools"
savedInstanceState) {
android:id="@+id/map"
super.onCreate(savedInstanceState);
android:name="com.google.android.gms.maps.Su
setContentView(R.layout.activity_maps);
pportMapFragment"
// Obtain the SupportMapFragment and get notified
android:layout_width="match_parent"
when the map is ready to be used.
android:layout_height="match_parent"
SupportMapFragment mapFragment =
tools:context=".MapsActivity" /> (SupportMapFragment)
getSupportFragmentManager()
MainActivity.java .findFragmentById(R.id.map);
package com.example.map; mapFragment.getMapAsync(this);
import androidx.fragment.app.FragmentActivity; }
import android.os.Bundle; @Override
import public void onMapReady(GoogleMap googleMap)
com.google.android.gms.maps.CameraUpdateFact {
ory;
mMap = googleMap;
import
// Add a marker in Sydney and move the camera
com.google.android.gms.maps.GoogleMap;
LatLng sydney = new LatLng(17.6806, 75.3155);
import
com.google.android.gms.maps.OnMapReadyCallb mMap.addMarker(new
ack; MarkerOptions().position(sydney).title("Marker in
Sydney"));
import
com.google.android.gms.maps.SupportMapFragm mMap.moveCamera(CameraUpdateFactory.newL
ent; atLng(sydney));
import
com.google.android.gms.maps.model.LatLng;
}
}
resources/string.xml android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_roun
<?xml version="1.0" encoding="utf-8"?> d"
<resources> android:supportsRtl="true"
<string name="map_kay" translatable="false" /> android:theme="@style/AppTheme" >
AIzaSyALMp5hkiAhmlFWfJ cE6NFjhyF5LH4 <meta-data
Adg
android:name="com.google.android.geo.API_KE
</resources>
Y"
android:value="@string/google_maps_key" />
AndroidManifest.xml
<activity
android:name=".MapsActivity"
<?xml version="1.0" encoding="utf-8"?>
android:label="@string/title_activity_maps">
<manifest
<intent-filter>
xmlns:android="http://schemas.android.com/apk/r
<action
es/android"
android:name="android.intent.action.MAIN" />
package="com.example.map">
<category
<uses-permission
android:name="android.intent.category.LAUNCH
android:name="android.permission.ACCESS_FIN
ER" />
E_LOCATION" />
</intent-filter>
<application
</activity>
android:allowBackup="true"
</application>
android:icon="@mipmap/ic_launcher"
</manifest>