Let us consider a example....
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var mapProp = {
center:new google.maps.LatLng(51.508742,-0.120850),
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>
</html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var mapProp = {
center:new google.maps.LatLng(51.508742,-0.120850),
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>
</html>
Explanation:
1. Load the Google API
The Google Maps API is a JavaScript library. It can be added to a web page with a <script> tag:
<script src="http://maps.googleapis.com/maps/api/js"></script>
2. Set Map Properties
Create a function to initialize the map:
function initialize() {
}
}
var mapProp = {
center:new google.maps.LatLng(51.508742, -0.120850),
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
center:new google.maps.LatLng(51.508742, -0.120850),
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
The zoom property specifies the zoom level for the map. zoom: 0 shows a map of the Earth fully zoomed out. Higher zoom levels zoom in at a higher resolution.
The mapTypeId property specifies the map type to display. The following map types are supported:
- ROADMAP (normal, default 2D map)
- SATELLITE (photographic map)
- HYBRID (photographic map + roads and city names)
- TERRAIN (map with mountains, rivers, etc.)
3. Create a Map Container
Create a <div> element to hold the map.Use CSS to size the element:
<div id="googleMap" style="width:500px;height:380px;"></div>
4. Create a Map Object
var map=new google.maps.Map(document.getElementById("googleMap"), mapProp);
5. Add an Event Listener to Load the Map
Add a DOM listener that will execute the initialize() function on window load (when the page is loaded):
google.maps.event.addDomListener(window, 'load', initialize);
No comments:
Post a Comment