-3

It is a taxi application that gets the customer's location (lat,long) and calculates the travel distance between every taxi driver and the customer, In order to know which taxi driver is closer to the customer.

I have all the locations of taxi drivers stored in web database. and I have searched for methods that takes lat1,lat2 long1,long2 and returns the distance in Kilo meter.

Google maps has Distance Matrix API but it prohibits using it without showing the map
(I do not need the map, If you know a possible solution for this, I will be able to use google-maps service) Fortunately, Bing maps allows calculating travel distance without showing the map! in the tutorial below: http://msdn.microsoft.com/en-us/library/gg636957.aspx

but I am facing tow issues with it :
* it takes the location as a String (location name) and I want to give it as float (lat and long)
*I do not know how to use XML with PHP (how to store driving distance in a PHP variable)

I have been searching for one week and I will be very thankful for your help ()

Lana
  • 148
  • 1
  • 3
  • 12
  • Google matrix api do not say this... https://developers.google.com/maps/documentation/distancematrix/#DistanceMatrixRequests This is the json returned -> https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR – Marco Mura Dec 12 '14 at 13:25
  • 1
    @MarcoMura they said it the terms of service it is porhipeted to use it without maps but thanks for the link – Lana Dec 12 '14 at 16:47

1 Answers1

1

You can transform the location name into a latitude and longitude using the Google Maps Geocoding API. For example do

$location = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA");

Documentation: https://developers.google.com/maps/documentation/geocoding/

You can access the content as PHP object using json_decode. So this should work:

print_r(json_decode($location));

Using the location variable from above. The lat and lon are in there.

As for XML in PHP: using SimpleXMLElement is probably the easiest: http://php.net/manual/en/simplexml.examples-basic.php

You don't need a distance API by the way, if you have the latitude and longitude of two locations you can calculate the distance yourself. The simplest way is simply a triangle and Pythagoras but because the Earth is a sphere this is not exact. You can copy/paste this snippet. I took it from http://www.geodatasource.com/developers/php.

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) 
    * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) 
    * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344);
  } else if ($unit == "N") {
    return ($miles * 0.8684);
  } else {
    return $miles;
  }
}
Luc Hendriks
  • 2,473
  • 13
  • 18