0% found this document useful (0 votes)
12 views

Practical No-32

The document discusses different ways to add markers to a Google Map in Android, including adding a simple marker with latitude and longitude, customizing the marker icon, adding an info window, clustering markers, and animating markers. It also provides the syntax to add a compass to the Google Map. An example program is given to draw a route between two locations on the map by getting the source and destination from EditTexts and opening the Google Maps app with the route URL.

Uploaded by

Prathmesh Pawar
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)
12 views

Practical No-32

The document discusses different ways to add markers to a Google Map in Android, including adding a simple marker with latitude and longitude, customizing the marker icon, adding an info window, clustering markers, and animating markers. It also provides the syntax to add a compass to the Google Map. An example program is given to draw a route between two locations on the map by getting the source and destination from EditTexts and opening the Google Maps app with the route URL.

Uploaded by

Prathmesh Pawar
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/ 5

Practical No - 32

Practical Related Questions


1] Explain the ways to add markers on the GoogleMap
Ans.
In Android, adding markers to a Google Map can be achieved using various methods
provided by the Google Maps Android API. Here are the common ways to add markers on a
Google Map in Android:
• Add a Simple Marker: You can add a simple marker to the map by specifying
its position using latitude and longitude coordinates.
LatLng markerPosition = new LatLng(latitude, longitude);
googleMap.addMarker(newMarkerOptions().position(markerPosition).title("MarkerTitle"));
• Customize Marker Icon: You can customize the marker icon to make it more
visually appealing or to represent different types of markers.
LatLng(latitude,longitude); MarkerOptionsmarkerOptions=newMarkerOptions()
.position(markerPosition).title("MarkerTitle")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker_icon));
googleMap.addMarker(markerOptions);
• Add Marker with Info Window: You can add aninfowindow to the marker to display
additional information when the user taps on it.
LatLngmarkerPosition=newLatLng(latitude,longitude);MarkerOptionsmarkerOptions= new
MarkerOptions() .position(markerPosition) .title("Marker Title") .snippet("Additional
information about the marker"); googleMap.addMarker(markerOptions);
• Cluster Markers: If you have multiple markers close to each other,you can use
clustering to group them together for better visualization.
Cluster Manager<MyItem>clusterManager=newClusterManager<>(context,googleMap);
googleMap.setOnCameraIdleListener(clusterManager);
• Animate Marker: You can animate the marker to move it smoothly from one
position to another.
LatLngstartPosition=newLatLng(startLatitude,startLongitude);LatLngendPosition=new
LatLng(endLatitude, endLongitude); Marker marker = googleMap.addMarker(new
MarkerOptions().position(startPosition).title("Marker Title"));
marker.setPosition(endPosition); // This animates the marker to the new position
2] Write the syntax for method which is used to add compass in Google Map
Ans. You can set compass by calling below
method:mMap.getUiSettings().setCompassEnabled(true);

Exercise
1] Write a program to draw a route between two locations
XMLFile
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:layout_width="400dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginTop="50dp"an
droid:src="@drawable/img" />
<EditText
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginLeft="70dp"
android:layout_marginTop="30dp"and
roid:hint="Enter
Source"android:id="@+id/t1"/>
<EditText
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginLeft="70dp"
android:layout_marginTop="30dp"an
droid:hint="Enter
Destination"android:id="@+id/t2"/>
<Button
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginLeft="70dp"
android:layout_marginTop="50dp"
android:text="Display"
android:onClick="fun"
android:textSize="20dp"
android:backgroundTint="#009688"/>
</LinearLayout>
JAVAFile
packagecom.cscorner.practicalno_32;
importandroidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
importandroid.view.View;
importandroid.widget.EditText;
publicclassMainActivityextendsAppCompatActivity{
EditText t1,t2;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=findViewById(R.id.t1);
t2=findViewById(R.id.t2);
}
publicvoidfun(Viewview){
String s1=t1.getText().toString().trim();
Strings2=t2.getText().toString().trim();
Uriuri=Uri.parse("https://www.google.com/maps/dir/"+s1+""+s2);
Intent i1=new Intent(Intent.ACTION_VIEW,uri);
i1.setPackage("com.google.android.apps.maps");
startActivity(i1);
}
}
Manifest.xml
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permissionandroid:name="android.permission.INTERNET"></uses-permission>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_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.PracticalNo32"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy