23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

That said, let’s put map.html in an html folder. Right-click the project and select Add/New Folder<br />

(entering html to name it). Then right-click that folder, select Add/New Item…, and then select <strong>HTML</strong><br />

Page. Once the new page appears, replace its contents with the following: 12<br />

<br />

<br />

<br />

Map<br />

<br />

<br />

//Global variables here<br />

var map = null;<br />

document.addEventListener("DOMContentLoaded", init);<br />

window.addEventListener("message", processMessage);<br />

//Function to turn a string in the syntax { functionName: ..., args: [...] }<br />

//into a call to the named function with those arguments. This constitutes a generic<br />

//dispatcher that allows code in an iframe to be called through postMessage.<br />

function processMessage(msg) {<br />

//Verify data and origin (in this case the local context page)<br />

if (!msg.data || msg.origin !== "ms-appx://" + document.location.host) {<br />

return;<br />

}<br />

var call = JSON.parse(msg.data);<br />

if (!call.functionName) {<br />

throw "Message does not contain a valid function name.";<br />

}<br />

var target = this[call.functionName];<br />

if (typeof target != 'function') {<br />

throw "The function name does not resolve to an actual function";<br />

}<br />

}<br />

return target.apply(this, call.args);<br />

function notifyParent(event, args) {<br />

//Add event name to the arguments object and stringify as the message<br />

args["event"] = event;<br />

window.parent.postMessage(JSON.stringify(args),<br />

"ms-appx://" + document.location.host);<br />

}<br />

12 Note that you should replace the credentials inside the init function with your own key obtained from<br />

https://www.bingmapsportal.com/.<br />

70

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!