0

I read google doc on markers and tried to integrate the sample code into mine. But it doesn't work. The markers don't display. What's wrong with my code? Thank you!

function addMarker(place)
{
    // TODO


        var myLatLng = {lat: parseFloat(place.latitude), lng: parseFloat(place.longitude)};

        var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 4,
        center: myLatLng
        });

        var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        title: place.admin_name2 + ", " + place.admin_name1
        });

        markers.push(marker);

}

2 Answers 2

0

In your code you have overridden the map variable. So when you add a marker with map : map option it is the map inside the function addMarker that gets the marker but that map is displayed only for a fraction of second. Then when the interpreter returns from addMarker you get to see again the previous map which doesn't have any markers.

So you need to just remove all the map code from this function then hopefully everything will be fine.

Please accept this answer if you find it useful.

0

You are instantiating a new (local) map object in the function and then adding the markers to it. All things being equal, the rest of the program is operating on the "global" map instantiated in the supplied code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .