Name: Shreyas Liladhar Lokhande Roll No: 41
Class: TYCM Batch: B
Subject: MAD
Practical No & Name: 31. Deploy map-based application. Part I
XML Code: Output:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mapFragment"
android:name="com.google.android.gms.maps
.SupportMapFragment"
/>
</RelativeLayout>
Java Code :
package com.example.pr31;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
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.*;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
public class MainActivity extends AppCompatActivity implements
OnMapReadyCallback {
private GoogleMap mMap;
private FusedLocationProviderClient fusedLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fusedLocationClient =
LocationServices.getFusedLocationProviderClient(this);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.mapFragment);
if (mapFragment != null) mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap=googleMap;
showLocation();
}
@SuppressLint("MissingPermission")
private void showLocation() {
fusedLocationClient.getLastLocation().addOnSuccessListener(this,
location -> {
if (location != null && mMap != null) {
LatLng userLocation = new LatLng(location.getLatitude(),
location.getLongitude());
mMap.addMarker(new
MarkerOptions().position(userLocation).title("You are here"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,
15));
}
});
}
}