geocoder-php / GeocoderLaravel

Geocoder service provider for Laravel
http://geocoder-php.org/GeocoderLaravel/
MIT License
704 stars 102 forks source link

getLatitude and getLongitude #158

Closed madn3z closed 5 years ago

madn3z commented 5 years ago

General Information

GeocoderLaravel Version: 4.2 Laravel Version: 5.8 PHP Version: 7.1.3 Operating System and Version: Windows

Issue Description

I am trying to get latitude and longitude with the following code, but it tells me that the "getLatitude" or "getLongitude" method does not exist.

$geocode = Geocoder::geocode($direccion)->get();` 
$geocode->getLatitude();

error:

message: "Method Illuminate\Support\Collection::getLatitude does not exist.",…}
exception: "BadMethodCallException"
file: "C:\xampp\htdocs\vuesax\vendor\laravel\framework\src\Illuminate\Support\Traits\Macroable.php"
line: 104
message: "Method Illuminate\Support\Collection::getLatitude does not exist."

with dd($geocode) the following appears to me:

Collection {#496
  #items: array:1 [
    0 => GoogleAddress {#486
      -id: "ChIJFR4I3sXaYpYRzeMjpvVWvSA"
      -locationType: "ROOFTOP"
      -resultType: array:1 [
        0 => "street_address"
      ]
      -formattedAddress: "Piramide 1000, San Miguel, Región Metropolitana, Chile"
      -streetAddress: null
      -intersection: null
      -political: "Chile"
      -colloquialArea: null
      -ward: null
      -neighborhood: null
      -premise: null
      -subpremise: null
      -naturalFeature: null
      -airport: null
      -park: null
      -pointOfInterest: null
      -establishment: null
      -subLocalityLevels: AdminLevelCollection {#490
        -adminLevels: []
      }
      -partialMatch: false
      -coordinates: Coordinates {#499
        -latitude: -33.501497
        -longitude: -70.651687
      }
      -bounds: Bounds {#500
        -south: -33.502845980291
        -west: -70.653035980292
        -north: -33.500148019708
        -east: -70.650338019708
      }
      -streetNumber: "1000"
      -streetName: "Piramide"
      -subLocality: null
      -locality: "San Miguel"
      -postalCode: null
      -adminLevels: AdminLevelCollection {#501
        -adminLevels: array:3 [
          1 => AdminLevel {#502
            -level: 1
            -name: "Región Metropolitana"
            -code: "Región Metropolitana"
          }
          2 => AdminLevel {#503
            -level: 2
            -name: "Santiago"
            -code: "Santiago"
          }
          3 => AdminLevel {#504
            -level: 3
            -name: "San Miguel"
            -code: "San Miguel"
          }
        ]
      }
      -country: Country {#505
        -name: "Chile"
        -code: "CL"
      }
      -timezone: null
      -providedBy: "google_maps"
    }
  ]
}
mikebronner commented 5 years ago

@madn3z Hi, thanks for writing in. Your $geocode is a collection. It has no idea of the coordinates, because it may contain many results. This should work:

$geocode->first()->getLatitude();
madn3z commented 5 years ago

@mikebronner I did the test with what you told me, but now I throw another error.

message: "Call to undefined method Geocoder\Provider\GoogleMaps\Model\GoogleAddress::getLatitude()",…}
exception: "Symfony\Component\Debug\Exception\FatalThrowableError"
file: "C:\xampp\htdocs\vuesax\app\Http\Controllers\EmpresaController.php"
line: 221
message: "Call to undefined method Geocoder\Provider\GoogleMaps\Model\GoogleAddress::getLatitude()"
madn3z commented 5 years ago

@mikebronner I solved my problem, I was missing the following.

$geocode->getCoordinates()->getLatitude()

Thank you very much for your help.

mikebronner commented 5 years ago

Good find! I was about to reply with the same answer, you can view the Address model: https://github.com/geocoder-php/Geocoder/blob/master/src/Common/Model/Address.php