jcadima
9/4/2018 - 8:31 PM

Laravel Get Latitude Longitude


<?php

// CONTROLLER METHOD - returns lat/lon
public function getLatitudeLongitude( $location ) {
    $geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=' . urlencode( $location ) . '&sensor=false');
    $output[] = json_decode($geocode);    
    return $output;    

}



public function store( Request $request ) {

    $loc = $this->getLatitudeLongitude( $request->location ) ;

    $this->validate($request, [
          ...
          'latitude'   => 'sometimes',
          'longitude'  => 'sometimes'
          ...

    ]);

    $property = Property::create([
        ...
        'latitude'         => $loc[0]->results[0]->geometry->location->lat,
        'longitude'        => $loc[0]->results[0]->geometry->location->lng,
        ...
    ]);

    if( $request->hasFile('image') ) 
        $this->StoreFile( $request->file('image'), $property, '/properties', 'image' ) ;              

    Session::flash('success', 'Property created succesfully.');
    return redirect()->route('property.index');  

}