我在我的Rails应用中使用谷歌地图,我遇到了一些困难。 我希望在用户点击圈子时显示信息窗口。当他点击标记时它工作正常,但现在我有圆圈它不起作用。
这是我的代码:
markers_json = <%= raw @circles %>;
var map;
var infoWindow;
buildMap = function(){
var mapStyle = [
// https://snazzymaps.com/style/70/unsaturated-browns
{"elementType":"geometry","stylers":[{"hue":"#ff4400"},{"saturation":-68},{"lightness":-4},{"gamma":0.72}]},{"featureType":"road","elementType":"labels.icon"},{"featureType":"landscape.man_made","elementType":"geometry","stylers":[{"hue":"#0077ff"},{"gamma":3.1}]},{"featureType":"water","stylers":[{"hue":"#00ccff"},{"gamma":0.44},{"saturation":-33}]},{"featureType":"poi.park","stylers":[{"hue":"#44ff00"},{"saturation":-23}]},{"featureType":"water","elementType":"labels.text.fill","stylers":[{"hue":"#007fff"},{"gamma":0.77},{"saturation":65},{"lightness":99}]},{"featureType":"water","elementType":"labels.text.stroke","stylers":[{"gamma":0.11},{"weight":5.6},{"saturation":99},{"hue":"#0091ff"},{"lightness":-86}]},{"featureType":"transit.line","elementType":"geometry","stylers":[{"lightness":-48},{"hue":"#ff5e00"},{"gamma":1.2},{"saturation":-23}]},{"featureType":"transit","elementType":"labels.text.stroke","stylers":[{"saturation":-64},{"hue":"#ff9100"},{"lightness":16},{"gamma":0.47},{"weight":2.7}]}
];
handler = Gmaps.build('Google');
map = handler.buildMap({
internal: {id: 'map'},
provider: {
//disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: mapStyle,
scrollwheel: false
}
}, function(){
var circles = handler.addCircles(markers_json, {strokeWeight: 1, strokeColor: '#aaa'});
handler.bounds.extendWith(circles);
handler.fitMapToBounds();
for (var i = 0; i < circles.length; ++i) {
var marker = circles[i];
//google.maps.event.addListener(marker.serviceObject, 'click', onMarkerClick(marker));
addInfoWindow(marker, 'hey');
}
});
};
function addInfoWindow(marker, content) {
var infoWindow = new google.maps.InfoWindow({
content: content
});
google.maps.event.addListener(marker.serviceObject, 'click', function () {
i = infoWindow.open(map, marker.serviceObject);
console.log(i);
});
}
当我尝试打开infowindow时,我一次又一次出现此错误:
Uncaught TypeError: Cannot read property 'get' of undefined(…)
所以我尝试i = infoWindow.open(map.serviceObject, marker.serviceObject);
但现在没有任何反应,我甚至没有错误信息。
我真的不知道我还能做什么......你能帮助我吗?
答案 0 :(得分:1)
infoWindow.open();
的第二个参数必须是具有position
的MVC对象 - 类型为google.maps.LatLng
的属性
google.maps.Marker
确实有position
- 属性,但google.maps.Circle
没有。
使用setOptions
设置信息窗口的map
和position
(例如圆圈的中心):
infoWindow.setOptions({
map: map.serviceObject,
position: marker.serviceObject.getCenter()
});