|
||||||
How to Add a Google Map to a Web SiteGoogle Maps are Powerful and Can Easily be Added to any Web Page
Is a description of how to get to a location not good enough? If that's the case then add a Google Map to any web site - just by using a simple piece of Javascript code.
Google maps is a powerful mapping system that allows its users to view both maps and satellite pictures of any location in the world. However, even more importantly (from a web site designer's point of view), Google provides a free API (Application Programming Interface) that enables a web site designer to add Google Maps to any web page. All that the web site designer needs are:
Obtaining a Google Maps KeyA Google Maps key may be obtained from the Google API sign up web page. Each key:
That said - it only takes a few seconds to generate the key and it's free. Calling the Google Maps APIThe Google Maps API can be accessed very easily; it is actually a Javascript library and is loaded as part of a web page head section: <html>
<head>
<script
src="http://maps.google.com/maps?file=api&v=2&key=abcd123&sensor=false"
type="text/javascript"></script>
There are two things to take note of when loading the Google Maps API:
Once the API has been loaded then the map itself can be initialized. Initializing the Google MapThe map is initialized by creating a Google Map GMap2 object (if the browser is compatible): <script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
The center of of the map then needs to be set with the setCenter method; this requires:
map.setCenter(new GLatLng(52.7109, -0.3856), 100);
Finally the type of map is selected; this can be:
map.setMapType(G_HYBRID_MAP);
}
}
</script>
</head>
The map is actually loaded at the same time as the body and should be unloaded with the GUnload method - this will prevent any memory leaks: <body onload="initialize()" onunload="GUnload()">
Displaying the MapThe map itself is a div with the id map_canvas: <div id="map_canvas" style="width: 50%; height: 50%"></div>
</body>
</html>
And with that a simple map of anywhere in the world can be displayed in a web page. How to Find the Latitude and Longitude with Google MapsThe maps are always centered on a location's latitude and longitude; the latitude and longitude can be found by using using Google Maps, even though Google Maps does not display them:
SummaryIt is very easy to add a Google Map to any web page; all that's required is for the web site designer to:
And so, with the minimum of effort, a professional looking map will be displayed - all through the power of Google Maps.
The copyright of the article How to Add a Google Map to a Web Site in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish How to Add a Google Map to a Web Site in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||