Soatl
Soatl

Reputation: 10582

Google map generating incorrectly

I have a program that takes in a zip code and makes a google map. The div that the map is set tohidden until the map is made. Once the map is made the div is set to display : block. The problem is that the first time the map is generated (and only the first time) it looks like this: enter image description here

Once I hit the find a store button again it looks like this:

enter image description here

I have already tried to make a initial call to the map method (which I kept hidden until a real call is made) but this does not fix the issue. I don't want to show all my code (there is a lot) but here is how I make the map.

<div id = "map_canvas" style = " height: 300px; width: 300px;"></div>

//Creates a new center location for the google map
var latlng = new google.maps.LatLng(lat, lng);

//The options for the google map
var mapOptions = {
    zoom: 7,
    maxZoom: 12,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

//Creates the new map
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

Also note that both images below have the correct markers in the correct place.

Any suggestions?

Upvotes: 3

Views: 797

Answers (3)

herostwist
herostwist

Reputation: 3958

this is a common problem. you need to trigger a map redraw after changing the container. most of the time this is caused by showing/hiding the div.

in v3 it's:

google.maps.event.trigger(map, 'resize')

in v2 it's:

map.checkResize()

Upvotes: 6

Toby Sullivan
Toby Sullivan

Reputation: 198

I would suspect that possibly an error is occurring on your first "Find Store" button click that might prevent the map creation code from being reached and executing properly.

Upvotes: 0

Duniyadnd
Duniyadnd

Reputation: 4043

It looks like when you initialize the map, it is completely zoomed out, which brings out the single 250x250 tile that google uses.

Upvotes: 0

Related Questions