Plotting additional points on mapquest API map results in "Uncaught TypeError: t.addLayer is not a function"

70 Views Asked by At

I am trying to plot locations on a map using the Mapquest API. When the page is loaded, the first script generated the map, and places it in a div, which works properly.

When I call the function "plotPoint()" to add another point on the map, I get the following error: "Uncaught TypeError: t.addLayer is not a function"

<script src="https://api.mqcdn.com/sdk/mapquest-js/v1.3.2/mapquest.js"></script>

<script type="text/javascript">
    L.mapquest.key = 'KEY';
    var map;

      window.onload = function() {

        var map = L.mapquest.map('map', {
          center: [23.1596297, -82.2116596],
          layers: L.mapquest.tileLayer('map'),
          zoom: 8
        });

        L.marker([23.1596297, -82.2116596], {
          icon: L.mapquest.icons.marker(),
          draggable: false
        }).bindPopup('LOCATION 1').addTo(map);

        
      };

function plotPoint(){
        L.marker([23.1796297, -82.2416596], {
          icon: L.mapquest.icons.marker(),
          draggable: false
        }).bindPopup('LOCATION 2').addTo(map);
}
</script>

Any insight or suggestion would be most appreciated.

1

There are 1 best solutions below

0
Nebo On

map was declared as a local variable, when it should have been globally.