3

I'm fairly new to Laravel and PHP in general.

I'm trying to use PHPCoord to convert Northing/Easting to Lat/Lng.

I've installed it via Composer, and it appears in my composer.json file and vendor directory, but I don't know how to reference it in my code?

I'm pretty sure it's to do with name-spacing or the lack of an autoload.php, but I could be a million miles off.

I've tried use PHPCoord\PHPCoord; and use Php-Coord\Php-Coord;, but it fails to find the class.

(In the latter it doesn't seem to like the hyphen.)

I'm just dropping in the example code in my Laravel controller for now, to see if it's working:

$OSRef = new OSRef(500000, 200000); //Easting, Northing
$LatLng = $OSRef->toLatLng();
$GPSLatLng = $LatLng->toWGS84(); //optional, for GPS compatibility

$lat =  $LatLng->getLat();
$long = $LatLng->getLng();

dd($lat);

But I get Class 'App\Http\Controllers\OSRef' not found

n8udd
  • 657
  • 1
  • 9
  • 30
  • 2
    Welcome to PHP and Laravel .. perhaps this brief introduction to namespacing will help you out, as this is an issue with namespacing in PHP ... https://mattstauffer.co/blog/a-brief-introduction-to-php-namespacing – lagbox Aug 17 '18 at 12:14
  • Thanks. I tried looking for this sort of post, but was searching for 'Laravel Namespace' rather than PHP. I'll give that post a thorough read. – n8udd Aug 17 '18 at 12:18
  • np, good luck, have fun and enjoy Laravel – lagbox Aug 17 '18 at 12:37

2 Answers2

2

You have to include the OSRef with its namespace. Have a look at this answer.

So in your code you have to add this code at the beginning of the file. Like so:

<?php

namespace App\Http\Controlles;

use PHPCoord\OSRef;

class YourClass {

public function yourMethod() {
}
    $OSRef = new OSRef(500000, 200000); //Easting, Northing
    $LatLng = $OSRef->toLatLng();
    $GPSLatLng = $LatLng->toWGS84(); //optional, for GPS compatibility

    $lat =  $LatLng->getLat();
    $long = $LatLng->getLng();

    dd($lat);
}
IlGala
  • 3,331
  • 4
  • 35
  • 49
  • 1
    Thanks! So I was on the right lines, only I was using `use PHPCoord\PHPCoord` rather than `use PHPCoord\Osref`. I'll mark this as answer! – n8udd Aug 17 '18 at 12:16
1

use the correct class in your controller as

use PHPCoord\OSRef;
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70