Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

javascript - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...