Extension component utilising Google Maps APIs with Web Awesome Blazor components.
Get the latest package from NuGet
dotnet add package WebAwesomeBlazor.Mapping
Follow install instructions for the main Web Awesome Blazor package.
Register for Google Places (new) API and obtain your API key from Google Cloud Console.
Add the following to your _Imports.razor file:
@using WebAwesomeBlazor.Mapping
@using WebAwesomeBlazor.Mapping.Google
@using WebAwesomeBlazor.Mapping.ServicesAdd the following to your Program.cs:
builder.Services.AddMapsServices("YOUR_API_KEY");Important
Don't store your API key in source code for production applications. Use secure storage mechanisms.
<PlacesAutoComplete @ref="AutoCompleteInput" Label="Search address" OnAddressSelected="AddressSelected" AllowNew="true" NewAddressRequested="NewAddress" />
@code {
private PlacesAutoComplete AutoCompleteInput;
private Task AddressSelected(PlaceResult place)
{
// Handle selected address
return Task.CompletedTask;
}
private Task NewAddress()
{
// Handle new address request
return Task.CompletedTask;
}
}| Property | Type | Default | Description |
|---|---|---|---|
| Style | string | Styles to apply to the autocomplete component | |
| Class | string | Classes to apply to the autocomplete component | |
| PlaceholderText | string | Text to display while the input is empty | |
| AllowNew | bool | false | Allows users to select a "create new" option. Triggers NewAddressRequested |
| Label | string | The label to apply to the input control | |
| Hint | string | Hint text to display below the input | |
| OnAddressSelected | EventCallback | Callback triggered when an address is selected. | |
| NewAddressRequested | EventCallback | Callback triggered when the new address option is selected. |
| Method | Parameters | Description |
|---|---|---|
| SetLocationBias | LocationBias | Sets the location bias for the Google Place API |
| SetDisplayText | Sets the text to display in the input box. Creates a "new" address with the text. |
Location bias can be applied to the PlacesAutoComplete component to help refine search results. Bias can be either a rectangle from longitude and latitude pairs, or a circle from a centre point.
// Set a circular location bias around Melbourne, Australia with a 50km radius
AutoCompleteInput.SetLocationBias(new Services.LocationBias(
new Services.LocationCircle()
{
Latitude = -37.814901,
Longitude = 144.960659,
Radius = 50000
}
));
// Set a rectangular location bias around Melbourne, Australia
AutoCompleteInput.SetLocationBias(new Services.LocationBias(
new Services.LocationRectangle()
{
HighLatitude = -37.7479167,
HighLongitude = 145.4347686,
LowLatitude = -37.813057,
LowLongitude = 145.015198
}
));