Question

I want to have a page where after selecting a city the adjoining map will point to that city.I need to have two fields with autosuggest option where the fields will be populated with places of closest relevance. The distance will be calculated after selection of these places automatically.We want to implement a google map API for this.

Was it helpful?

Solution

Make a AJAX request containing the two coordinates of your cities that the API found.

In the PHP you can calculate the distance between two points without having to use the Javascript map api.

If you can have the coordinates without using the API you could calculate directly in PHP...

Javascript:

$.get( php_script , function( data ) {
   alert( "The distance is "+data+" km" );
});

PHP:

unset($params);
$params["point1"]["lat"]; 
$params["point1"]["lng"]; 
$params["point2"]["lat"]; 
$params["point2"]["lng"]; 
echo orthodromy($params);

function orthodromy($params) {
   unset($lat1, $lng1, $lat2, $lng2);
   $lat1 = $params["point1"]["lat"]; 
   $lng1 = $params["point1"]["lng"]; 
   $lat2 = $params["point2"]["lat"]; 
   $lng2 = $params["point2"]["lng"]; 

   if (!is_numeric($lat1) || !is_numeric($lng1) || !is_numeric($lat2) 
       || !is_numeric($lng2)) return false;

   unset($dist);
   $dist = acos(
      sin( deg2rad($lat1) ) *
      sin( deg2rad($lat2) ) +
      cos( deg2rad($lat1) ) *
      cos( deg2rad($lat2) ) *
      cos( deg2rad($lng1-$lng2) )
   ) * 6371;
   if (!is_numeric($dist)) return false; else return $dist;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top