Using ggogle maps inside a tab

I am attempting to display (and interact with) a google map in a tab follwoing the artcicle here: https://www.syncfusion.com/faq/blazor/general/how-can-i-add-google-maps-to-a-blazor-application

However as the map div is not rendered until the tab is clicked the div is not found by the GetElementById javascript.  Is there a suitable event I can run the map initialize function from?

many thanks

Martin

3 Replies 1 reply marked as answer

HB Hareesh Balasubramanian Syncfusion Team February 19, 2021 02:14 PM UTC

Hi Martin, 

Greetings from Syncfusion Support..! 

We have prepared a sample based on your shared query “render the Google Map inside the Tab component” using Selected event of the Tab component, which can be downloaded from the following link. 


[index.razor]: 
<SfTab> 
    <TabEvents Selected="Selected"></TabEvents> 
    <TabItems> 
        <TabItem> 
            <ChildContent> 
                <TabHeader Text="HTML"></TabHeader> 
            </ChildContent> 
            <ContentTemplate> 
                <div>HyperText Markup Language, commonly referred to as HTML, is the standard markup language used to create web pages. Along with CSS, and JavaScript, HTML is a cornerstone technology, used by most websites to create visually engaging web pages, user interfaces for web applications, and user interfaces for many mobile applications.[1] Web browsers can read HTML files and render them into visible or audible web pages. HTML describes the structure of a website semantically along with cues for presentation, making it a markup language, rather than a programming language.</div> 
            </ContentTemplate> 
        </TabItem> 
        <TabItem> 
            <ChildContent> 
                <TabHeader Text="Java"></TabHeader> 
            </ChildContent> 
            <ContentTemplate> 
                <div>Java is a set of computer software and specifications developed by Sun Microsystems, later acquired by Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers. While less common, Java applets run in secure, sandboxed environments to provide many features of native applications and can be embedded in HTML pages.</div> 
            </ContentTemplate> 
        </TabItem> 
        <TabItem> 
            <ChildContent> 
                <TabHeader Text="Google Maps"></TabHeader> 
            </ChildContent> 
            <ContentTemplate> 
                <div id="map" style="height:500px;width:100%;"></div> 
            </ContentTemplate> 
        </TabItem> 
    </TabItems> 
</SfTab> 
@code{ 
    public async void Selected(SelectEventArgs args) 
    { 
        if (args.SelectedIndex == 2) 
        { 
            await JSRuntime.InvokeVoidAsync("initialize", null); 
        } 
    } 

[script.js]: 
function initialize() { 
    var latlng = new google.maps.LatLng(40.716948, -74.003563); 
    var options = { 
        zoom: 14, center: latlng, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map"), options); 

Kindly try the above solution and get back to us if you need any further assistance. 

Regards, 
Hareesh 


Marked as answer

MA Martin February 19, 2021 06:03 PM UTC

That works just fine - excellent!

Many thanks


NR Nevitha Ravi Syncfusion Team February 22, 2021 05:23 AM UTC

Hi Martin, 

You are most welcome..! We are glad that our provided solution helped at your end. Please get back to us if you need any further assistance. 

Regards, 
Nevitha 


Loader.
Up arrow icon