Why is the centering of my map off in my jQuery Mobile / Google Maps API3 application?

牧云@^-^@ 提交于 2019-12-18 06:26:06

问题


I have a map powered by Google Maps Javascript API3 in my jQuery Mobile web application. I added a circle with black borders on it.

If you go to this page of my app with the map directly, the map is centered correctly at the center of the circle.

http://goo.gl/XvLST

If you go to this home page of my app first (http://goo.gl/0IWgE) and then click your way into the page with the map, the map is

  1. centered erringly (The map should be centered at the center of the circle).
  2. missing an entire area covered in grey.

Why is the map erring when I click into the page with the map from my home page, but working correctly if I visit the page with the map directly?

I could just not use Ajax to load the map page with data-ajax='false'. However, I cannot actually do this because a mobile application does not allow for non-ajax loads.

google.maps.event.trigger(map, 'resize') seems to reposition the map when I enter it into the Chrome Web Developer console, but I am not sure where to call it in my javascript.


回答1:


Because the page is actully loaded via ajax (when coming from index) - the page event you are using is probably being called before the DOM is totally ready. GMaps expect the page to be ready.

Try using a different event other than pageinit. Looks like pageshow might be a good one (as it runs after any animation has finished) http://jquerymobile.com/demos/1.0/docs/api/events.html

Or maybe you could put the call to the maps resize event, in a pageshow event.




回答2:


Is the page reloading between the home page and the map? If not, and if you're creating or resizing a div, you'll need to call google.maps.event.trigger(map, 'resize') after the div changes (size change or removing a display:none). (edited to show the code to trigger the event, thanks to @Heitor Chang)




回答3:


Call google.maps.event.trigger(mapObj, "resize"); on pageshow or pagechange




回答4:


In my case, my workaround is $('#mapdiv').trigger('create'); working well. but in other cases I don't know it works.



来源:https://stackoverflow.com/questions/9776761/why-is-the-centering-of-my-map-off-in-my-jquery-mobile-google-maps-api3-applic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!