Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

303
Visualizações
Cannot get a circle on the Leaflet Map when user clicks on the button - Blazor

I'm trying to get a circle of certain radius when the user click on the button. My application is in Blazor. I've started with initialise function which works well. However, the GetCircle() function doesn't work and it gives me an exception error 'map.addLayer is not a function' I.

When I also tried to declare the map and circle variable to the top of the code, the error turns to 'Cannot read properties of undefined (reading '_leaflet_id''

Here is my js:

window.myMap = {
    initialise : function(){
var map = L.map('map').setView([51.75164151931502, -1.2382256195107832], 15);

L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
    attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
    maxZoom: 18,
    id: 'mapbox/streets-v11',
    tileSize: 512,
    zoomOffset: -1,
    accessToken: '----'

}).addTo(map);

  var marker = L.marker([51.75262676993951, -1.2533975397416488]).addTo(map);


  map.on('click',function(e) {
    if (marker !== null ) {
        map.removeLayer(marker);
    }
   
    marker = new L.Marker(e.latlng);
    map.addLayer(marker);
    
});
},
GetCircle: function()
{
  //here is the issue

    var circle = L.circle([51.75262676993951,-1.2533975397416488],{radius:300});
   
    map.addLayer(circle);

}


};


this is my Blazor Component:

@page "/testt"
@inject IJSRuntime JSRuntime
@using Microsoft.JSInterop



<style>
    #map {
    height: 600px;
}
       
</style>
<br>
 <div class="col-9" id="map"  ></div>
<button @onclick="GetCircle">
Get Area
</button>

 @code{

     protected override async Task OnAfterRenderAsync(bool firstRender)
{

if (firstRender)
    {
        await JSRuntime.InvokeVoidAsync("myMap.initialise");

        StateHasChanged();
    }
}
      public async Task GetCircle()

    {
        await JSRuntime.InvokeVoidAsync("myMap.GetCircle");
        
    }
}
 

I'm getting 'map.addLayer is not a function' I've tried to put 'addTo(map)' instead of addLayer and the same error came out.

Any hint?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your map variable is not accessible gloabl. You can do following:

var map = L.map('map').setView([51.75164151931502, -1.2382256195107832], 15);
window.map = map;
GetCircle: function()
{
    var circle = L.circle([51.75262676993951,-1.2533975397416488],{radius:300});
    window.map.addLayer(circle);
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda