(Application Programming Interface)
An API is essentially a way for programmers to communicate with a certain application.
Really simple definition:
An API is the middleman between a programmer and an application.
Accepts requests and, if that request is allowed, returns the data
Informs programmers about everything they can request, exactly how to ask for it and how to receive it
Some companies you may have heard of...
Their APIs receive over a billion calls per day!
Google Maps is simply a combination of HTML, CSS and JavaScript working together. The map tiles are images that are loaded into the background.
The Maps API let's us create our own map and use JavaScript to tell it how to behave.
And by that I mean code. You guys ready to try this out?
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body, #map-canvas { height: 100%; margin: 0; padding: 0;}
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: { lat: -34.397, lng: 150.644},
zoom: 8
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=">
</script>
We can get away with leaving out the API key for today's demo. In the real world you'd want to generate the key and include it after the equal sign.
Now we have a giant map of Sydney Australia. Why is that?
It's because the sample code used Sydney's latitude and longitude to center the map.
Remember Geography class?
Go ahead and change up the coordinates to center the map someplace else... your choice!
Right clicking on Google Maps and selecting "What's here" will get you the coordinates of anywhere on earth!
zoom: 18
zoom: 5
Small values display larger swaths of land with lower resolution imagery.
Larger values will narrow in on specific areas displayed in
high resolution.
Back to the documentation. On the left nav select:
Drawing on the Map > Markers > Add a Marker.
var marker = new google.maps.Marker({
position:{ lat: 45.522919, lng: -122.673019},
map: map,
title: 'Voodoo Doughnut'
});
Let's drop a marker on our map with the same coordinates as our map center.
Let's make our pointers drop (or bounce) on page load. Simply add the animation attribute to our marker instance.
var marker = new google.maps.Marker({
position:{ lat: 45.522919, lng: -122.673019},
map: map,
title: 'Voodoo Doughnut',
animation: google.maps.Animation.DROP
}
Try changing DROP to BOUNCE and reload the page. See how it changes.
Go back to the documentation and select Customize a marker image. Figure out how to change the marker from the universal pin to something else.
Hint: Do a Google image search and select 'icon'
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: { lat: 45.522919, lng: -122.673019},
zoom: 18,
scrollwheel: false
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var image = 'donut.png';
var marker = new google.maps.Marker({
position: { lat: 45.522919, lng: -122.673019},
map: map,
title: 'Voodoo Doughnut',
animation: google.maps.Animation.DROP,
icon: image
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
new code
new code
Here's a novel idea. Let's provide information to the user when they click the marker.
We have to add a bit of code to our initialize function.
var contentString = '<h1>Woohooooooo!</h1>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
Check the docs for reference:
Drawing on the Map > Info Windows