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>
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.
You can modify the whole InfoWindow using jquery alone... var popup = new google. maps. InfoWindow({ content:'<p id="hook">Hello World!
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.
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.
window.open()
takes a URL as a parameter. Your newWindow()
function does not return anything, let alone a URL.window.open()
with a valid URL passed in, which takes care of setting up the map itself.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?
the variable for window.open is the url
<a href="#" onclick="window.open('stackoverflow.com')">New Map(In new window)</a>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With