BlazorGoogleMaps: Display multiple maps on page within child component throws an error

I’m trying to show multiple cards on a page, each with a map within. If I use this code it works fine:

@foreach (int item in Enumerable.Range(1, 8)) {
  <GoogleMap Id="@($"map-{item}")" Height="300px" Options="_mapOptions" />
}

However, if I move the GoogleMap element into a child component so the code then looks like this:

@foreach (Property property in TheClient.Properties) {
  <MapCard Property="property" />
}

And the child component is this:

<div class="card mb-3 round-it overflow-hidden property cursor-pointer">
  <div class="card-img rounded-0 position-relative overflow-hidden" style="height: 150px;">
    <GoogleMap Id="@($"map-{Property.Id}")" Options="_mapOptions" Height=""175px" />
    <div class="position-absolute" style="inset: 0;"></div>
  </div>

  <div class="card-body p-3 pb-2">
    <div>
      <div class="fw-bold address">@Property.Name</div>
    </div>
  </div>
</div>

@code {
  [Parameter]
  public Property Property { get; set; } = default!;
  private MapOptions _mapOptions = default!;

  protected override Task OnInitializedAsync() {
    _mapOptions = new() {
      Zoom = Property != null ? 17 : 5,
      Center = new LatLngLiteral() {
        Lat = Property != null ? Property.Lat : 53.6865494,
        Lng = Property != null ? Property.Lng : -2.1476396
      },
      MapTypeId = MapTypeId.Roadmap
    };

    return InvokeAsync(StateHasChanged);
  }
}

Then I get the following error:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Property 'Map' not found
      Error: Property 'Map' not found
          at stringToFunction (https://localhost:7365/_content/BlazorGoogleMaps/js/objectManager.js:8:23)
          at Object.createObject (https://localhost:7365/_content/BlazorGoogleMaps/js/objectManager.js:374:35)
...

If I open a page that has a single map, and only then open a page with multiple, then no error occurs and it shows all the maps.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (1 by maintainers)

Most upvoted comments

@Nickztar this is perfect thank you. This is OK in my case.

Maybe serve that page from serverside. There are registered couple similar issues here, but so far no one find solution. If i knew what to do i will implement it without complain.