0% found this document useful (0 votes)
14 views3 pages

Par 32

The document provides a program for drawing a route between two locations using Google Maps in an Android application. It includes an XML layout file and a Java class that initializes the map, fetches the user's current location, and adds markers for New York and Los Angeles while drawing a polyline between them. The program also handles location permissions and updates the map accordingly.

Uploaded by

shekharvatmode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Par 32

The document provides a program for drawing a route between two locations using Google Maps in an Android application. It includes an XML layout file and a Java class that initializes the map, fetches the user's current location, and adds markers for New York and Los Angeles while drawing a polyline between them. The program also handles location permissions and updates the map accordingly.

Uploaded by

shekharvatmode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Q.1 Write a program to draw a route between two locations.

Program
activity_main.xml MainActivity.java
<?xml version="1.0" encoding="utf-8"?> package com.example.exp32_1;
<fragment import androidx.core.app.ActivityCompat;
xmlns:android="http://schemas.android.com/apk/res/and import androidx.fragment.app.FragmentActivity;
roid" import android.Manifest;
xmlns:map="http://schemas.android.com/apk/res- import android.content.pm.PackageManager;
auto" import android.graphics.Color;
xmlns:tools="http://schemas.android.com/tools" import android.location.Location;
android:id="@+id/map" import android.os.Bundle;
import com.google.android.gms.location.*;
android:name="com.google.android.gms.maps.Support import com.google.android.gms.maps.*;
MapFragment" import com.google.android.gms.maps.model.*;
android:layout_width="match_parent" import com.google.android.gms.tasks.*;
android:layout_height="match_parent"
tools:context=".MapsActivity" /> public class MapsActivity extends FragmentActivity
implements OnMapReadyCallback {
private GoogleMap mMap;
FusedLocationProviderClient locationClient;
Location currentLocation;

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

locationClient =
LocationServices.getFusedLocationProviderClient(th
is);
fetchLocation();

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

void fetchLocation() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATIO
N) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCA
TION}, 100);
return;
}

Task<Location> task =
locationClient.getLastLocation();
task.addOnSuccessListener(new
OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
currentLocation = location;

// Update map if it's ready


if (mMap != null) {
LatLng vvp = new
LatLng(currentLocation.getLatitude(),
currentLocation.getLongitude());
mMap.addMarker(new
MarkerOptions().position(vvp).title("I am Here"));

mMap.moveCamera(CameraUpdateFactory.newLatL
ngZoom(vvp, 15));
}
}
}
});
}

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

LatLng newYork = new LatLng(40.7128, -


74.0060);
LatLng losAngeles = new LatLng(34.0522, -
118.2437);

// Add markers
mMap.addMarker(new
MarkerOptions().position(newYork).title("New
York"));
mMap.addMarker(new
MarkerOptions().position(losAngeles).title("Los
Angeles"));

// Move camera to show both

mMap.moveCamera(CameraUpdateFactory.newLatL
ngZoom(new LatLng(37.0, -96.0), 4));

// Draw straight line route (polyline)


mMap.addPolyline(new PolylineOptions()
.add(newYork, losAngeles)
.width(5)
.color(Color.BLUE));
}
}
Output:-
Exp32_1

You might also like