Download the PHP package swixpop/locate without Composer
On this page you can find all versions of the php package swixpop/locate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download swixpop/locate
More information about swixpop/locate
Files in swixpop/locate
Package locate
Short Description Harness the power of the Google Autocomplete API inside Craft. Adds an autocomplete search box to Craft entries.
License MIT
Informations about the package locate
Locate plugin for Craft CMS 3.x
Harness the power of the Google Autocomplete API inside Craft. Adds an autocomplete search box to Craft entries which allows place and address queries to be made to the API. Populates a hidden Location field with lat
, lng
, location
, and placeid
which you can grab in your templates to do with as you wish.
Requirements
- Craft CMS 3.0.0-beta.23 or later.
- Google API key with
Google Maps JavaScript API
andGoogle Places API Web Service
enabled.
Note:
Google has updated their API access policy. See https://cloud.google.com/maps-platform/user-guide/
Installation
To install the plugin, follow these instructions.
-
Open your terminal and go to your Craft project:
cd /path/to/project
-
Then tell Composer to load the plugin:
composer require swixpop/locate
-
In the Control Panel, go to Settings → Plugins and click the “Install” button for Locate, or from the command line:
./craft install/plugin locate
-
Configure the plugin via the plugin settings page in the control panel.
- You can now create
Location
fields and add them to your entries.
Configuration
Navigate to Settings > Plugins > Locate. Enter your Google API key if you already have one, or get one here. The settings page also allows you to customize the behaviour of the autocomplete box.
Customize the autocomplete box
The autocomplete search box need not be customized. It defaults to all place types (cities, addresses, businesses, etc.) in the world. By default, the API will attempt to detect the user's location from their IP address, and will bias the results to that location.
However, by passing in some basic options, advanced filtering can be achieved. You can modify the default behaviour by passing in a JSON object of options. For a full list of allowed options see the official documentation from Google.
These options can be set globally or on a per field basis. Options set on individual fields will override the global options.
The options object must be formatted correctly or the plugin will throw a javascript error! If after reading this documentation you are unclear about what to enter in the options box, please leave it blank.
Restrict search by place type
You can specify an array of types
to restrict the results returned to your autocomplete box. In general only a single type is allowed. Possible values are:
geocode
address
establishment
(regions)
(cities)
See the official documentation for details on how using these types
will restrict your results.
Example usage
Restrict the results to cities:
Restrict the results to business establishments:
Bias the search towards a geographical already
You can use the bounds
property to specify a google.maps.LatLngBoundsLiteral
and bias your search results to a geographic area. This is an object specifying north
, east
, south
, and west
values. The autocomplete box return results biased towards, but not restricted to, the area you specify.
See the LatLngBoundsLiteral object specification for full details.
Note that the values are entered as a number and not a string
Example usage
Bias the search results to the Pacific Northwest:
Restrict search by country
You can restrict results to an individual country using the componentRestrictions
property. The country must be passed as as a two-character, ISO 3166-1 Alpha-2 compatible country code.
Example usage
Restrict the results to Canada:
Putting it all together
Using the above examples we can create an options object that restricts searches to businesses in Canada, preferably in the Pacific Northwest region (ie. Starbucks in Vancouver).
Using the Location field
The Location field returns locationData
, lat
, lng
, location
, and placeid
. You can use these in your templates and pass them on to your javascript.
locationData
returns the full API response from Google, with an additionalcomponents
array which maps theaddress_components
to an an array with the component types as keys. See here for a list of some types. (For example, you can accesslocationData.components.country
in your twig. You can also add_short
to any component key to access theshort_name
of that address component, for examplelocationData.components.country_short
).lat
returns the latitude of the placelng
returns the longitude of the placelocation
returns autocomplete queryplaceid
returns a textual identifier that uniquely identifies a place
The placeid
can be used to make additional requests to the Google Maps API. With placeid
you can make place details requests to get information such as address, phone number, reviews, etc. You can also use the placeid
to generate map markers. Read more about referencing a place with a place ID.
Templating examples
Using the options restrictions from the previous section, we could search for "Lucky's Donuts" in the autocomplete box.
would generate the following:
You could then generate a simple map by loading up the Google Maps javascript API:
and grabbing the placeid
, lat
, and lng
in your javascript:
Brought to you by Isaac Gray