EX 32
EX 32
Exercise
1. Write a program to draw a route between two locations.
activity_main.xml import
com.google.android.gms.maps.OnMapReadyCallb
ack;
<?xml version="1.0" encoding="utf-8"?>
import
<fragment com.google.android.gms.maps.SupportMapFragm
xmlns:android="http://schemas.android.com/apk/r ent;
es/android"
import
com.google.android.gms.maps.model.BitmapDesc
xmlns:map="http://schemas.android.com/apk/res- riptorFactory;
auto"
import
xmlns:tools="http://schemas.android.com/tools" com.google.android.gms.maps.model.LatLng;
android:id="@+id/map" import
com.google.android.gms.maps.model.MarkerOpti
ons;
android:name="com.google.android.gms.maps.Su
pportMapFragment" import
com.example.ex3201.databinding.ActivityMapsBi
android:layout_width="match_parent" nding;
android:layout_height="match_parent" import
tools:context=".MapsActivity" /> com.google.android.gms.maps.model.PolylineOpt
ions;
import org.json.JSONObject;
MainActivity.java
import java.io.BufferedReader;
package com.example.ex3201;
import java.io.IOException;
import java.io.InputStream;
import androidx.fragment.app.FragmentActivity;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import android.graphics.Color;
import java.net.URL;
import android.os.AsyncTask;
import java.util.ArrayList;
import android.os.Bundle;
import java.util.HashMap;
import android.util.Log;
import java.util.List;
import
com.google.android.gms.maps.CameraUpdateFact
ory; public class MapsActivity extends
import FragmentActivity implements
com.google.android.gms.maps.GoogleMap; OnMapReadyCallback {
if (markerPoints.size() == 1) {
@Override
options.icon(BitmapDescriptorFactory.defaultMar
public void onMapReady(GoogleMap
ker(BitmapDescriptorFactory.HUE_GREEN));
googleMap) {
} else if (markerPoints.size() == 2) {
mMap = googleMap;
options.icon(BitmapDescriptorFactory.defaultMar
// Add a marker in Sydney and move the ker(BitmapDescriptorFactory.HUE_RED));
camera
}
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new
// Add new marker to the Google Map
MarkerOptions().position(sydney).title("Marker in
Android API V2
Sydney"));
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newL
atLngZoom(sydney,16));
// Checks, whether start and end locations
are captured
mMap.setOnMapClickListener(new if (markerPoints.size() >= 2) {
GoogleMap.OnMapClickListener() {
LatLng origin = (LatLng)
@Override markerPoints.get(0);
LatLng dest = (LatLng) }
markerPoints.get(1);
parserTask.execute(result);
// Start downloading json data from
Google Directions API
downloadTask.execute(url); }
}
@Override
// Origin of route
for (int i = 0; i < result.size(); i++) {
String str_origin = "origin=" + origin.latitude
points = new ArrayList(); + "," + origin.longitude;
lineOptions = new PolylineOptions();
// Destination of route
List<HashMap> path = result.get(i); String str_dest = "destination=" + dest.latitude
+ "," + dest.longitude;
// Output format
points.add(position);
String output = "json";
}
data = sb.toString();
// Building the url to the web service
String url = br.close();
"https://maps.googleapis.com/maps/api/directions/
" + output + "?" + parameters;
} catch (Exception e) {
Log.d("Exception", e.toString());
} finally {
return url;
iStream.close();
}
urlConnection.disconnect();
}
private String downloadUrl(String strUrl)
throws IOException { return data;
String data = ""; }
InputStream iStream = null; }
HttpURLConnection urlConnection = null;
try { AndroidManifest.xml
URL url = new URL(strUrl);
<application
iStream = urlConnection.getInputStream();
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
BufferedReader br = new
BufferedReader(new android:label="@string/app_name"
InputStreamReader(iStream));
android:roundIcon="@mipmap/ic_launcher_roun
d"
StringBuffer sb = new StringBuffer();
android:supportsRtl="true"
android:theme="@style/Theme.EX3201">
String line = "";
while ((line = br.readLine()) != null) {
<meta-data
sb.append(line);
}
android:name="com.google.android.geo.API_KE
Y"
android:value="EMPTY" />
<activity
android:name=".MapsActivity"
android:exported="true"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>