Practical 31
Practical 31
Q1)
Copy the URL from google_map_api.xml file to generate Google map key.
Paste the copied URL at the browser. It will open the following page.
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;
ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATIO
N) != 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_LOC
ATION,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
String.xml
<resources>
<string name="app_name">practical31</string>
<string name="api_key">API KEY HERE</string>
</resources>
OUTPUT:-