Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Result always return empty object #101

Closed
stefensuhat opened this issue Sep 23, 2017 · 20 comments
Closed

Result always return empty object #101

stefensuhat opened this issue Sep 23, 2017 · 20 comments

Comments

@stefensuhat
Copy link

stefensuhat commented Sep 23, 2017

General Information

GeocoderLaravel Version: ^4.0
Laravel Version: 5.5.11
PHP Version: 7.2
Operating System and Version: Windows 10

Description

I have this simple code to try out this package:

return app('geocoder')->geocode('Los Angeles, CA')->get();

But everytime it always return me

[
{},
{},
{},
{},
{}
]

this is my geocoder.php:


return [
    'cache-duration' => 0,
    'providers' => [
        Chain::class => [
            GoogleMaps::class => [
                'en-US',
                env('GOOGLE_MAPS_API_KEY'),
            ],
        ],
        GoogleMaps::class => [
            'us',
            env('GOOGLE_MAPS_API_KEY'),
        ],
    ],
    'adapter'  => Client::class,
];

All GOOGLE_MAPS_API_KEY already setup. If I run dd(app('geocoder')->geocode('Los Angeles, CA')->get()) I can see the results. Only when I put return it return me empty.

@mikebronner
Copy link
Member

Can you paste the method where you are returning the geo coded results? I wonder if the return type is force-typecasting the results? Are you using a return type there?

@stefensuhat
Copy link
Author

@mikebronner no. my code only return app('geocoder')->geocode('Los Angeles, CA')->get(); I just want to get the collection result.

I've tried changed the ->get() to ->first() it returns me
Call to undefined method Geocoder\Laravel\ProviderAndDumperAggregator::first()

@mikebronner
Copy link
Member

mikebronner commented Sep 23, 2017

Please paste the entire method you have your code in.

There is no first() method, you have to do first on the result:
app('geocoder')->geocode('...')->get()->first();, since the get() method returns a collection.

@stefensuhat
Copy link
Author

stefensuhat commented Sep 23, 2017

@mikebronner only this method i have return app('geocoder')->geocode('Los Angeles, CA')->get();

for first() method i've just tried to see if it resolve the issue.

if I dd(app('geocoder')->geocode('Los Angeles, CA')->get()) this is the result:

Collection {#561 ▼
  #items: array:5 [▼
    0 => GoogleAddress {#659 ▼
      -id: "ChIJV_npp981fYcRDwLg180Pemk"
      -locationType: "ROOFTOP"
      -resultType: array:1 [▶]
      -formattedAddress: "13024 SD-244, Keystone, SD 57751, USA"
      -streetAddress: null
      -intersection: null
      -political: "United States"
      -colloquialArea: null
      -ward: null
      -neighborhood: null
      -premise: null
      -subpremise: null
      -naturalFeature: null
      -airport: null
      -park: null
      -pointOfInterest: null
      -establishment: null
      -subLocalityLevels: AdminLevelCollection {#662 ▶}
      -coordinates: Coordinates {#653 ▶}
      -bounds: Bounds {#654 ▶}
      -streetNumber: "13024"
      -streetName: "South Dakota 244"
      -subLocality: null
      -locality: "Keystone"
      -postalCode: "57751"
      -adminLevels: AdminLevelCollection {#660 ▶}
      -country: Country {#658 ▶}
      -timezone: null
      -providedBy: "google_maps"
    }
  ]
}

I can't get the value inside GoogleAddress object

@mikebronner
Copy link
Member

I am unable to replicate the issue. Unit you can provide me your entire code block, there is not much I can do to help. As previously requested, please provide the entire method that contains your code return app('geocoder')->geocode('Los Angeles, CA')->get();. Please review the documentation https://github.com/geocoder-php/Geocoder to see how GeoCoder should be used.

To inspect your results, you can also do this: return app('geocoder')->geocode('Los Angeles, CA')->get()->toArray();.

@stefensuhat
Copy link
Author

@mikebronner what code block do you need? Like I said before, I just tried this package to see if its working or not. and my code only this:

Route::get('/', function () {
        $geo = app('geocoder')->reverse(43.882587,-103.454067)->get();
        return $geo;
    });

the return result is:

[
{},
{},
{},
{},
{}
]

I've tried to put ->get()->toArray(); same result.

@mikebronner
Copy link
Member

mikebronner commented Sep 24, 2017

A route returns a view or html. It will not render objects. Using dd() is the correct way to look at the output when debugging. You will need to loop over the results and output them within HTML like you would any other Laravel collection.

@stefensuhat
Copy link
Author

@mikebronner but if you the return statement like what I did, it suppose to return an json object (that's why I cant receive [{},{}].

I've done this so many times when trying an query or something else to see if its working or not.

And fyi I've tried to call it via controller with same exact code still return same object.

@mikebronner
Copy link
Member

mikebronner commented Sep 24, 2017

The objects in the collection aren't Laravel models and won't automatically render to JSON. they are normal PHP objects wrapped in a collection.

@humblecoder
Copy link

So, is there no way to convert an individual Address object to JSON? I wouldn't doubt I'm missing something, but returning a collection of PHP objects with no intuitive means of navigation/interpretation doesn't seem helpful. 🤔

@mikebronner
Copy link
Member

mikebronner commented Jul 15, 2018

@humblecoder You could always do something like this:

$json = $geocoderResultCollection
    ->map(function ($result) {
        return json_encode($result);
    })
    ->toJson();

Let me know how that works.

@humblecoder
Copy link

@mikebronner Hrm, your method returns an empty set. I ended up manually building a hash with each function individually (e.g. getFormattedAddress(), getStreetName(), getLocality())
😖

@mikebronner
Copy link
Member

@humblecoder would you mind sharing your solution? perhaps it will help with #132

@humblecoder
Copy link

Basically I did the following (assumes you're inside a Laravel controller):

Step 1) Calculate address.

$reverse = Geocoder::reverse($request->input('latitude'), $request->input('longitude'))->get();

Step 2) Acquire relevant collection value. In my case it's the index of the 'ROOFTOP' object.

$relevant = $reverse[0]; //JUST IN CASE NOTHING IS FOUND IN THE NEXT STEP

foreach($reverse as $rev){
  if('ROOFTOP' == $rev->getLocationType()){
    $relevant = $rev;
  }
}

Step 2) Call built-in methods on that particular collection value.

return response()->json([
  'formatted_address' => $relevant->getFormattedAddress(),
  'street' => ($relevant->getStreetNumber() ?? '') . $relevant->getStreetName(),
  'city' => $relevant->getLocality(),
  'state_province' => $relevant->getAdminLevels()->first()->getName(),
  'postal_code' => $relevant->getPostalCode(),
],200);

@mikebronner
Copy link
Member

mikebronner commented Jul 16, 2018

@humblecoder Thanks for sharing. This will hopefully help others in the meantime.

The reason for this limitation is that the underlying Geocoder library does not support automatic conversion to json, like eloquent collections with models do. This package is just a nice wrapper to give standard collections around the objects returned by the Geocoder library.

mikebronner added a commit that referenced this issue Nov 3, 2018
@mikebronner
Copy link
Member

mikebronner commented Nov 3, 2018

@humblecoder @stefensuhat I don't know why I didn't bring this up earlier. You can get the JSON representation via ->dump("geojson") instead of ->get(). I added a shortcut in 4.0.21:

app('geocoder')
    ->geocode('1600 Pennsylvania Ave NW, Washington, DC 20500, USA')
    ->toJson();

Unfortunately the classes in the collection do not have a toArray() method, which would enable toJson() to work on the collection.

@dungnh
Copy link

dungnh commented Feb 13, 2020

I have same issue as this on Laravel 6.x and PHP 7.3. Please help.

$output = app('geocoder')->geocode('Los Angeles, CA')->get();
dd($output);

and the result is:

Illuminate\Support\Collection {#22243 ▼
  #items: []
}

@luiscolmenares
Copy link

I have same issue as this on Laravel 6.x and PHP 7.3. Please help.

$output = app('geocoder')->geocode('Los Angeles, CA')->get();
dd($output);

and the result is:

Illuminate\Support\Collection {#22243 ▼
  #items: []
}

@dungnh try this:
$output = json_decode(app('geocoder')->geocode('Los Angeles, CA')->tojson());

It works as a workaround from what @mikebronner mentioned.

@omarmfs98
Copy link

omarmfs98 commented May 6, 2020

I am getting the same answer, please reopen this error

Illuminate\Support\Collection {#457 ▼
  #items: []
}

@mikebronner
Copy link
Member

@omarmfs98 Please provide the code you are using to get the output. dd() and dump() will not provide results, as the object contained in the collection don't fall back to an array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants