문제

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.

도움이 되었습니까?

해결책

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top