Native Mobile App Development With Java

Point my Current location Android

29-Sep-2024

Point my Current location Android

Step 1 :

Add  Map ,  Location  library

File open app/Gradle Scripts/build.gradle file −


dependencies {
implementation libs.appcompat
implementation libs.material
implementation libs.activity
implementation libs.constraintlayout
testImplementation libs.junit
androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core

implementation 'com.google.android.gms:play-services-location:21.3.0' //add library
implementation 'com.google.android.gms:play-services-maps:19.0.0' //add library
}


Step 2 :

Add permission in Manifests app/manifests/AndroidManifests.xml 



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


Step 3 :

Add meta-data

Add permission in Manifests app/manifests/AndroidManifests.xml 


<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}" />


 To add API-KEY follow below steps:

1 :-  go to Console cloud and log in by you email.



2 :-  Create a project 




3 :-  Give a project name And Create




4 :-  Goto your project and Open and Enable API and Services 

left side  APIs and services>Credentials




4 :- Create Credentials 





5 :- Create API Key





5 :- Get the  Key




Step 4 :

File open res/layout/activity_main.xml file −


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
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>


Step 5 :

File open app/java/MainActivity.java file −


package com.example.currentlocation;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

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;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

private final int FINE_PERMISSION_CODE = 1;
private GoogleMap mygoogleMap;
Location currentLocation;
FusedLocationProviderClient fusedLocationProviderClient;

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

fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
getLastLocation();

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

private void getLastLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, FINE_PERMISSION_CODE);
return;
}
Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
currentLocation = location;
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(MainActivity.this);
}
}
});
}


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

if (currentLocation != null) {
LatLng myLocation = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
mygoogleMap.addMarker(new MarkerOptions().position(myLocation).title("My Location"));
mygoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 15));
} else {
Toast.makeText(MainActivity.this, "Location not available", Toast.LENGTH_SHORT).show();
}
}

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

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == FINE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLastLocation();
} else {
Toast.makeText(MainActivity.this, "Location permission is denied", Toast.LENGTH_SHORT).show();
}
}
}
}




Comments