Moumou
Moumou

Reputation: 1532

Google maps api(v3) doesn't show markers

I am working with google maps api(v3) and i have to add markers on a map. Actually, I had no problem with displaying them or whatever but after a while the markers started not to be displayed or more precisly, they are displayed from time to time.

Here is some code so you can see what i'm doing and where the problem could come from:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <link rel="stylesheet" href="style.css" type="text/css" media="screen" />

        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=[KEY]&sensor=true"></script>
        <script type="text/javascript" src="google.js"></script>
        <script type="text/javascript" src="dataLoading.js"></script>
    </head>
    <body onload="initializeMaps();">
        <div id="map_canvas"></div>
        <script type="text/javascript">
            loadSpots();
        </script>
    </body>
</html>

And here is the javascript code:

var map;
var yMarker = 'http://maps.google.com/mapfiles/ms/icons/yellow-dot.png';
var gMarker = 'http://maps.google.com/mapfiles/ms/icons/green-dot.png';
var bMarker = 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png';
var pMarker = 'http://maps.google.com/mapfiles/ms/icons/pink-dot.png';

function initializeMaps()
{
    geocoder = new google.maps.Geocoder();
var mapOptions =
{
    center: new google.maps.LatLng(46.52863469527167, 2.4389648437),
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
    mapOptions);
}

function getLatlng(address, name, type)
{
geocoder.geocode({'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK)
    {
        var marker = new google.maps.Marker({
            title: name,
            map: map,
            animation: google.maps.Animation.DROP,
            position: results[0].geometry.location
        });
        marker.setIcon((type == 2 ? gMarker : (type == 3 ? bMarker : (type == 4 ? pMarker : yMarker))));
    }
});
}

function addMarker(address, name, type)
{
if (address != null && address != "")
    getLatlng(address, name, type);
}

EDIT1

LoadSpots() is a function that gets data from DB with an ajax request and then calls addMarker function

Upvotes: 0

Views: 2084

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117354

Call loadSpots() at the end of initializeMaps(), otherwise it may happen that the map will be accessed before it's initialized.

Upvotes: 1

Related Questions