javascript 在新窗口中打开谷歌地图。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4373212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:12:17  来源:igfitidea点击:

Open google map in new window.

javascripthtmlfunctiongoogle-maps

提问by KennC.

I have created a Google Map API and I would like to open it in a new tab(Window). May I know what's wrong with my code? I can open a new tab, but I can't display the Google Map.

我已经创建了一个谷歌地图 API,我想在一个新选项卡(窗口)中打开它。我可以知道我的代码有什么问题吗?我可以打开一个新选项卡,但无法显示 Google 地图。

Below are my code. Thanks!

下面是我的代码。谢谢!

    function newWindow() 
    { 
     var myLatlng = new google.maps.LatLng(0.7,40);
     var myOptions = 
        {
         zoom: 2,
         center: myLatlng,
         mapTypeId: google.maps.MapTypeId.HYBRID
        };
    map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);
    } 

<A HREF="" onclick="window.open('javascript:newWindow()')" >New Map(In new window)</A>

采纳答案by Matt Ball

  1. window.open()takes a URL as a parameter. Your newWindow()function does not return anything, let alone a URL.
  2. You need to call window.open()with a valid URL passed in, which takes care of setting up the map itself.
  3. If you're going to attach an event handler inline, do it right:

    <a onclick="window.open('some_url_here'); return false;">...</a>.

  1. window.open()将 URL 作为参数。您的newWindow()函数不返回任何内容,更不用说 URL。
  2. 您需要window.open()使用传入的有效 URL进行调用,该 URL 负责设置地图本身。
  3. 如果您要内联附加事件处理程序,请正确执行:

    <a onclick="window.open('some_url_here'); return false;">...</a>.

That said, in the interest of unobtrusive JavaScript, you should really attach JS event handlers using your external JS code.

也就是说,为了不引人注目的 JavaScript,您应该真正使用外部 JS 代码附加 JS 事件处理程序。



Perhaps you want to open your map in a modal dialog instead?

也许您想在模态对话框中打开地图?

回答by Timothy Ruhle

the variable for window.open is the url

window.open 的变量是 url

<a href="#" onclick="window.open('stackoverflow.com')">New Map(In new window)</a>

回答by Illuminati

There is actually a solution for your problem. The first error here is that the context in new window is different from the old window.

实际上有一个解决方案可以解决您的问题。这里的第一个错误是新窗口中的上下文与旧窗口不同。

var w = window.open('', '_blank', options);

wis a Window object different from the window. Empty URL create an empty page "about:blank", and since there is no domain, you have read/write access to w.document. So something like this:

w是一个不同于window的 Window 对象。空 URL 创建一个空页面“about:blank”,并且由于没有域,您对w.document具有读/写访问权限。所以像这样:

function newWindowMap(latitude, longitude) {
    var w = window.open('', '_blank'); //you must use predefined window name here for IE.
    var head = w.document.getElementsByTagName('head')[0];
    //Give some information about the map:
    w.document.createElement('input');
    //Place ID, value and type='hidden' here
    var loadScript = w.document.createElement('script');
    loadScript.src = '...'; //Link to script that load google maps from hidden elements.
    var googleMapScript = w.document.createElement('script');
    googleMapScript.src = '...'; //Link to google maps js, use callback=... URL parameter to setup the calling function after google maps load.
    head.appendChild(loadScript);
    head.appendChild(googleMapScript);
}

Now the whole loadScript will be in context of the new window and google map will call a function from it, when it finished loading. You can create new divdynamically and use it to create a map.

现在整个 loadScript 将在新窗口的上下文中,谷歌地图将在加载完成后从中调用一个函数。您可以动态创建新div并使用它来创建地图。