Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

google maps refreshing grey

I'm trying to add a Google Map to a jquery.mobile site javascript and the Google Maps API. The problem is that every time I load the map, most of the map is greyed out. I've done some research and apparently I must add the command:

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

But I've been fiddling with this for the last few hours and I can't seem to make it work. This is the code I'm using:


      var myLatlng = new google.maps.LatLng(34.310774,66.225586);

      var mapOptions = {
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }

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

      // boundry
      var southWest = new google.maps.LatLng(31.207164,61.347656);
      var northEast = new google.maps.LatLng(37.617713,69.785156);
      var bounds = new google.maps.LatLngBounds(southWest,northEast);


      map.fitBounds(bounds);
      google.maps.event.trigger(map, 'resize');
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

To solve that problem you need to:

  1. make sure the div where the map is displayed has a valid size, if it is hidden, it will have zero size and you will need to display the div before it will have a valid size. If it is sized using percentages, make sure that all of its parent elements either have a percent size or a specific size (see Mike Williams' Google Maps API v2 tutorial on the subject for details).

  2. once it has a size initialize the map (if you haven't already)

  3. if it was already initialized, trigger the resize event on it.

  4. You may need to set the center of the map after triggering the resize event.

You haven't provided enough information to see where you are going wrong.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...