0% found this document useful (0 votes)
6 views3 pages

Google Current Location

The document contains an Android application manifest that requests location permissions and includes a Google Maps API key. It also features an XML layout file for displaying a map and a Java class that manages map functionality, including location permissions and displaying the user's current location. The app utilizes the Fused Location Provider to retrieve and show the user's location on the map.

Uploaded by

death0000005
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)
6 views3 pages

Google Current Location

The document contains an Android application manifest that requests location permissions and includes a Google Maps API key. It also features an XML layout file for displaying a map and a Java class that manages map functionality, including location permissions and displaying the user's current location. The app utilizes the Fused Location Provider to retrieve and show the user's location on the map.

Uploaded by

death0000005
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/ 3

Manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mymapapp">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application>
<!-- Activity and Google Maps API key -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_GOOGLE_MAPS_API_KEY" />
</application>
</manifest>

Xml file

<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=".MainActivity">

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

</RelativeLayout>

Java

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;


private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationClient;

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

// Initialize the SupportMapFragment and set the callback


SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}

// Initialize the Fused Location Provider


mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}

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

// Check for location permission


if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
} else {
showCurrentLocation();
}
}

private void showCurrentLocation() {


// Check again if permission is granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);

// Get last known location


mFusedLocationClient.getLastLocation().addOnSuccessListener(location -> {
if (location != null) {
// Move the map's camera to the current location
LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(currentLocation).title("You are
here"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15));
} else {
Toast.makeText(this, "Unable to retrieve location", Toast.LENGTH_SHORT).show();
}
});
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
showCurrentLocation();
} else {
Toast.makeText(this, "Location permission is required to use this app",
Toast.LENGTH_SHORT).show();
}
}
}
}

You might also like