项目作者: spatie

项目描述 :
Geocode addresses to coordinates
高级语言: PHP
项目地址: git://github.com/spatie/geocoder.git
创建时间: 2014-05-01T19:37:31Z
项目社区:https://github.com/spatie/geocoder

开源协议:MIT License

下载






Logo for Geocoder



Geocode Addresses into Coordinates



Latest Version
MIT Licensed
run-tests
Check & fix styling
Total Downloads

This package can convert any address to GPS coordinates using Google’s geocoding service. Here’s a quick example:

  1. Geocoder::getCoordinatesForAddress('Samberstraat 69, Antwerpen, Belgium');
  2. // will return this array
  3. [
  4. 'lat' => 51.2343564,
  5. 'lng' => 4.4286108,
  6. 'accuracy' => 'ROOFTOP',
  7. 'formatted_address' => 'Samberstraat 69, 2060 Antwerpen, Belgium',
  8. 'viewport' => [
  9. "northeast" => [
  10. "lat" => 51.23570538029149,
  11. "lng" => 4.429959780291502
  12. ],
  13. "southwest" => [
  14. "lat" => 51.2330074197085,
  15. "lng" => 4.427261819708497
  16. ]
  17. ]
  18. ]

Support us

Learn how to create a package like this one, by watching our premium video course:

Laravel Package training

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You’ll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install this package through composer.

  1. composer require spatie/geocoder

Laravel installation

Though the package works fine in non-Laravel projects we included some niceties for our fellow artistans.

In Laravel 5.5 the package will autoregister itself. In older versions of Laravel you must manually install the service provider and facade.

  1. // config/app.php
  2. 'providers' => [
  3. '...',
  4. Spatie\Geocoder\GeocoderServiceProvider::class
  5. ];
  1. // config/app.php
  2. 'aliases' => array(
  3. ...
  4. 'Geocoder' => Spatie\Geocoder\Facades\Geocoder::class,
  5. )

Next, you must publish the config file :

  1. php artisan vendor:publish --provider="Spatie\Geocoder\GeocoderServiceProvider" --tag="config"

This is the content of the config file:

  1. return [
  2. /*
  3. * The api key used when sending Geocoding requests to Google.
  4. */
  5. 'key' => env('GOOGLE_MAPS_GEOCODING_API_KEY', ''),
  6. /*
  7. * The language param used to set response translations for textual data.
  8. *
  9. * More info: https://developers.google.com/maps/faq#languagesupport
  10. */
  11. 'language' => '',
  12. /*
  13. * The region param used to finetune the geocoding process.
  14. *
  15. * More info: https://developers.google.com/maps/documentation/geocoding/intro#RegionCodes
  16. */
  17. 'region' => '',
  18. /*
  19. * The bounds param used to finetune the geocoding process.
  20. *
  21. * More info: https://developers.google.com/maps/documentation/geocoding/intro#Viewports
  22. */
  23. 'bounds' => '',
  24. /*
  25. * The country param used to limit results to a specific country.
  26. *
  27. * More info: https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests
  28. */
  29. 'country' => '',
  30. ];

Usage

Here’s how you can use the Geocoder.

  1. $client = new \GuzzleHttp\Client();
  2. $geocoder = new Geocoder($client);
  3. $geocoder->setApiKey(config('geocoder.key'));
  4. $geocoder->setCountry(config('geocoder.country', 'US'));
  5. $geocoder->getCoordinatesForAddress('Infinite Loop 1, Cupertino');
  6. /*
  7. This function returns an array with keys
  8. "lat" => 37.331741000000001
  9. "lng" => -122.0303329
  10. "accuracy" => "ROOFTOP"
  11. "formatted_address" => "1 Infinite Loop, Cupertino, CA 95014, USA",
  12. "viewport" => [
  13. "northeast" => [
  14. "lat" => 37.3330546802915,
  15. "lng" => -122.0294342197085
  16. ],
  17. "southwest" => [
  18. "lat" => 37.3303567197085,
  19. "lng" => -122.0321321802915
  20. ]
  21. ]
  22. */

You can get the result back in a specific language.

  1. $geocoder->setLanguage('it');
  2. $geocoder->getCoordinatesForAddress('Infinite Loop 1, Cupertino');
  3. /*
  4. This function returns an array with keys
  5. "lat" => 37,3318598
  6. "lng" => -122,0302485
  7. "accuracy" => "ROOFTOP"
  8. "formatted_address" => "Infinite Loop 1, 1 Infinite Loop, Cupertino, CA 95014, Stati Uniti"
  9. ...
  10. */

You can also get all the results instead of the first one

  1. $geocoder
  2. ->getAllCoordinatesForAddress('Infinite Loop 1, Cupertino');
  3. /*
  4. This function returns an array of results (array of array)
  5. ^ array:2 [
  6. 0 => array:7 [
  7. "lat" => 37,3318115
  8. "lng" => -122,0301837
  9. "accuracy" => "ROOFTOP"
  10. "formatted_address" => "1 Infinite Loop, Cupertino, CA 95014, Stati Uniti"
  11. "viewport" => [
  12. "northeast" => [
  13. "lat" => 37.3330546802915,
  14. "lng" => -122.0294342197085
  15. ],
  16. "southwest" => [
  17. "lat" => 37.3303567197085,
  18. "lng" => -122.0321321802915
  19. ]
  20. ]
  21. "place_id" => "ChIJHTRqF7e1j4ARzZ_Fv8VA4Eo"
  22. ]
  23. 1 => array:7 [
  24. "lat" => 37,3318598
  25. "lng" => -122,0302485
  26. "accuracy" => "ROOFTOP"
  27. "formatted_address" => "Infinite Loop 1, 1 Infinite Loop, Cupertino, CA 95014, Stati Uniti"
  28. "viewport" => [
  29. "northeast" => [
  30. "lat" => 37.333046180291
  31. "lng" => -122.02883961971
  32. ],
  33. "southwest" => [
  34. "lat" => 37.330348219708
  35. "lng" => -122.03153758029
  36. ]
  37. ]
  38. "place_id" => "ChIJAf9D3La1j4ARuwKZtGjgMXw"
  39. ]
  40. ]
  41. */

This is how you can reverse geocode coordinates to addresses.

  1. $geocoder->getAddressForCoordinates(40.714224, -73.961452);
  2. /*
  3. This function returns an array with keys
  4. "lat" => 40.7142205
  5. "lng" => -73.9612903
  6. "accuracy" => "ROOFTOP"
  7. "formatted_address" => "277 Bedford Ave, Brooklyn, NY 11211, USA",
  8. "viewport" => [
  9. "northeast" => [
  10. "lat" => 37.3330546802915,
  11. "lng" => -122.0294342197085
  12. ],
  13. "southwest" => [
  14. "lat" => 37.3303567197085,
  15. "lng" => -122.0321321802915
  16. ]
  17. ]
  18. */

You can also reverse geocode coordinates to all the related addresses.

  1. $geocoder->getAllAddressesForCoordinates(40.714224, -73.961452);
  2. /*
  3. This function returns an array of results (array of array)
  4. array:2 [
  5. 0 => array: 7 [
  6. "lat" => 40.7142205
  7. "lng" => -73.9612903
  8. "accuracy" => "ROOFTOP"
  9. "formatted_address" => "277 Bedford Ave, Brooklyn, NY 11211, USA",
  10. "viewport" => [
  11. "northeast" => [
  12. "lat" => 37.3330546802915,
  13. "lng" => -122.0294342197085
  14. ],
  15. "southwest" => [
  16. "lat" => 37.3303567197085,
  17. "lng" => -122.0321321802915
  18. ]
  19. ]
  20. ],
  21. 1 => array: 7 [
  22. "lat" => 40.7142015
  23. "lng" => -73.9613077
  24. "accuracy" => "ROOFTOP"
  25. "formatted_address" => "279 Bedford Ave, Brooklyn, NY 11211, USA",
  26. "viewport" => [
  27. "northeast" => [
  28. "lat" => 40.715557080291,
  29. "lng" => -73.959947169708
  30. ],
  31. "southwest" => [
  32. "lat" => 40.712859119708,
  33. "lng" => -73.962645130291
  34. ]
  35. ]
  36. ]
  37. ]
  38. */

If you are using the package with Laravel, you can simply call getCoordinatesForAddress.

  1. Geocoder::getCoordinatesForAddress('Infinite Loop 1, Cupertino');
  2. /*
  3. This function returns an array with keys
  4. "lat" => 37.331741000000001
  5. "lng" => -122.0303329
  6. "accuracy" => "ROOFTOP"
  7. "formatted_address" => "1 Infinite Loop, Cupertino, CA 95014, Stati Uniti",
  8. "viewport" => [
  9. "northeast" => [
  10. "lat" => 37.3330546802915,
  11. "lng" => -122.0294342197085
  12. ],
  13. "southwest" => [
  14. "lat" => 37.3303567197085,
  15. "lng" => -122.0321321802915
  16. ]
  17. ]
  18. */

The accuracy key can contain these values:

  • ROOFTOP
  • RANGE_INTERPOLATED
  • GEOMETRIC_CENTER
  • APPROXIMATE

You can read more information about these values on the Google Geocoding API Page

When an address is not found accuracy and formatted_address will contain result_not_found

Credits

License

The MIT License (MIT). Please see License File for more information.