Introduction
Almost everyone who is reading this article would have used Google Earth. So here I show how you can use it in your Windows application. It provides all the navigation functionalities of Google Earth.
Requirements
The basic requirements which should be fulfilled to use this utility are:
- Google Earth Plug-in should be installed on your system (small one, takes a minute or two). When you will run this application, it will show you the link itself, so you don't need to worry about it.
- Since this uses Google APIs, you should have an App key from Google (it's quite easy to get one). Get it from here.
- You should have Visual Studio installed on your system.
Basic Fundamental
We have an HTML page (using JavaScript) which just displays Google Earth on it. So the basic idea is that we will add a browser control to our application and set the URL to our HTML page.
HTML Page
The HTML page uses JavaScript and is not made up of a thousand lines of code. All the work is done by Google. We just need to call certain functions so that we could have the work done.
<html>
<head>
<title>Sample</title>
<script src="http://www.google.com/jsapi?key="My App key here"> </script>
<script type="text/javascript">
var ge;
google.load("earth", "1");
function init() {
google.earth.createInstance('map3d', initCB, failureCB);
}
function initCB(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
}
function failureCB(errorCode) {
}
google.setOnLoadCallback(init);
</script>
</head>
<body>
<div id="map3d" style="height: 400px; width: 600px;">
</div>
</body>
</html>
Please notice the first script
tag. In its key attribute, you need to add your App key which you get from Google. Even I have one, but I don't feel like making it public.
Creating Windows Form
- Create a Windows Forms application
- To your form, add a browser control as shown below:
In the forms designer file (Form1.Designer.cs), change the URL property of your browser control to the HTML file. Also change the URI kind to absolute.
this.webBrowser1.Url = new System.Uri(System.Environment.CurrentDirectory + "\\" +
"MYFiles\\MyGoogleEarthFile.html", System.UriKind.Absolute);
Set the height of your browser control to 400 and width to 600 because these are the dimensions in which our Google earth will be shown.
FINAL STEP
The final step isn't some big piece of code. So just run your application. That's it.
Do post your comments on the article, whatever way you feel about it. Thanks !!!!
History
- 19th August, 2009: Initial post