places-autocomplete-svelte 0.0.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +204 -1
- package/dist/PlaceAutocomplete.svelte +119 -3999
- package/dist/PlaceAutocomplete.svelte.d.ts +6 -5
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +35 -3
package/README.md
CHANGED
|
@@ -1 +1,204 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Places Autocomplete Svelte
|
|
2
|
+
|
|
3
|
+
This Svelte component leverages the [Google Maps Places Autocomplete API](https://developers.google.com/maps/documentation/javascript/place-autocomplete-overview) to provide a user-friendly way to search for and retrieve detailed address information within your [SvelteKit](https://kit.svelte.dev) applications.
|
|
4
|
+
|
|
5
|
+
## Features:
|
|
6
|
+
- Seamless Integration: Easily integrate the component into your SvelteKit projects.
|
|
7
|
+
- Autocomplete Suggestions: Provide users with real-time suggestions as they type, enhancing the search experience.
|
|
8
|
+
- Detailed Address Retrieval: Retrieve comprehensive address information, including street address, city, region, postal code, and country.
|
|
9
|
+
- Formatted and Unformatted Responses: Access both formatted address strings and raw address component data for flexible use cases.
|
|
10
|
+
- Country Selection: Allow users to specify a region for more targeted results.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+

|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## Requirements
|
|
18
|
+
|
|
19
|
+
Before using this component, you'll need:
|
|
20
|
+
- Google Maps API Key: Create an API key with the Places API (New) enabled. Refer to [Use API Keys](https://developers.google.com/maps/documentation/javascript/get-api-key) for detailed instructions.
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
Using npm:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
$ npm i places-autocomplete-svelte
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
## Import Component
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
import PlaceAutocomplete from 'places-autocomplete-svelte';
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Basic Usage
|
|
39
|
+
|
|
40
|
+
Initiate the component with your public Google Maps API key `PUBLIC_GOOGLE_MAPS_API_KEY` and `formattedAddress` properties. Enter your search query in the search box, then click to select the results. The `formattedAddress` property poulated with the place name and address.
|
|
41
|
+
|
|
42
|
+
The request default `language: 'en-GB'` and `region: 'GB'`, see below to specify different.
|
|
43
|
+
|
|
44
|
+
Use keyboard `up` and `down` key to navigate the suggested results. `esc` will clear the search query.
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
<script>
|
|
48
|
+
import {PlaceAutocomplete} from 'places-autocomplete-svelte';
|
|
49
|
+
|
|
50
|
+
//the business name and address.
|
|
51
|
+
let formattedAddress = '';
|
|
52
|
+
// Your Google Maps Public API Key
|
|
53
|
+
let PUBLIC_GOOGLE_MAPS_API_KEY = '--YOUR_API_KEY--';
|
|
54
|
+
</script>
|
|
55
|
+
|
|
56
|
+
<PlaceAutocomplete bind:PUBLIC_GOOGLE_MAPS_API_KEY bind:formattedAddress/>
|
|
57
|
+
|
|
58
|
+
// Etihad Stadium, Etihad Campus, Manchester M11 3FF
|
|
59
|
+
<p>{formattedAddress}</p>
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
- Replace `'--YOUR_API_KEY--'` with your actual Google Maps API key.
|
|
63
|
+
- The `formattedAddress` variable will be populated with the selected place's formatted address string.
|
|
64
|
+
|
|
65
|
+
## Accessing Address Components
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
<script>
|
|
69
|
+
import {PlaceAutocomplete} from 'places-autocomplete-svelte';
|
|
70
|
+
// formatted address object
|
|
71
|
+
let formattedAddressObj = {
|
|
72
|
+
street_number: '',
|
|
73
|
+
street: '',
|
|
74
|
+
town: '',
|
|
75
|
+
county: '',
|
|
76
|
+
country_iso2: '',
|
|
77
|
+
postcode: ''
|
|
78
|
+
};
|
|
79
|
+
// Your Google Maps Public API Key
|
|
80
|
+
let PUBLIC_GOOGLE_MAPS_API_KEY = '--YOUR_API_KEY--';
|
|
81
|
+
|
|
82
|
+
</script>
|
|
83
|
+
|
|
84
|
+
<PlaceAutocomplete
|
|
85
|
+
bind:PUBLIC_GOOGLE_MAPS_API_KEY
|
|
86
|
+
bind:formattedAddressObj />
|
|
87
|
+
/**
|
|
88
|
+
* Formatted address
|
|
89
|
+
* {
|
|
90
|
+
* "street_number":"Etihad Stadium",
|
|
91
|
+
* "street":"Etihad Campus",
|
|
92
|
+
* "town":"Manchester",
|
|
93
|
+
* "county":"Greater Manchester",
|
|
94
|
+
* "country_iso2":"GB",
|
|
95
|
+
* "postcode":"M11 3FF"
|
|
96
|
+
* }
|
|
97
|
+
*/
|
|
98
|
+
<p>{JSON.stringify(formattedAddressObj)}</p>
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- The `formattedAddressObj` will contain parsed address components, allowing you to access individual elements like street number, town, and postcode.
|
|
103
|
+
|
|
104
|
+
Address Parsing:
|
|
105
|
+
|
|
106
|
+
The Google Mps API `fetchFields()` method retrieves detailed place information. This information is then mapped to the `formattedAddressObj` based on address component types. The following mapping corresponds to the following component types:
|
|
107
|
+
|
|
108
|
+
- `street_number`: longText property of the `street_number`
|
|
109
|
+
- `street`: longText property of the `route`
|
|
110
|
+
- `town`: longText property of the `postal_town`
|
|
111
|
+
- `county`: longText property of the `administrative_area_level_2`
|
|
112
|
+
- `country_iso2`: shortText property of the `country`
|
|
113
|
+
- `postcode`: longText property of the `postal_code`
|
|
114
|
+
|
|
115
|
+
Benefits:
|
|
116
|
+
|
|
117
|
+
By using `formattedAddressObj`, you can easily access individual address components like street number, town, and postcode, simplifying address manipulation in your application.
|
|
118
|
+
|
|
119
|
+
## Specifying Countries
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
<script>
|
|
124
|
+
import {PlaceAutocomplete} from 'places-autocomplete-svelte';
|
|
125
|
+
|
|
126
|
+
//the business name and address.
|
|
127
|
+
let formattedAddress = '';
|
|
128
|
+
// Your Google Maps Public API Key
|
|
129
|
+
let PUBLIC_GOOGLE_MAPS_API_KEY = '--YOUR_API_KEY--';
|
|
130
|
+
// countries
|
|
131
|
+
let countries = [
|
|
132
|
+
{ name: 'United Kingdom', region: 'GB' },
|
|
133
|
+
{ name: 'United States', region: 'US' },
|
|
134
|
+
{ name: 'Switzerland', region: 'CH' },
|
|
135
|
+
{ name: 'Greece', region: 'GR' },
|
|
136
|
+
{ name: 'Russia', region: 'RU' },
|
|
137
|
+
{ name: 'Japan', region: 'JP' }
|
|
138
|
+
];
|
|
139
|
+
</script>
|
|
140
|
+
|
|
141
|
+
<PlaceAutocomplete
|
|
142
|
+
bind:PUBLIC_GOOGLE_MAPS_API_KEY
|
|
143
|
+
bind:formattedAddress
|
|
144
|
+
bind:countries/>
|
|
145
|
+
|
|
146
|
+
// Etihad Stadium, Etihad Campus, Manchester M11 3FF
|
|
147
|
+
<p>{formattedAddress}</p>
|
|
148
|
+
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
- The `countries` array allows users to select a specific region for their search.
|
|
152
|
+
|
|
153
|
+
An optional property `countries` to allow country selection. As per [Google Maps documentation](https://developers.google.com/maps/documentation/javascript/reference/autocomplete-data#AutocompleteRequest):
|
|
154
|
+
|
|
155
|
+
`The region code, specified as a CLDR two-character region code. This affects address formatting, result ranking, and may influence what results are returned. This does not restrict results to the specified region.`
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
## Formatted and Unformatted reponses
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
<script>
|
|
162
|
+
import {PlaceAutocomplete} from 'places-autocomplete-svelte';
|
|
163
|
+
|
|
164
|
+
//the business name and address.
|
|
165
|
+
let formattedAddress = '';
|
|
166
|
+
// full unformatted response
|
|
167
|
+
let fullResponse = [];
|
|
168
|
+
// Your Google Maps Public API Key
|
|
169
|
+
let PUBLIC_GOOGLE_MAPS_API_KEY = '--YOUR_API_KEY--';
|
|
170
|
+
// countries
|
|
171
|
+
let countries = [
|
|
172
|
+
{ name: 'United Kingdom', region: 'GB' },
|
|
173
|
+
{ name: 'United States', region: 'US' },
|
|
174
|
+
{ name: 'Switzerland', region: 'CH' },
|
|
175
|
+
{ name: 'Greece', region: 'GR' },
|
|
176
|
+
{ name: 'Russia', region: 'RU' },
|
|
177
|
+
{ name: 'Japan', region: 'JP' }
|
|
178
|
+
];
|
|
179
|
+
</script>
|
|
180
|
+
|
|
181
|
+
<PlaceAutocomplete
|
|
182
|
+
bind:formattedAddress
|
|
183
|
+
bind:formattedAddressObj
|
|
184
|
+
bind:fullResponse
|
|
185
|
+
bind:PUBLIC_GOOGLE_MAPS_API_KEY
|
|
186
|
+
bind:countries/>
|
|
187
|
+
|
|
188
|
+
```
|
|
189
|
+
Depending on your application requirements you can bind the component properties as needed.
|
|
190
|
+
Below example binds all three responsones:
|
|
191
|
+
- `formattedAddress` - place name and address as string
|
|
192
|
+
- `formattedAddressObj` - populated with the parsed address components
|
|
193
|
+
- `fullResponse` - populated address components response as it retruned from `fetchFilds()` method
|
|
194
|
+
|
|
195
|
+
## Customization:
|
|
196
|
+
|
|
197
|
+
- **Language:** The component defaults to `language: 'en-GB'` and `region: 'GB'`. You can customize these settings as needed.
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
## License
|
|
202
|
+
|
|
203
|
+
[MIT](LICENSE)
|
|
204
|
+
|