这一点.setValues不是一个函数谷歌地图API 3
this.setValues is not a function google maps API 3
我知道这个问题以前已经问过了,我已经遵循了一些答案,但似乎没有一个工作。最接近的是这个
地图标记不显示(JavaScript/Google Maps API V3)
但在这里我不知道他想做什么,尽管如此,我认为我的情况是不同的,它对我不起作用。所以,如果有人有任何建议,请告诉我。
function initialize() {
var mapOptions = {
zoom: 16,
center: new google.maps.LatLng(52.5498783, 13.425209099999961),
mapTypeId: google.maps.MapTypeId.SATELLITE
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
overlay = new DraggableOverlay(map,
map.getCenter(),
'<div class="overlay">drag me!</div>'
);
}
DraggableOverlay.prototype = new google.maps.OverlayView();
DraggableOverlay.prototype.onAdd = function () {
var container = document.createElement('div'),
that = this;
if (typeof this.get('content').nodeName !== 'undefined') {
container.appendChild(this.get('content'));
}
else {
if (typeof this.get('content') === 'string') {
container.innerHTML = this.get('content');
}
else {
return;
}
}
container.style.position = 'absolute';
container.draggable = true;
google.maps.event.addDomListener(this.get('map').getDiv(),
'mouseleave',
function () {
google.maps.event.trigger(container, 'mouseup');
}
);
google.maps.event.addDomListener(container,
'mousedown',
function (e) {
this.style.cursor = 'move';
that.map.set('draggable', false);
that.set('origin', e);
that.moveHandler = google.maps.event.addDomListener(that.get('map').getDiv(),
'mousemove',
function (e) {
var origin = that.get('origin'),
left = origin.clientX - e.clientX,
top = origin.clientY - e.clientY,
pos = that.getProjection()
.fromLatLngToDivPixel(that.get('position')),
latLng = that.getProjection()
.fromDivPixelToLatLng(new google.maps.Point(pos.x - left,
pos.y - top));
that.set('origin', e);
that.set('position', latLng);
that.draw();
});
}
);
google.maps.event.addDomListener(container, 'mouseup', function () {
that.map.set('draggable', true);
this.style.cursor = 'default';
google.maps.event.removeListener(that.moveHandler);
});
this.set('container', container)
this.getPanes().floatPane.appendChild(container);
};
function DraggableOverlay(map, position, content) {
if (typeof draw === 'function') {
this.draw = draw;
}
this.setValues({
position: position,
container: null,
content: content,
map: map
});
}
DraggableOverlay.prototype.draw = function () {
var pos = this.getProjection().fromLatLngToDivPixel(this.get('position'));
this.get('container').style.left = pos.x + 'px';
this.get('container').style.top = pos.y + 'px';
};
DraggableOverlay.prototype.onRemove = function () {
this.get('container').parentNode.removeChild(this.get('container'));
this.set('container', null)
};
google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
.overlay{
background:yellow;
padding:30px;
border:4px double black;
}
<div id="map-canvas"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD7FTNE22Wl6S6DTQF83sTZTqbFFPzEkmU&libraries=drawing,places,geometry&callback=initialize">
</script>
你可以看到这里有两个问题
- Google未定义
- 。setValues不是一个函数
所以如果有人遇到这个问题请告诉我
google is not defined
:如果您异步加载API,则需要将所有依赖于回调函数内API的代码(在您的情况下为initialize
)。任何依赖于API的东西在API被加载之前都无法工作。您需要将DraggableOverlay
的定义移动到initialize
中,或者同步加载API。
下面的例子没有异步加载的API(像文档中的例子和Dr.Molle的回答,它看起来像DraggableOverlay
来自:我们可以使自定义覆盖在谷歌地图V3上可拖动)
function initialize() {
var mapOptions = {
zoom: 16,
center: new google.maps.LatLng(52.5498783, 13.425209099999961),
mapTypeId: google.maps.MapTypeId.SATELLITE
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
overlay = new DraggableOverlay(map,
map.getCenter(),
'<div class="overlay">drag me!</div>'
);
}
DraggableOverlay.prototype = new google.maps.OverlayView();
DraggableOverlay.prototype.onAdd = function() {
var container = document.createElement('div'),
that = this;
if (typeof this.get('content').nodeName !== 'undefined') {
container.appendChild(this.get('content'));
} else {
if (typeof this.get('content') === 'string') {
container.innerHTML = this.get('content');
} else {
return;
}
}
container.style.position = 'absolute';
container.draggable = true;
google.maps.event.addDomListener(this.get('map').getDiv(),
'mouseleave',
function() {
google.maps.event.trigger(container, 'mouseup');
}
);
google.maps.event.addDomListener(container,
'mousedown',
function(e) {
this.style.cursor = 'move';
that.map.set('draggable', false);
that.set('origin', e);
that.moveHandler = google.maps.event.addDomListener(that.get('map').getDiv(),
'mousemove',
function(e) {
var origin = that.get('origin'),
left = origin.clientX - e.clientX,
top = origin.clientY - e.clientY,
pos = that.getProjection()
.fromLatLngToDivPixel(that.get('position')),
latLng = that.getProjection()
.fromDivPixelToLatLng(new google.maps.Point(pos.x - left,
pos.y - top));
that.set('origin', e);
that.set('position', latLng);
that.draw();
});
}
);
google.maps.event.addDomListener(container, 'mouseup', function() {
that.map.set('draggable', true);
this.style.cursor = 'default';
google.maps.event.removeListener(that.moveHandler);
});
this.set('container', container)
this.getPanes().floatPane.appendChild(container);
};
function DraggableOverlay(map, position, content) {
if (typeof draw === 'function') {
this.draw = draw;
}
this.setValues({
position: position,
container: null,
content: content,
map: map
});
};
DraggableOverlay.prototype.draw = function() {
var pos = this.getProjection().fromLatLngToDivPixel(this.get('position'));
this.get('container').style.left = pos.x + 'px';
this.get('container').style.top = pos.y + 'px';
};
DraggableOverlay.prototype.onRemove = function() {
this.get('container').parentNode.removeChild(this.get('container'));
this.set('container', null)
};
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
.overlay {
background: yellow;
padding: 30px;
border: 4px double black;
}
<div id="map-canvas"></div>
<script src="https://maps.googleapis.com/maps/api/js?libraries=drawing,places,geometry">
</script>
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 我可以在json对象中添加一个函数吗
- Javascript使函数一个接一个地执行
- 是JavaScript中的函数一个对象
- 两个几乎相等的jQuery函数;一个适用于IE,一个不适用于IE
- 两个Javascript函数一个window.onload=Custom.init;和一个window.onload=f
- 使javascript加载函数一个接一个地执行
- 如何通过两个嵌套的匿名函数(一个带有超时的事件处理程序)传递变量
- 为什么我不能让两个jQuery函数一个在另一个里面呢?
- 给函数一个变量名,而不是它的值
- 如何使用嵌入函数(一个滚动页由Pete - peachananr)
- 是否有可能给JavaScript函数一个类型/类?
- 确保两个函数一个接一个地执行,其中第一个函数内部有一个异步调用
- 两个不同的ajax函数一个接一个调用返回相同的值
- jQuery第一次更改函数一个工作
- 给这个函数一个id
- 如何给回调函数一个值一个变量在特定时刻
- 使用回调使两个函数一个接一个地运行
- 两个函数(一个用php)是否可以用“;onclick”;
- 两个几乎相同的函数.一个函数用Queue进行排队.Jquery效果在queued函数上不起作用.为什么