Google 地图 V3 未居中,仅显示地图的一部分
我正在为 iPhone 开发一个基于 jQTouch 的应用程序,其中一部分使用 Google Maps API (V3)。我希望能够将地理位置坐标传递到地图,并让它用标记将位置居中。我现在得到的是适当缩放级别的地图,但所需的中心点出现在右上角。它还仅显示大约三分之一的地图区域(其余部分为灰色),并且当您平移或缩放时,它的行为有些不稳定。这是代码:
var coords = { latitude : "35.510630", longitude : "-79.255374" };
var latlng = new google.maps.LatLng(coords.latitude, coords.longitude);
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map($("#map_canvas").get(0), myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
});
顺便说一句:它在其他平台/浏览器上的外观和行为也相同。
想法?
预先感谢,
马克
补充 这是一个链接,可以准确显示正在发生的情况: iPhone 模拟器的屏幕截图
I'm developing a jQTouch-based app for the iPhone and part of it uses the Google Maps API (V3). I want to be able to pass the geolocation coordinates to the map and have it center the location with a marker. What I'm getting now is the map at the proper zoom level but the desired center-point appears in the upper-righthand corner. It's also showing only about a third of the map area (the rest is gray) and it behaves somewhat erratically when you pan or zoom. Here's the code:
var coords = { latitude : "35.510630", longitude : "-79.255374" };
var latlng = new google.maps.LatLng(coords.latitude, coords.longitude);
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map($("#map_canvas").get(0), myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
});
BTW: It looks and behaves the same on other platforms/browsers as well.
Thoughts?
Thanks in advance,
Mark
Added
Here's a link that'll show exactly what's happening:
Screen shot of iPhone emulator
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我猜您正在使用 AJAX 来显示地图,或者在某个时刻隐藏地图,然后根据操作显示它?
在这种情况下,Google 不知道地图容器的大小,它只会绘制地图的一部分,通常是偏离中心的。
要解决此问题,请调整地图大小,然后重新以纬度/经度为中心。像这样的事情:
I'm guessing you're using AJAX to display the map, or have the map hidden at some point and then display it based on an action?
In this case Google doesn't know the size of the map container and it will draw only part of the map, usually off-centered.
To fix this, resize the map, then re-center on your lat/lng. Something like this:
正确渲染地图的解决方案是在 jQTouch 的 pageAnimationEnd-Event 上执行地图代码。
Page #div 中的示例:
不过,我发现自己在使地图全屏高度方面遇到困难。在这种情况下的任何帮助将不胜感激。请注意,我使用 DataZombies 栏扩展来固定页脚导航。
The solution for getting the map renedered correctly is to execute your Maps-Code on pageAnimationEnd-Event of jQTouch.
Example within Page #div:
Though, i find myself having trouble to make the Map fullscreen-height. Any help in this case is appreciated. Note that im using DataZombies bars extension for fixed footer navigation.
请参阅我对 Google Maps API 3 & 的回答jQTouch
基本上,您的 #map_canvas div 在构建地图时不可见,因为它所在的 div 已被 jqtouch 隐藏。 api 无法处理这个问题并且得到的大小是错误的。
如果 gmaps v3 允许在构造函数中设置显式大小(就像 v2 那样),将会容易得多。
不管怎样,延迟构建地图直到“pageAnimationEnd”。
see my answer on Google Maps API 3 & jQTouch
basically, your #map_canvas div is not visible at the time you're constructing the map as the div it's in has been hidden by jqtouch. the api can't deal with this and get's the size wrong.
would be much easier if gmaps v3 allowed setting explicit size in the constructor (as v2 did).
anyway, delay constucting map until 'pageAnimationEnd'.
确保您已绝对设置画布的宽度/高度(即)
当然,假设您已启动并运行 jqtouch 工具栏...
Make sure you have set the width/height of the canvas absolutely (i.e.)
Of course that assumes you have the jqtouch toolbar up and running...
在使用 AJAX 加载内容并显示和隐藏地图画布 div 后,加载带有方向的地图时遇到同样的问题。
您必须触发调整大小事件,就像 Brett DeWoody 所说的那样,但我发现我的全局变量超出了范围,我必须引用它们,即 google 地图变量,在窗口对象上显式地在我的函数中引用它们。这是我的 directionsService 响应中的内容:
Had the same exact issue loading a map with directions after loading content with AJAX and showing and hiding the map-canvas div.
You have to trigger the resize event, like Brett DeWoody says, but I found that my global variable went out of scope and I had to refer to them, i.e. the google map variable, INSIDE my functions explicitly on the window object. Here's what I have inside my directionsService response:
Brett DeWoody 的解决方案有效,但当我以模式显示地图时,对我来说唯一的解决方案是添加 setTimeout,如下所示:
添加 setTimeout 后,地图将完美呈现和居中。
Brett DeWoody's solution worked, but as I'm showing the map in modal the only solution for me was to add setTimeout like this:
After adding setTimeout the map renders and centers perfectly.