You are on page 1of 7

Practical No 31

Q1) Write a program to locate user’s current location

First we follow some steps to accessing “Google Map”

1. First Create the activity_maps. xml file

2. Copy URL from google_map_api.xml file to generate Google map key.


3. Copied URL in the browser . It will open the next page

4. Click on Create API key to generate API key.


5. Then After clicking on Create API key, It is generate our API key.

6. Copy this generated API key in our google_map_api.xml file


Activity_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context= ".MainActivity" />
build.gradle.kts ( Dependencies)
dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation ("com.google.android.gms:play-services-maps:18.2.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}

Java file

package com.example.practical31;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
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 {
private GoogleMap gm;

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

if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATI
ON) != PackageManager.PERMISSION_GRANTED &&

ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCA
TION) != PackageManager.PERMISSION_GRANTED &&

ContextCompat.checkSelfPermission(this,Manifest.permission.INTERNET) !=
PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new
String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_
COARSE_LOCATION,Manifest.permission.INTERNET},100);
}
else
{
SupportMapFragment support = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
support.getMapAsync(new OnMapReadyCallback()
{
@Override
public void onMapReady(@NonNull GoogleMap googleMap)
{
LatLng current = new LatLng(-12.949860, -38.384602);
gm.addMarker(new MarkerOptions().position(current).title("Gadhinglaj"));
gm.moveCamera(CameraUpdateFactory.newLatLng(current));
}
});
}
}
}

AndroidManifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<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"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Practical31"
tools:targetApi="31">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/api_key" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
String.xml
<resources>
<string name="app_name">practical31</string>
<string name="api_key">API KEY HERE</string>
</resource

Output :-
Output :-

You might also like