你好,我正在尝试用Knockout脚本绑定Google。几乎所有的事情都正常,但我不能强迫infowindows出现在事件中。如果没有Knockout,我的代码可以工作,但是它不能工作。下面是js代码:
var infowindow;
function point(name, lat, long) {
this.name = name;
this.lat = ko.observable(lat);
this.long = ko.observable(long);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, long),
title: name,
map: map,
draggable: true
});
//if you need the poition while dragging
google.maps.event.addListener(marker, 'drag', function () {
var pos = marker.getPosition();
this.lat(pos.lat());
this.long(pos.lng());
}.bind(this));
//if you just need to update it when the user is done dragging
google.maps.event.addListener(marker, 'dragend', function () {
var pos = marker.getPosition();
this.lat(pos.lat());
this.long(pos.lng());
}.bind(this));
google.maps.event.addListener(marker, 'mouseover', function () {
infowindow = new google.maps.InfoWindow({ content: "empty" });
console.log("mouseover");
infowindow.setContent(this.title);
infowindow.open(map, this);
}.bind(this));
}
var map = new google.maps.Map(document.getElementById('googleMap'), {
zoom: 5,
center: new google.maps.LatLng(55, 11),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var viewModel = {
points: ko.observableArray([
new point('Test1', 55, 11),
new point('Test2', 56, 12),
new point('Test3', 57, 13)])
};
function addPoint() {
viewModel.points.push(new point('a', 58, 14));
}
ko.applyBindings(viewModel);现在我的问题是:
这是简单的方法让它发挥作用。如果是的话,谁能建议我到哪里去找呢?
发布于 2013-12-20 15:49:26
可能是你使用this。
添加var self = this;作为point函数中的第一行&使用self引用点内的属性。
在mouseover事件中,这是指标记、地图还是视图模型?如果拖动事件设置的值正确,那么这就是点视图模型,在这种情况下,在您称为this.title的mouseover事件中。没有头衔..。
function point(name, lat, long) {
var self = this;
self.name = name;
self.lat = ko.observable(lat);
self.long = ko.observable(long);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, long),
title: name,
map: map,
draggable: true
});
//if you need the poition while dragging
google.maps.event.addListener(marker, 'drag', function () {
var pos = marker.getPosition();
self.lat(pos.lat());
self.long(pos.lng());
}.bind(this));
//if you just need to update it when the user is done dragging
google.maps.event.addListener(marker, 'dragend', function () {
var pos = marker.getPosition();
self.lat(pos.lat());
self.long(pos.lng());
}.bind(this));
google.maps.event.addListener(marker, 'mouseover', function () {
infowindow = new google.maps.InfoWindow({ content: "empty" });
console.log("mouseover");
infowindow.setContent(marker.title);
infowindow.open(map, this);
}.bind(this));
}发布于 2013-12-20 11:47:02
我本人从未使用过敲除,但将其与地图集成看起来并不简单,以下是一些阅读材料:http://www.codeproject.com/Articles/351298/KnockoutJS-and-Google-Maps-binding http://www.codeproject.com/Articles/387626/BikeInCity-2-KnockoutJS-JQuery-Google-Maps Google maps and knockoutjs
您提供的地图代码看起来是正确的,所以我假设问题在于淘汰赛的集成。
https://stackoverflow.com/questions/20702777
复制相似问题