Add a Google Map to your website
Adding a Google Map to your website
Introduction
Adding a Google Map to your web page is very easy, once you've been shown how! That's what we're going to do in this lesson - we'll go over each step of creating a basic Google Map using the JavaScript API.From there, it's only a matter of tweaking some of the options to customize the map to your liking.
The basic HTML page:
Because everything on the web is made up of HTML, we'll start there. The following code creates the simplest of web pages:<!DOCTYPE html>
<html>
<head></head>
<body>
</body>
</html>
None of this is specific to Google Maps - it's the basis for any HTML page.
Open your text editor and add this code, then save the file to your desktop as
google-maps.html (or any other filename that ends with .html).Your Google Map requires a page element in which to appear; add a
div tag to
the body with an id attribute of map. This creates a container that
we'll reference later in the lesson.<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="map"></div>
</body>
</html>
Set the width and height of the div element using CSS. By default, a div
has a height of 0, meaning that any map you place inside it won't be visible.
Use a style tag in the head to set the map to any size; in this case 500
pixels wide and 400 pixels high.<!DOCTYPE html>
<html>
<head>
<style>
#map {
width: 500px;
height: 400px;
}
</style>
</head>
<body>
<div id="map"></div>
</body>
</html>
Load the HTML file in a web browser by dragging it from your desktop into the
address bar of your browser. You'll notice that nothing is displayed - there's
nothing in the div yet. If you'd like to see the div on your page, add a
background-color CSS declaration to your existing <style> tag:<style>
#map {
width: 500px;
height: 400px;
background-color: #CCC;
}
</style>
Reloading the page will display a grey box; that's your div.To bring forth a map, you must add two pieces of JavaScript to your page. The first loads the Google Maps JavaScript API; the second creates and configures the map.
Loading the API:
Load the Google Maps API by adding a<script> tag to the <head> section of
your HTML. This script downloads the code required to display maps on your
page.<!DOCTYPE html>
<html>
<head>
<style>
#map {
width: 500px;
height: 400px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
</head>
<body>
<div id="map"></div>
</body>
</html>
Create and configure the map:
The final piece of code is the JavaScript that creates the map. The code contains a function to run once the page has loaded. In this example, and all of the examples in the Maps API documentation, this function is namedinitialize.<script>
function initialize() {}
</script>
Add this code immediately after the <script> tag you created in the last
step.The google.maps.Map object
The first thing theinitialize function needs to do is create a new Google
Maps object:var map = new google.maps.Map();
The Map object constructor takes two arguments:-
A reference to the div that the map will be loaded into. We use the
JavaScript
getElementByIdfunction to obtain this.
<script> function initialize() { var mapCanvas = document.getElementById('map'); var map = new google.maps.Map(mapCanvas); } </script> -
Options for the map, such as the center, zoom level, and the map type. There
are many more options
that can be set, but these three are required.
<script> function initialize() { var mapCanvas = document.getElementById('map'); var mapOptions = { center: new google.maps.LatLng(44.5403, -78.5463), zoom: 8, mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(mapCanvas, mapOptions); } </script>centeris a Google MapsLatLngobject that tells the the API where to center the map.zoomis a number between 0 (farthest) and 22 that sets the zoom level of the map.mapTypeIdis used to specify what type of map to use. Your choices areROADMAP,SATELLITE,HYBRID, orTERRAIN.
Executing the JavaScript function
Add an event listener to thewindow object that will call the initialize
function once the page has loaded. Calling initialize before the page has
finished loading will cause problems, since the div it's looking for may not
have been created yet; this function waits until the HTML elements on the page
have been created before calling initialize. <script>
function initialize() {
...
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
The finished code:
This is the final code you've put together in this lesson. It:- Creates a div, and gives it a size.
- Loads the Google Maps JavaScript API.
- Creates and displays a Google Map in the div.
<!DOCTYPE html>
<html>
<head>
<style>
#map {
width: 500px;
height: 400px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var mapCanvas = document.getElementById('map');
var mapOptions = {
center: new google.maps.LatLng(44.5403, -78.5463),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions)
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>
Conclusion:
finally we can say that if you learn this article step by step, you will able to add google map in your website. So I"ll say you should go though this article cleverly. So start.......
Add a Google Map to your website
Reviewed by Lawyer Sabuj
on
11:20:00 PM
Rating:
Reviewed by Lawyer Sabuj
on
11:20:00 PM
Rating:



No comments:
Post a Comment