Atma Atma - 2 months ago 7
Javascript Question

google maps initialize can't see map variable when adding marker

I have the following initialization for a google map:

function initialize()
{
var mapOptions = {
zoom: 15,
center: myLatLng,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
},
zoomControl: true,
scaleControl: true,
scrollwheel: true,
disableDoubleClickZoom: true,
};
var map = new google.maps.Map(document.getElementById("googleMap"),
mapOptions);
}

var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});

google.maps.event.addDomListener(window, "load", initialize);
};


When trying to add the marker, I get the error:


Uncaught ReferenceError: map is not defined


How can I see this map or add a marker properly to the map?

Answer

seems you don't have a proper myLatLng position for your marker try adding one eg: myLatLng = new google.maps.LatLng(45.00, 10.00);

function initialize() 
    {
        var mapOptions = {
            zoom: 15,
            center: myLatLng,
            mapTypeControl: true, 
            mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
            },
            zoomControl: true,
            scaleControl: true,
            scrollwheel: true,
            disableDoubleClickZoom: true,
        };
        var map = new google.maps.Map(document.getElementById("googleMap"),
            mapOptions);
        }

    var myLatLng = new google.maps.LatLng(45.00, 10.00);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        title: 'Hello World!'
      });

    google.maps.event.addDomListener(window, "load", initialize);
    };
Comments