Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:10:58+00:00 2026-06-14T14:10:58+00:00

I am trying to implement some location-based services features on my MVC 4 web

  • 0

I am trying to implement some location-based services features on my MVC 4 web page. I am completely new to using Javascript and MVC so please bear with me.

This is my current web page:

@{
    ViewBag.Title = "OnlineUsers";
}


<html>
<head>
    <title>Online Users</title>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.4/leaflet.css" />
    <script src="http://cdn.leafletjs.com/leaflet-0.4/leaflet.js"></script>

</head>
<body>
    <button onclick="showMap()">Show Map</button>
    <button onclick="goToLocation()">My Location</button>
    <div id="locationDetails"></div>
    <div id="map" style="height: 500px; width: 800px"></div>

    <script type="text/javascript">
        **var map = L.map('map');**
        function showMap() {

            var map = L.map('map');
            L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', {
                maxZoom: 18,
                attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>'
            }).addTo(map);

            map.locate({ setView: true, maxZoom: 16 });
        }

    </script>
</body>
</html> 

At the top of the script section is the following piece of code: var map = L.map('map');. If I include this code within a function, the page loads and calling the function creates the map. However, if I place this code (or any other JS code) outside of a function, the page simply does not load.

I used Chrome’s debugger and the console showed the following error:

Uncaught ReferenceError: L is not defined 

I don’t see ‘L’ being defined anywhere, but then why does this work when called from within the function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:10:59+00:00Added an answer on June 14, 2026 at 2:10 pm

    It works within your function because L is a part of leaflet, which is not loaded by the time your function is called but not on page load. L is minified representation of something else.

    It would be usual to wait until document is ready, so you can be sure that your referenced js has loaded, e.g. if you have jQuery then the following

    $(document).ready(function () {
        var map = L.map('map');
    }
    

    UPDATE: it may be slow to load this js, so you could put a check in that it is there first, e.g.

    window.setTimeout(initMap, 100); 
    
    function initMap(){
             //this should check if your leaflet is available or wait if not. 
             if(typeof L=== "undefined"){
                    window.setTimeout(initMap, 100);
                    return;
             }
             var map = L.map('map');
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement some code on my web page to auto-scroll after
I'm trying to implement some custom model metadata in ASP.NET MVC 3. I can't
I am trying to implement some kind of new Linux task scheduler so I
I am trying to implement some interface changes in my app, based on the
I'm trying to create a new type in Clojure using deftype to implement a
I started to implement some new html5 features (standards, nothing fancy) in my project.
I'm trying to implement PDF functionality to my application. So, I added some new
I trying to implement some relatively simple 2D sprite batching in OpenGL ES 2.0
im trying to implement some behaviors when a mapview element scrolls... by coding a
I'm currently trying to implement some kind of search system in a program of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.