Code to do Distance Query