0% found this document useful (0 votes)
9 views4 pages

Practical No. 32

The document provides a code example for an Android application that uses Google Maps to draw a route between two locations, Kolhapur and Mumbai. It includes the layout in XML and the main activity in Java, detailing the setup of the MapView, markers, and polyline for the route. Additionally, it outlines necessary permissions and metadata for the AndroidManifest.xml file to enable location access and API key integration.

Uploaded by

ganeshkumbhar638
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)
9 views4 pages

Practical No. 32

The document provides a code example for an Android application that uses Google Maps to draw a route between two locations, Kolhapur and Mumbai. It includes the layout in XML and the main activity in Java, detailing the setup of the MapView, markers, and polyline for the route. Additionally, it outlines necessary permissions and metadata for the AndroidManifest.xml file to enable location access and API key integration.

Uploaded by

ganeshkumbhar638
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/ 4

Q.

Write a program to draw a route between import


two locations com.google.android.gms.maps.model.Marker
Options;
activity_main.xml
import
<FrameLayout com.google.android.gms.maps.model.Polylin
eOptions;
xmlns:android="http://schemas.android.com/ import java.util.Arrays;
apk/res/android"
android:layout_width="match_parent" public class MainActivity extends
android:layout_height="match_parent"> AppCompatActivity implements
OnMapReadyCallback {
<com.google.android.gms.maps.MapView
android:id="@+id/mapView" private MapView mapView;
android:layout_width="match_parent" private GoogleMap googleMap;
android:layout_height="match_parent" /> private static final String
MAP_VIEW_BUNDLE_KEY =
</FrameLayout> "MapViewBundleKey";

MainActivity.java @Override
package com.example.practicalno32; protected void onCreate(Bundle
savedInstanceState) {
import androidx.annotation.NonNull; super.onCreate(savedInstanceState);
import setContentView(R.layout.activity_main);
androidx.appcompat.app.AppCompatActivity
; mapView =
import android.os.Bundle; findViewById(R.id.mapView);
import Bundle mapViewBundle = null;
com.google.android.gms.maps.CameraUpdat if (savedInstanceState != null) {
eFactory; mapViewBundle =
import
com.google.android.gms.maps.GoogleMap; savedInstanceState.getBundle(MAP_VIEW_
import BUNDLE_KEY);
com.google.android.gms.maps.MapView; }
import mapView.onCreate(mapViewBundle);
com.google.android.gms.maps.OnMapReady mapView.getMapAsync(this);
Callback; }
import
com.google.android.gms.maps.model.LatLng @Override
; public void onMapReady(@NonNull
GoogleMap map) {
googleMap = map;
mapView.onPause();
LatLng kolhapur = new LatLng(16.7050, super.onPause();
74.2433); }
LatLng mumbai = new LatLng(19.0760,
72.8777); @Override
protected void onStop() {
googleMap.addMarker(new mapView.onStop();
super.onStop();
MarkerOptions().position(kolhapur).title("Ko }
lhapur"));
googleMap.addMarker(new @Override
protected void onDestroy() {
MarkerOptions().position(mumbai).title("Mu mapView.onDestroy();
mbai")); super.onDestroy();
}
googleMap.moveCamera(CameraUpdateFact
ory.newLatLngZoom(kolhapur, @Override
6)); public void onLowMemory() {
super.onLowMemory();
googleMap.addPolyline(new mapView.onLowMemory();
PolylineOptions() }
.add(kolhapur)
.add(mumbai) @Override
.width(8) protected void
.color(android.graphics.Color.RED)); onSaveInstanceState(@NonNull Bundle
} outState) {
super.onSaveInstanceState(outState);
@Override
protected void onStart() { Bundle mapViewBundle =
super.onStart(); outState.getBundle(MAP_VIEW_BUNDLE_
mapView.onStart(); KEY);
} if (mapViewBundle == null) {
mapViewBundle = new Bundle();
@Override
protected void onResume() { outState.putBundle(MAP_VIEW_BUNDLE_
super.onResume(); KEY, mapViewBundle);
mapView.onResume(); }
}
mapView.onSaveInstanceState(mapViewBun
@Override dle);
protected void onPause() { }
} <meta-data

AndroidManifest.xml android:name="com.google.android.geo.API
_KEY"
<?xml version="1.0" encoding="utf-8"?> android:value="YOUR_API_KEY" />
<manifest <!-- Replace with your actual API key -->
xmlns:android="http://schemas.android.com/
apk/res/android" <activity
android:name=".MainActivity"
xmlns:tools="http://schemas.android.com/too android:exported="true">
ls"> <intent-filter>
<action
<uses-permission android:name="android.intent.action.MAIN"
android:name="android.permission.ACCESS />
_FINE_LOCATION" /> <category
<uses-permission android:name="android.intent.category.LAU
android:name="android.permission.ACCESS NCHER" />
_COARSE_LOCATION" /> </intent-filter>
<uses-permission </activity>
android:name="android.permission.INTERN </application>
ET" />
</manifest>
<application
android:allowBackup="true"

android:dataExtractionRules="@xml/data_ex
traction_rules"

android:fullBackupContent="@xml/backup_
rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_
round"
android:supportsRtl="true"

android:theme="@style/Theme.PracticalNo3
2"
tools:targetApi="31">

You might also like