Exp31: Write a program to locate user’s current location.
activity_main.xml:
<RelativeLayout
xmlns:android="[Link]
xmlns:map="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/map"
android:name="[Link]"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ZoomControls
android:id="@+id/zoom_controls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="~Developed By [Link] (Roll no:06)"
android:textColor="#0000FF"
android:textSize="16dp"
android:layout_marginBottom="35dp"
android:layout_marginEnd="35dp" />
</RelativeLayout>
[Link]:
package [Link].map1;
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];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends FragmentActivity implements
OnMapReadyCallback,
LocationListener, [Link],
[Link] {
ZoomControls zoomControls;
private GoogleMap mMap;
Location mLastLocation;
Marker mCurrLocationMarker;
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
private static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Obtain the SupportMapFragment and get notified when the map is
ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById([Link]);
[Link](this);
zoomControls = findViewById([Link].zoom_controls);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link]());
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link]());
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Check for runtime permissions
if ([Link].SDK_INT >= Build.VERSION_CODES.M) {
if ([Link](this,
[Link].ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
[Link](true);
} else {
// Request location permission
[Link](this,
new
String[]{[Link].ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
} else {
buildGoogleApiClient();
[Link](true);
}}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new [Link](this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi([Link]).build();
[Link]();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = new LocationRequest();
[Link](1000);
[Link](1000);
[Link](LocationRequest.PRIORITY_BALANCED_POWER_ACCURA
CY);
if ([Link](this,
[Link].ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
[Link](mGoogleApiClient,
mLocationRequest, this);
}}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null) {
[Link]();
}
//Place current location marker
LatLng latLng = new LatLng([Link](),
[Link]());
MarkerOptions markerOptions = new MarkerOptions();
[Link](latLng);
[Link]("Current Position");
[Link]([Link](BitmapDescriptorFa
ctory.HUE_GREEN));
mCurrLocationMarker = [Link](markerOptions);
[Link]([Link](latLng));
[Link]([Link](11));
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[] permissions, @NonNull int[] grantResults) {
[Link](requestCode, permissions,
grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if ([Link] > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
// permission was granted
if ([Link](this,
[Link].ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
if (mGoogleApiClient == null) {
buildGoogleApiClient();}
[Link](true);
}
} else {
// permission denied
// You can show a message or request the permission
again.}
return;
}
}}
}
add below in manifest file:
<meta-data
android:name="[Link].API_KEY"
android:value="@string/google_maps_key" />
//add api key in [Link]
<uses-permission android:name="[Link]" />
<uses-permission android:name="[Link].ACCESS_FINE_LOCATION" />
<uses-permission android:name="[Link].ACCESS_COARSE_LOCATION"
/>
Output:-