0

folks, I can't get this to work. What I'm trying to do is to calculate the distance between latitude/longitude pairs, using a function. I'm retrieving the data from my wordpress db table. All is ok up to this point. The problem I'm facing is that the first item retrieved from database calculates the distance perfectly, but all the others get repeated the same wrong value. I'm trying to generate a json response for my api.

Here's the function to calculate the distance:

 function get_distance($lat1, $lon1, $lat2, $lon2) {

  $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;
 
  return ($miles * 1.609344);
            

}

Here's the function that retrieves data from db and calls get_distance() to calculate:

    function listing_by_coordinates($city){
    
        $cat = array();

            $ucat = array(
        'post_type' => 'listar',
        'posts_per_page' => 10,
        'tax_query' => array(
            array(
                'taxonomy' => 'listar_category',
                'field' => 'slug',
                'terms' => $city,
            )
         )
        );

         
    $postID;
     
    $features = get_posts($ucat);
         
    $postID;
     
    $myresult = [];
    $i = 0;
    $dist = 0;

    foreach($features as $feature) {

    $postID = $feature->ID;
    
    $lat2 = get_field('latitude', $postID);
    $lon2 = get_field('longitude', $postID);
        
    $dist = get_distance('-25.440766549426275','-49.27733841639869',$lat2,$lon2);
        
    $cat[$i]['name'] = $feature->post_title;
    $cat[$i]['latitude'] = $lat2;
    $cat[$i]['longitude'] = $lon2; 
    $cat[$i]['distance'] = $dist;
                    
        $myresult[$i]=$cat;
        $i ++;
    
 } 
    return $cat;
        
    }

AND HERE IS THE RESULT. Note that the first entry has a correct distance, but all the rest gets the same wrong value.

  {
    "name": "PIZZAPP Batel",
    "latitude": "-25.44089532023346",
    "longitude": "-49.27379207908012",
    "distance": 0.356366447483585979671971699644927866756916046142578125
  },
  {
    "name": "yummizza",
    "latitude": "-25,433287",
    "longitude": "-49,272498",
    "distance": 56.39252784156145281713179429061710834503173828125
  },
  {
    "name": "Zak Pizzas",
    "latitude": "-25,480155",
    "longitude": "-49,335803",
    "distance": 56.39252784156145281713179429061710834503173828125
  },
  {
    "name": "Zap Pizzaria",
    "latitude": "-25,379378",
    "longitude": "-49,254974",
    "distance": 56.39252784156145281713179429061710834503173828125
  },
  {
    "name": "Zap Pizzaria Água Verde",
    "latitude": "-25,461179",
    "longitude": "-49,294455",
    "distance": 56.39252784156145281713179429061710834503173828125
  },
  {
    "name": "Ziza Pizza",
    "latitude": "-25,517236",
    "longitude": "-49,295935",
    "distance": 56.39252784156145281713179429061710834503173828125
  },
  {
    "name": "Zupt Sabor Pizza Burger Grill",
    "latitude": "-25,397456",
    "longitude": "-49,270823",
    "distance": 56.39252784156145281713179429061710834503173828125
  },
  {
    "name": "XBom Lanches",
    "latitude": "-25,434282",
    "longitude": "-49,276825",
    "distance": 56.39252784156145281713179429061710834503173828125
  },
  {
    "name": "Yankees Pizzaria",
    "latitude": "-25,460863",
    "longitude": "-49,201307",
    "distance": 56.39252784156145281713179429061710834503173828125
  },
  {
    "name": "Yellow Pizzaria",
    "latitude": "-25,475251",
    "longitude": "-49,296798",
    "distance": 56.39252784156145281713179429061710834503173828125
  }

Please help!

1 Answer 1

2

This is causing because the first entry has a correct longitude and latitude, but all the rest have comma instead of decimal point in the values of longitudes and latitudes. Like the second json value "latitude" is "-25,433287" should be "-25.433287" and "longitude" is "-49,272498" should be "-49.272498". Same goes for the rest of the values.

{
    "name": "PIZZAPP Batel",
    "latitude": "-25.44089532023346",
    "longitude": "-49.27379207908012",
    "distance": 0.356366447483585979671971699644927866756916046142578125
  },
  {
    "name": "yummizza",
    "latitude": "-25,433287", //should be "-25.433287"
    "longitude": "-49,272498", //should be "-49.272498"
    "distance": 56.39252784156145281713179429061710834503173828125
  },
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, THANK YOU!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.