Shahbaz Kaware

Shahbaz Kaware

  • NA
  • 535
  • 31.5k

How to calculate fare using distance matrix api asp.net c#

Apr 30 2017 1:33 AM
<head runat="server">
<title></title>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/distancematrix/xml?origins="></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAI7lEkgNw-eaO6SMezTWme3rl-jUsujCY&sensor=false">
</script>
<script lang="javascript" type="text/javascript">
var panorama;
var DistanceMatrix;
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
function InitializeMap() {
directionsDisplay = new google.maps.DirectionsRenderer();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions =
{
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directionpanel'));
var control = document.getElementById('control');
control.style.display = 'block';
}
function calcRoute() {
debugger;
var start = document.getElementById('startvalue').value;
var end = document.getElementById('endvalue').value;
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
//var hdnfldVariable = document.getElementById('hdnfldVariable');
//hdnfldVariable.value = request;
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
function initialize() {
panorama = new google.maps.StreetViewPanorama(
document.getElementById('street-view'),
{
position: { lat: 37.869260, lng: -122.254811 },
pov: { heading: 165, pitch: 0 },
zoom: 1
});
}
function Dist()
{
DistanceMatrix = new google.maps.
}
function Button1_onclick() {
debugger;
calcRoute();
initialize();
}
window.onload = InitializeMap;
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="hdnfldVariable" runat="server" />
<table id="control">
<tr>
<td>
<table>
<tr>
<td>From:</td>
<td>
<%--<input id="startvalue" type="text" style="width: 305px" />--%>
<asp:TextBox ID="startvalue" runat="server" class="form-control input-lg"></asp:TextBox>
</td>
</tr>
<tr>
<td>To:</td>
<td>
<%--<input id="endvalue" type="text" style="width: 301px" />--%>
<asp:TextBox ID="endvalue" runat="server" class="form-control input-lg"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<%--<input id="Button1" type="button" value="GetDirections" onclick="return Button1_onclick()" />--%>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click1" Text="GetDirections" OnClientClick="Button1_onclick(); return false;" />
</td>
<%-- <td align="right">
<asp:Button ID="btnCalFare" runat="server" OnClick="btnCalFare_Click" Text="Cal Fare" />
</td>
<td align="right">
<asp:Label ID="lblFare" runat="server" ></asp:Label>
</td>--%>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top">
<div id="directionpanel" style="height: 390px; overflow: auto"></div>
</td>
<td valign="top">
<div id="map" style="height: 390px; width: 489px"></div>
</td>
</tr>
<tr>
<td class="style2">
Start Value</td>
<td >
<asp:Label ID="startvalue2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
End Value</td>
<td >
<asp:Label ID="endvalue2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
Distance</td>
<td >
<asp:Label ID="Lbl_Distance" runat="server" Font-Bold="True"></asp:Label>
</td>
</tr>
<tr>
<td class="style3">
Total Fare</td>
<td class="style4">
<%--<asp:Label ID="Lbl_Fare0" runat="server" Font-Bold="True">£</asp:Label>--%>
&nbsp;
<asp:Label ID="Lbl_Fare" runat="server" Font-Bold="True"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
public partial class Pickup3 : System.Web.UI.Page
{
string dkm, ds, distance;
string origin, destination;
double conv = 0.62137;
double miles, d2;
string xmlResult = null;
XmlDocument doc = new XmlDocument();
static int Taxicount;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//double dist = Convert.ToDouble(Session["taxi_distance"]);
double veh = Convert.ToDouble(Session["taxi_vehicle"]);
//startvalue2.Text = Session["taxi_origin"].ToString();
//endvalue2.Text = Session["taxi_destination"].ToString();
}
}
protected void Button2_Click1(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + startvalue.Text + "&destinations=" + endvalue.Text + "&mode=Car&language=us-en&sensor=false");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader resStream = new StreamReader(response.GetResponseStream());
xmlResult = resStream.ReadToEnd();
doc.LoadXml(xmlResult);
try
{
if (doc.DocumentElement.SelectSingleNode("/DistanceMatrixResponse/row/element/status").InnerText.ToString().ToUpper() != "OK")
{
//Response.Write("<script>alert('Invalid City Name please try again');</script>");
return;
}
XmlNodeList xnList = doc.SelectNodes("/DistanceMatrixResponse");
foreach (XmlNode xn in xnList)
{
if (xn["status"].InnerText.ToString() == "OK")
{
dkm = doc.DocumentElement.SelectSingleNode("/DistanceMatrixResponse/row/element/distance/text").InnerText;
ds = dkm.Replace("km", " ");
distance = ds.Trim();
d2 = Convert.ToDouble(distance);
// miles = conv * d2;
origin = xn["origin_address"].InnerText.ToString();
destination = xn["destination_address"].InnerText.ToString();
Session.Add("taxi_origin", origin);
Session.Add("taxi_destination", destination);
// Session.Add("taxi_distance", miles);
Session.Add("shams", "Return Date and Time");
//Session.Add("taxi_cartype", Rdb_CarType.SelectedValue);
//double dist = Convert.ToDouble(Session["taxi_distance"]);
double veh = Convert.ToDouble(Session["taxi_vehicle"]);
//Lbl_Distance.Text = Math.Round(dist, 2).ToString() + " miles";
// Lbl_Fare.Text = Math.Round(dist * 2.5 * veh, 2).ToString();
string a = "1";
Session.Add("BookTaxi", a);
Taxicount++;
Session.Add("TaxiCount", Taxicount.ToString());
if (Session["taxi_oneway"] == "One Way")
{
//Lbl_Distance.Text = Math.Round(dist, 2).ToString() + " miles";
// Lbl_Fare.Text = Math.Round(dist * 2.5 * veh, 2).ToString();
}
Session.Add("totalfare", Lbl_Fare.Text);
//Session.Add("totaldistance", Lbl_Distance.Text);
}
}
}
catch
{
Response.Write("<script>alert('Server Busy Try After some time !!!');</script>");
return;
}
}
}
These are my codes how to calculate fare of between origin and destination as per kilometer plz help guys