Save $100 with our Black Friday deals!

Get $100 OFF with promo code BLACKFRIDAY2024 at checkout until November 30.

See Pricing

Still using Radzen Studio?
Achieve more with Radzen Blazor Studio

Radzen Blazor Studio is our new flagship product and vision of how rapid Blazor application development should be done.

Go to Radzen Blazor Studio

GoogleMap (Blazor)

This article demonstrates how to use the GoogleMap component which is a wrapper for the Google Maps API. Check also the component guide and API reference.

GoogleMap Properties

Name Type Default Description
Name string ‘googlemap’ + index suffix Unique name of the map.
ApiKey string empty string Your Google Maps API key.
Center object with Lat and Lng properties 0, 0 The coordinates of the center of the map.
Markers inline array of marker objects empty The collection of markers displayed on the map declared manually.
Data array of marker objects empty The collection of markers displayed on the map.
Zoom integer 1 Zoom level of the map.

GoogleMap Events

Name Event Argument Description
MapClick position - the lat (latitude) and lng (longtitude) of the clicked location. Fires when the user click on the map.
MarkerClick marker - the clicked marker. Fires when the user click a marker.

Configuration

Radzen allows you to visually set the center and zoom of a map. Click the Configure Google Map button in the property grid. You can also search for the location which you want the map to show.

API Key

The Google Maps API is available as a commercial service. While you can use it for free there is a rate limit. Once you reach that limit the GoogleMap component will display a lower resolution map, a watermark and a warning that the API didn’t load correctly. To avoid that you should get a Google API key and set it via the ApiKey property.

Markers

You can define markers in two ways - by data-binding the Markers property to an existing page property or by adding a marker from the property grid.

Data-binding the Markers property

This approach is useful when the marker locations and other data depend on a database or other API call.

  1. Add a Set action in the Page load event.
  2. Set the Name of the property to markers and set its Value to be an list of RadzenGoogleMapMarker e.g. new List<RadzenGoogleMapMarker>(){ new RadzenGoogleMapMarker(){ Position = new GoogleMapPosition() { Lat = 51.5074, Lng = 0.1278 } } }
  3. Select the map and set Data to ${markers}.

Add markers from the property grid

You can also add markers from the property grid as with other array properties.

  1. Click the plus button.
  2. Set the Lat and Lng properties of the marker. You can also the location picker by clicking the compass button.

Add markers from code

Sometimes you need to add a marker after some user action. For example when the user clicks on the map.

  1. You need to data-bind the Markers property in order to do that.
  2. Handle the MapClick event and use an Execute C# code action. Set Code to ${markers}.Add( new RadzenGoogleMapMarker() { Position = new GoogleMapPosition() { Lat = ${event}.Position.Lat, Lng = ${event}.Position.Lng } }).

Remove markers from code

You can use a similar approach to remove a marker - remove the corresponding item from the property which Markers is data-bound to.

  1. You need to data-bind the Markers property.
  2. Handle the MarkerClick event and use an Execute C# code action. Set Code to ${markers}.Remove(markers.Find(m => m.Position.Lat == ${event}.Position.Lat && m.Position.Lng == ${event}.Position.Lng))

Update existing markers from code

If the markers are data-bound you need to update the corresponding property:

  1. Handle the MapClick event.
  2. Add Execute C# code action. Set code to ${markers}[0] = new RadzenGoogleMapMarker() { Position = new GoogleMapPosition() { Lat = ${event}.Position.Lat, Lng = ${event}.Position.Lng }};

If the markers are defined from the property grid:

  1. Data-bind the Lat and Lng properties of the marker to some page properties created in the Page Load event via Set property actions e.g. lat, lng.
  2. Handle the MapClick event.
  3. Add Execute C# code action. Set code to ${lat} = ${event.Position.Lat}; ${lng} = ${event.Position.Lng}