Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open google map in new window.

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.

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>
like image 399
KennC. Avatar asked Dec 07 '10 03:12

KennC.


People also ask

How do I change where my Google Maps opens to?

Look to the top-left corner of the map, and click the three horizontal menu bars. From the options, select Your places. Next, click Home. Type the name of the location you want to set as your home address in the address field.

How do I customize the Google Maps window pop up?

You can modify the whole InfoWindow using jquery alone... var popup = new google. maps. InfoWindow({ content:'<p id="hook">Hello World!

How do I add Google Maps to my homepage?

On your Android phone or tablet, go to the widget section. Touch and hold the widget and drop it on your Home screen. At the top, choose a type of transport, like driving, transit, or walking. Enter a destination and shortcut name.

What is Info window in Google map?

An InfoWindow displays content (usually text or images) in a popup window above the map, at a given location. The info window has a content area and a tapered stem. The tip of the stem is attached to a specified location on the map.


3 Answers

  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>.

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


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

like image 122
Matt Ball Avatar answered Nov 07 '22 15:11

Matt Ball


the variable for window.open is the url

<a href="#" onclick="window.open('stackoverflow.com')">New Map(In new window)</a>
like image 21
Timothy Ruhle Avatar answered Nov 07 '22 16:11

Timothy Ruhle


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);

w is 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:

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 div dynamically and use it to create a map.

like image 38
Illuminati Avatar answered Nov 07 '22 16:11

Illuminati