Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views7 pages

Next Steps

The document outlines steps for integrating Google Maps for live bus tracking, setting up a backend API for fetching bus route data, and implementing a payment gateway for ticket purchases in an Android app. It includes code snippets for adding dependencies, creating Retrofit clients, and handling user interactions in various activities. Additionally, it emphasizes improving user interface design and responsiveness across different screen sizes.

Uploaded by

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

Next Steps

The document outlines steps for integrating Google Maps for live bus tracking, setting up a backend API for fetching bus route data, and implementing a payment gateway for ticket purchases in an Android app. It includes code snippets for adding dependencies, creating Retrofit clients, and handling user interactions in various activities. Additionally, it emphasizes improving user interface design and responsiveness across different screen sizes.

Uploaded by

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

Next Steps:

Map Integration: For the BusTrackingActivity, you could integrate Google Maps or
other map APIs to show live bus locations.

Backend Integration: For RouteInfoActivity, you could pull real-time bus route data
from a backend server using APIs.

Payment Gateway: For the TicketingActivity, you could integrate a payment system
for ticket purchases (such as Google Pay, Razorpay, etc.).

Styling & Design: You can improve the user interface by adding custom styles and
making the layout responsive for different screen sizes.

1. Google Maps Integration for Live Bus Tracking


To implement Google Maps in your app for bus tracking, you need to set up Google
Maps and display the bus's current location on a map.

1.1. Add Google Maps Dependency


In your build.gradle file, add the following dependencies to enable Google Maps:

gradle

dependencies {
implementation 'com.google.android.gms:play-services-maps:17.0.1'
implementation 'com.google.android.gms:play-services-location:17.0.0'
}

Google Maps API Key


Go to the Google Cloud Console.
Create a new project or use an existing one.
Enable the Google Maps SDK for Android.
Generate an API Key for your project.
Add the API key to your app's AndroidManifest.xml:

<application
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light">

<!-- Add Google Maps API Key -->


<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_GOOGLE_MAPS_API_KEY" />

<activity android:name=".BusTrackingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

. BusTrackingActivity.java with Google Maps


Now, in your BusTrackingActivity, integrate Google Maps to show the bus's location.
package com.example.pmpmlapp;

import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class BusTrackingActivity extends FragmentActivity implements


OnMapReadyCallback {

private GoogleMap mMap;

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

// Get the map fragment


SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

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

// For this example, we'll just add a dummy bus location


LatLng busLocation = new LatLng(18.5204, 73.8567); // Pune coordinates
(replace with live data)
mMap.addMarker(new MarkerOptions().position(busLocation).title("Bus
Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(busLocation, 15));

// You can update the bus's location in real-time with the location data
from your server
}
}

activity_bus_tracking.xml with Map Fragment


Modify your layout to include a SupportMapFragment for displaying the map:

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>

Backend API for Bus Routes


Next, we'll set up a simple backend to serve bus route data via a REST API. For
simplicity, I'll show you how to consume a sample bus route API and display it in
your app.

2.1. Create API Using Retrofit


Add Retrofit to your app to make network calls. In build.gradle, add the following:

gradle

dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}

Create Retrofit Client


Create a RetrofitClient.java class to make network calls.

package com.example.pmpmlapp;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {

private static Retrofit retrofit = null;

public static Retrofit getClient() {


if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com/") // Replace with actual API
endpoint
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}

Bus Route API Interface


Create an interface for your bus route API.

java

package com.example.pmpmlapp;

import retrofit2.Call;
import retrofit2.http.GET;
public interface BusRouteService {

@GET("routes") // Replace with actual API endpoint


Call<BusRouteResponse> getBusRoutes();
}

Bus Route Response Model


Create a model to parse the bus route response from the API.

java

package com.example.pmpmlapp;

import com.google.gson.annotations.SerializedName;

import java.util.List;

public class BusRouteResponse {

@SerializedName("routes")
private List<String> routes;

public List<String> getRoutes() {


return routes;
}

public void setRoutes(List<String> routes) {


this.routes = routes;
}
}

Fetching Bus Routes in RouteInfoActivity


In your RouteInfoActivity.java, call the backend API to fetch and display bus
routes.

package com.example.pmpmlapp;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RouteInfoActivity extends AppCompatActivity {

private TextView routeInfo;

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

routeInfo = findViewById(R.id.routeInfo);

BusRouteService service =
RetrofitClient.getClient().create(BusRouteService.class);
Call<BusRouteResponse> call = service.getBusRoutes();

call.enqueue(new Callback<BusRouteResponse>() {
@Override
public void onResponse(Call<BusRouteResponse> call,
Response<BusRouteResponse> response) {
if (response.isSuccessful() && response.body() != null) {
StringBuilder routeList = new StringBuilder("Available Routes:\
n");
for (String route : response.body().getRoutes()) {
routeList.append(route).append("\n");
}
routeInfo.setText(routeList.toString());
} else {
Toast.makeText(RouteInfoActivity.this, "Failed to fetch
routes", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<BusRouteResponse> call, Throwable t) {
Toast.makeText(RouteInfoActivity.this, "Error: " + t.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
}
}

Payment Gateway for Ticket Purchase

To integrate a payment gateway like Razorpay or Google Pay, you would need to
follow the official SDK integration guide. Below is an outline for using Razorpay:

3.1. Add Razorpay Dependency


Add Razorpay to your build.gradle:

gradle

dependencies {
implementation 'com.razorpay:checkout:1.6.15'
}

Set Up Razorpay Checkout


In your TicketingActivity.java, integrate the payment checkout.

package com.example.pmpmlapp;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.razorpay.Checkout;
import com.razorpay.PaymentResultListener;

import org.json.JSONObject;

public class TicketingActivity extends AppCompatActivity implements


PaymentResultListener {

Button buyTicketButton;

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

buyTicketButton = findViewById(R.id.buyTicketButton);

Checkout.preload(getApplicationContext());

buyTicketButton.setOnClickListener(v -> {
startPayment();
});
}

private void startPayment() {


Checkout checkout = new Checkout();
checkout.setKeyID("YOUR_RAZORPAY_KEY_ID");

try {
JSONObject options = new JSONObject();
options.put("name", "PMPML");
options.put("description", "Bus Ticket");
options.put("currency", "INR");
options.put("amount", 100 * 100); // 100 INR in paise

checkout.open(this, options);
} catch (Exception e) {
Toast.makeText(this, "Payment Error: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}

@Override
public void onPaymentSuccess(String s) {
Toast.makeText(this, "Payment Success: " + s, Toast.LENGTH_SHORT).show();
}

@Override
public void onPaymentError(int i, String s) {
Toast.makeText(this, "Payment Failed: " + s, Toast.LENGTH_SHORT).show();
}
}

You might also like