简体   繁体   English

您可以在虚拟元素中创建Google地图吗

[英]Can you create a Google map in a virtual element

I am wondering it is possible to create a working Google map in a virtual element which can then be pasted into any page at any time. 我想知道是否有可能在虚拟元素中创建可工作的Google地图,然后将其随时粘贴到任何页面中。

So here is my JS when targeting an element which already exists on the page. 所以这是我针对页面上已经存在的元素时的JS。

var mapLatLong = new google.maps.LatLng(51.5174456, -0.1305081);
var markerLatlng = new google.maps.LatLng(51.5170951, -0.1367416);

var mapOptions = {
    zoom: 16,
    center: mapLatLong,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}

var map = new google.maps.Map($('#map_canvas'), mapOptions);

var contentString = '<div id="content">'+
                        '<div id="siteNotice">'+
                        '</div>'+
                        '<h1 id="firstHeading" class="firstHeading">BrocklebankPenn</h1>'+
                        '<div id="bodyContent">'+
                            '<p>5th floor, 58-60 Berners Street,<br />London,<br />W1T 3NQ,<br />United Kingdom</p>'+
                            '<p>+44 (0)20 3137 7034</p>'+
                        '</div>'+
                    '</div>';

var infowindow = new google.maps.InfoWindow({
    content: contentString
});

var marker = new google.maps.Marker({
    position: markerLatlng,
    map: map,
    title: 'BrocklebankPenn'
});

google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
});

infowindow.open(map,marker);

What I want to do is something like this: 我想做的是这样的:

this.mapCanvas = document.createElement('div');
this.mapCanvas.setAttribute('id', 'map_canvas');

var mapLatLong = new google.maps.LatLng(51.5174456, -0.1305081);
var markerLatlng = new google.maps.LatLng(51.5170951, -0.1367416);

var mapOptions = {
    zoom: 16,
    center: mapLatLong,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}

var map = new google.maps.Map($(this.mapCanvas), mapOptions);

var contentString = '<div id="content">'+
                        '<div id="siteNotice">'+
                        '</div>'+
                        '<h1 id="firstHeading" class="firstHeading">BrocklebankPenn</h1>'+
                        '<div id="bodyContent">'+
                            '<p>5th floor, 58-60 Berners Street,<br />London,<br />W1T 3NQ,<br />United Kingdom</p>'+
                            '<p>+44 (0)20 3137 7034</p>'+
                        '</div>'+
                    '</div>';

var infowindow = new google.maps.InfoWindow({
    content: contentString
});

var marker = new google.maps.Marker({
    position: markerLatlng,
    map: map,
    title: 'BrocklebankPenn'
});

google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
});

infowindow.open(map,marker);

this.$el.html(this.mapCanvas);

Basically the exact same thing but with a variable instead of #map_canvas , I have taken this approach with JS in the past but it does not seem to be working for Google maps, I get the following error: 基本上完全相同,但是使用variable而不是#map_canvas ,我过去在JS上采用了这种方法,但是对于Google地图似乎不起作用,出现以下错误:

Uncaught TypeError: Cannot set property 'position' of undefined 

Which I am not sure of. 我不确定。

If anyone can help that would be awesome. 如果有人可以帮助,那就太好了。

Cheers, Luke. 干杯,卢克。

Basically it's possible, Demo: http://jsfiddle.net/doktormolle/5SjnK/ 基本上有可能,演示: http : //jsfiddle.net/doktormolle/5SjnK/

The issues with your code: 您的代码存在的问题:

var map = new google.maps.Map($(this.mapCanvas), mapOptions);

$(this.mapCanvas) is a jQuery-object, but it's expected to be an HTML-Element $(this.mapCanvas)是一个jQuery对象,但是应该是HTML元素

this.$el.html(this.mapCanvas);

Don't set the html, you must insert the original node into the document. 不要设置html,您必须将原始节点插入文档中。

But I wouldn't suggest it, there will be more issues, eg when you use API-methods that try to access the parentNode of the map-div or methods that need to calculate positions inside the document(both will fail before the node has been inserted into the document). 但我不建议这样做,会有更多问题,例如,当您使用尝试访问map-div的parentNode的API方法或需要计算文档内位置的方法时(两者都会在节点具有已插入文档中)。

Related to pasted into any page : 粘贴到任何页面相关

It's not possible, the document where you use the map must be the same document where you load the API, the API internally uses references to the window/document where the API has been loaded. 不可能,使用地图的文档必须与加载API的文档相同,API在内部使用对已加载API的窗口/文档的引用。 See this question: Marker drag not working in iframe under Chrome and Firefox 看到这个问题: 标记拖动在Chrome和Firefox下的iframe中不起作用

But as long as any page doesn't mean "any document"(regarding to your description it's always the same document) it's possible. 但是只要任何页面都不意味着“任何文档”(就您的描述而言,它始终是同一文档)就可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM