Name of Student: Shubham Sanjay Chougule
Name of Student: Shubham Sanjay Chougule
Name of Student: Shubham Sanjay Chougule
Title of LAB Assignment : Android program to work with google maps and location and
GPS
A) Use Google maps API to create an application and use "Add marker "method
B) Display the current location of your device (Latitude & Longitude)
PRACTICA 7
Aim: Android program to work with google maps and location "Add marker "method to
be used in the application students are creating.
Theory:
Google Maps API: An API key is needed to access the Google Maps servers. This key is
free, and you can use it with any of your applications. If you haven’t created project, you
can follow the below steps to get started:
Step 1: Open Google developer console and sign in with your gmail
account: https://console.developers.google.com/project
Step 2: Now create a new project. You can create a new project by clicking on the Create
Project button and giving the name to your project.
Step 3: Now click on APIs & Services and open Dashboard from it
Name: Shubham Chougule SYMCA/A Roll no.8
Step 9: Now your API key will be generated. Copy it and save it somewhere as we will
need it when implementing Google Map in our Android project.
Strings.xml:
<resources>
<string name="app_name">My Application_prac7</string>
<string name="map_key"
translatable="false">AIzaSyBkQ7SsgYqle37tPs0BYLpXddFu6m0uCuk</string>
</resources>
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">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/map_key"/>
Dependencies:
implementation 'com.google.android.libraries.maps:maps:3.1.0-
beta' implementation 'com.google.android.gms:play-services-
maps:18.0.0' implementation 'com.google.android.gms:play-
services-location:18.0.0' implementation
'com.google.maps.android:android-maps-utils:2.3.0'
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/and
roid"
xmlns:app="http://schemas.android.com/apk/res-
auto"
Name: Shubham Chougule SYMCA/A Roll no.8
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
Name: Shubham Chougule SYMCA/A Roll no.8
<fragment
android:id="@+id/google_m
ap"
android:name="com.google.android.gms.maps.SupportMapFrag
ment" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingClass" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.myapplication_prac7;
import
androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.annotation.SuppressLint;
import
android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import
android.widget.Toast;
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;
import androidx.annotation.NonNull;
import
androidx.appcompat.app.AppCompatActivity;
Name: Shubham Chougule SYMCA/A Roll no.8
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
@Override
Name: Shubham Chougule SYMCA/A Roll no.8
Task<Location> task =
fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>()
{
@Override
public void onSuccess(Location
location) { if(location != null){
currentLocation = location;
Toast.makeText(getApplicationContext(),currentLocation.getLatitude()
+""+currentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
SupportMapFragment supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.google_
map);
supportMapFragment.getMapAsync(MainActivity.th
is);
}
}
});
}
@Override
public void onMapReady(@NonNull GoogleMap
googleMap) { double srcLat =
Name: Shubham Chougule SYMCA/A Roll no.8
currentLocation.getLatitude();
double srcLong =
currentLocation.getLongitude(); LatLng
latLng = new
LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
MarkerOptions markerOptions = new MarkerOptions().position(latLng).title(srcLat + ":" +
Name: Shubham Chougule SYMCA/A Roll no.8
srcLong);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,10));
googleMap.addMarker(markerOptions);
gMap = googleMap;
gMap.setOnMapClickListener(new
GoogleMap.OnMapClickListener() { int count = -1;
@Override
public void onMapClick(@NonNull LatLng latLng) {
MarkerOptions markerOptions1 = new MarkerOptions();
markerOptions1.position(latLng);
markerOptions1.title(latLng.latitude+ ":" +
latLng.longitude); count++;
if(count % 2 !=
0){
gMap.clear();
}
gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,10));
gMap.addMarker(markerOptions1);
double res =
distance(srcLat,latLng.latitude,srcLong,latLng.longitude);
Toast.makeText(getApplicationContext(), "Distance = "+ res,
Toast.LENGTH_SHORT).show();
}
});
}
public static double distance(double lat1, double lat2,
double lon1,double lon2)
{
lon1 =
Math.toRadians(lon1);
lon2 =
Math.toRadians(lon2); lat1
= Math.toRadians(lat1);
lat2 =
Math.toRadians(lat2);
double dlon = lon2 - lon1;
double dlat = lat2 - lat1;
double a = Math.pow(Math.sin(dlat / 2), 2) + Math.cos(lat1) * Math.cos(lat2) *
Math.pow(Math.sin(dlon / 2),2);
Name: Shubham Chougule SYMCA/A Roll no.8
double c = 2 *
Math.asin(Math.sqrt(a)); double r =
6371;
return(c * r);
Name: Shubham Chougule SYMCA/A Roll no.8
}
@SuppressLint("MissingSuperCa
ll") @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case
REQUEST_CODE:
if (grantResults.length > 0 &&
grantResults[0] ==
PackageManager.PERMISSION_GRANTED);
fetchlastLocation(
); break;
}
}
}
Output: