Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

brunitosessa's avatar

Resource API data

Hi, I'm trying to send data to an App using Api resources. I have "Bar resource", that has to include the distance (using latitude and longitude) from an User to that Bar. I really don't know how to (or where to) send that latitude and longitude to calculate that distance.

Could someone help me?

Thanks a lot

0 likes
6 replies
a4ashraf's avatar

@brunitosessa

here is the solution to calculate the distance between two points

<?php 
		
	function twopoints_on_earth($latitudeFrom, $longitudeFrom, 
									$latitudeTo, $longitudeTo) 
	{ 
		$long1 = deg2rad($longitudeFrom); 
		$long2 = deg2rad($longitudeTo); 
		$lat1 = deg2rad($latitudeFrom); 
		$lat2 = deg2rad($latitudeTo); 
			
		//Haversine Formula 
		$dlong = $long2 - $long1; 
		$dlati = $lat2 - $lat1; 
			
		$val = pow(sin($dlati/2),2)+cos($lat1)*cos($lat2)*pow(sin($dlong/2),2); 
			
		$res = 2 * asin(sqrt($val)); 
			
		$radius = 3958.756; 
			
		return ($res*$radius); 
	} 

	// latitude and longitude of Two Points 
	$latitudeFrom = 19.017656 ; 
	$longitudeFrom = 72.856178; 
	$latitudeTo = 40.7127; 
	$longitudeTo = -74.0059; 
		
	// Distance between Mumbai and New York 
	print_r(twopoints_on_earth( $latitudeFrom, $longitudeFrom, 
					$latitudeTo, $longitudeTo).' '.'miles'); 

// This code is contributed by akash1295 
// https://auth.geeksforgeeks.org/user/akash1295/articles 
?> 

https://www.geeksforgeeks.org/program-distance-two-points-earth/

brunitosessa's avatar

Thanks for your answer @a4ashraf . I already have the code to calculate the distance. I just don't know where to put it to user it (according best practices of laravel). I need to send that information to an app (json) using an API resource.

Thanks a lot.

a4ashraf's avatar

@brunitosessa

you should create it in you controller or if you are using any pattern you should it in that particular pattern file

if you share you controller or pattern file I can help you

brunitosessa's avatar

Hi @a4ashraf ! Thanks for your help.

This is my User Controller:

class UserController extends Controller
{
    public function show($user_id)
    {
        return new UserResource(User::find($user_id));
    }
........

And this is my Bar Controller, where the Ionic App send the request with "lat" and "lng"

class BarController extends Controller
{
    public function index(Request $request) {
        $lat = $request->input('lat',0);
        $lng = $request->input('lng',0);

        return [
        	'estado' => 1,
        	'bares' => BarResource::collection(Bar::All()),
        ];   
    }

So, my Ionic App is sending to my laravel backend "lat" and "lng" and I should return with a Bar Resource like this:

class BarResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'city' => $this->city->id,
            'address' => $this->address,
            'distance' => *?????????????????*,
        ];
    }
......

So I really don't know where to put the method to calculate distance between user (Ionic App) and the bar. Should I put it into User's Model? Should I put it into Bar's Model? Should I put it into User Controller? Should I put it into Bar Resource calling an Action?

a4ashraf's avatar

@brunitosessa

here how you will add the calculate distance function in your controller

class BarController extends Controller
{
    public function index(Request $request) {
        $lat = $request->input('lat',0);
        $lng = $request->input('lng',0);

        $distance = $this->calculateDistance($lat, $lng);

        $request->request->add(['distance' => $distance]);

        return [
        	'estado' => 1,
        	'bares' => BarResource::collection(Bar::All()),
        ];   
    }

    protected function calculateDistance($lat, $lng) 
	{ 
		//your calculation code 
			
		return $distance; 
	} 
brunitosessa's avatar

Hi @a4ashraf . Thanks again for your help. I understand perfectly what you are saying, but I need to return that "distance" in BarResource to Ionic App. That's the part that I don't know how to join.

Thanks a lot!

Please or to participate in this conversation.