You have two option to find job by location
you required
1. Search users lat long
2. Job users Posted Lat long (it may be job location or job posted user location from database)
3. Query will be like -
SELECT zip,Round(((ACOS(SIN('$lat' * PI()/180) * SIN(latitude * PI()/180) + COS('$lat' * PI()/180) * COS(latitude * PI()/180) * COS(('$lon'-longitude) * PI()/180)) * 180/PI()) * 60 * 1.1515),(2)) AS distance FROM Jobs Having distance <= 30
Else
1. You have to call all jobs data in single query.
foreach($joblist as $job){
$milesresult = $this->calculateDistance($user_lat,$user_lon,$job['latitude'],$jobr['longitude']);
$miles = explode("-",$vendor['miles']);
$vendor_max_miles = $miles[1];
}
2. PHP function for lat long
function calculateDistance($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;
}
}
此查詢是否足夠,或者我必須使用彈性搜索進行快速處理? –