Google Maps API v.3 - 无法显示信息窗口,或者当我这样做时,地图中心位于设计位置
我在使用 Google 地图 API v.3 时遇到了一个小问题。 我从 MySql 数据库获取位置地址,并尝试将其动态发送到 GMap。 这工作正常,但我似乎无法打开“信息窗口”。 如果代码有点混乱,但我最接近的解决方案是打开信息窗口,但地图以定义位置为中心,而不是实际位置。我搜索过不同的网站,但找不到合适的建议。也许我混合了 v.2 和 v.3,但据我所知,似乎并非如此。这是我第一次了解 Google 的 API,所以请耐心等待。 所有代码均来自示例:
var marker;
var geocoder;
var map;
function initialize() {
var mapDiv = document.getElementById('map-canvas');
var latlng = new google.maps.LatLng(42.696492,23.326011);
geocoder = new google.maps.Geocoder();
var address = "Milano 20099";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
// alert("Geocode was not successful for the following reason: " + status);
}
});
var infowindow = new google.maps.InfoWindow({content: '<strong>I will be here!</strong><br/>Milano 20099'});
var myOptions = {
zoom: 9,
center: latlng,
mapTypeId: google.maps.MapTypeId.HYBRID
}
map = new google.maps.Map(mapDiv, myOptions);
infowindow.open(map, marker);
}
提前致谢!
I'm having a small problem with Google Maps' API v.3.
I get the location address from a MySql db and try to send it dynamically to GMaps.
This works OK, but I cannot seem to open a "infowindow".
If messed with the code a little bit but the closest I've got to a solution was the infowindow open but the map centered on the defining position and not the actual one. I've searched through different sites but couldn't find an appropriate advice. Maybe I'm mixing v.2 and v.3 but as far as I can see it doesn't seem to be so. This is my first look at Google's API so please be patient.
All of the code is from examples:
var marker;
var geocoder;
var map;
function initialize() {
var mapDiv = document.getElementById('map-canvas');
var latlng = new google.maps.LatLng(42.696492,23.326011);
geocoder = new google.maps.Geocoder();
var address = "Milano 20099";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
// alert("Geocode was not successful for the following reason: " + status);
}
});
var infowindow = new google.maps.InfoWindow({content: '<strong>I will be here!</strong><br/>Milano 20099'});
var myOptions = {
zoom: 9,
center: latlng,
mapTypeId: google.maps.MapTypeId.HYBRID
}
map = new google.maps.Map(mapDiv, myOptions);
infowindow.open(map, marker);
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将信息窗口打开方法移至地理编码器成功回调函数。地理编码请求可能需要一段时间才能响应,并且您正尝试在地理编码器返回标记位置之前打开信息窗口。所以你的代码应该如下:
You have to move your info window open method to the geocoder success callback function. The geocode request can take a while to respond and you are trying to open an info window before the geocoder returns marker position. So your code should be as follows: