Parse.com PHP GeoPoint- Laravel

I recently had a project that required an app to collect coordinates and display the location via an online dashboard.

I decided to use Parse.com as the backend for the app and the site. For the front end web part I went with the Laravel framework.

I came across the following issues when trying to echo out the coordinates from my parse table.

Object of class Parse\ParseGeoPoint could not be converted to string

Cannot use object of type Parse\ParseGeoPoint as array

The issue appeared to be related to a json conversion in Laravel as discussed here.

The fix was to encode the coordinate values to return array data if there is any.

Error:


$query2 = new ParseQuery("table");
$query2->select("coordinates");
$query2->select("username");
$query2->select("comments");
$query2->descending("createdAt");
$query2->limit(5);
$results2 = $query2->find();
if ($results2) {
//Display coordinates limit to 5
foreach($results2 as $row2){

echo $row2->username;
echo $row2->coordinates;
echo $row2->comments;
echo date_format($row2->getCreatedAt(), 'Y-m-d');

}
}

Encoding fix:

$query2 = new ParseQuery("table");
$query2->select("coordinates");
$query2->select("username");
$query2->select("comments");
$query2->descending("createdAt");
$query2->limit(5);
$results2 = $query2->find();
if ($results2) {
//Display coordinates limit to 5 
foreach($results2 as $row2){
 //encoding the coordinate values to return array data if there is data
 $values = $row2->coordinates->_encode();
 echo $row2->username;
 echo $values['latitude'] . ',' . $values['longitude']; 
 echo $row2->comments;
 echo date_format($row2->getCreatedAt(), 'Y-m-d');

}
}

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *