Google Maps JavaScript API v3
Alex Jolicoeur
Google Maps JavaScript API v3
Lets you embed and manipulate Google maps within your web pages.
Version 3 of the API is especially designed to be faster and more applicable to mobile devices, as well as traditional desktop browser applications.
The utilites and services available are nearly identical to those of https://maps.google.com
Loading the API
An API key is easily obtained from your google developer API console. It is not required since the API service is free, but recommended in order to track usage.
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=TRUE_OR_FALSE">
</script>
sensor indicates whether this application uses a sensor (such as a GPS locator) to determine the user's location.
Loading the API Asynchronously
You may want to load the Maps API after your page has fully loaded, or on demand (using window.onload) and to write the Maps JavaScript API into a <script> tag within the page.
Additionally, you can instruct the API to only execute the map's initialize() function after the API has fully loaded by passing "callback=initialize" to the Maps API bootstrap.
http://people.ucsc.edu/~ajolicoe/googleAPI/basic/asyncMap.html
Map DOM and Options
To display the map, you must create a spot in the DOM for it
(in this case a 'div' element):
<div id="map_canvas" style="width: 100%; height: 100%"></div>
Before creating the map, we can define options for it:
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
Map Options continued
center: (latitude, longitude) can be looked up using geolocation service.
zoom: when 0, map image is fully zoomed out.
mapTypeId:
The Map Object and Events
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
Each Map object exports a number of named events.You can register JavaScript event listeners for those events and execute code when those events are received by registering addListener() event handlers on the google.maps.event namespace.
UI Events:
(click, dblclick, mouseup, mousedown, mouseover, mouseout, etc)
google.maps.event.addListener(map, 'click', function(e) {� placeMarker(e.latLng, map);�});
http://people.ucsc.edu/~ajolicoe/googleAPI/events/addMarker.html
Map Events continued
MVC State Change Events:
When an object's property changes, the API will trigger an event that the property has changed.
For example: 'zoom_changed' event executes when the map's zoom level changes. Also have bounds_changed and center_changed.You will want to inspect the property that changed on an MVC state change by calling the appropriate getProperty method on that object.
google.maps.event.addListener(map, 'zoom_changed', function() {
var zoomLevel = map.getZoom();
map.setCenter(myLatLng);
infowindow.setContent('Zoom: ' + zoomLevel);
});
http://people.ucsc.edu/~ajolicoe/googleAPI/events/simpleEvent.html
Map Controls
in mapOptions: 'disableDefaultUI: false' will display the default controls on the map.
More controls
http://people.ucsc.edu/~ajolicoe/googleAPI/controls/simpleControls.html
Layers
http://people.ucsc.edu/~ajolicoe/googleAPI/layers/trafficLayer.html
More Layers
http://people.ucsc.edu/~ajolicoe/googleAPI/layers/weatherLayer.html
The Aerial View
You can disable 45° imagery by calling setTilt(0) on the Map object.
To enable 45° perspective imagery for supported map types: setTilt(45)
To orient map towards one of the cardinal directions: setHeading(90)
http://people.ucsc.edu/~ajolicoe/googleAPI/basic/aerial45.html
http://people.ucsc.edu/~ajolicoe/googleAPI/basic/aerialRotate.html
Map Styling
There are two ways to style maps: By setting the .styles property of the map's MapOptions object. Or by creating and defining a StyledMapType and applying it to the map.
using the .styles property:
http://people.ucsc.edu/~ajolicoe/googleAPI/styles/asyncStyled.html
creating a new StyledMapType:
http://people.ucsc.edu/~ajolicoe/googleAPI/styles/asyncStyled2.html
Map Overlays
markers, symbols, polylines, polygons, circles, user-editable shapes, info windows, and ground overlays.
To set an overlay: To remove and overlay:
overlay.setMap(map) overlay.setMap(null)
markers example:
http://people.ucsc.edu/~ajolicoe/googleAPI/overlays/markerOverlay.html
ground overlay example:
http://people.ucsc.edu/~ajolicoe/googleAPI/overlays/groundOverlay.html
Services
Directions
http://people.ucsc.edu/~ajolicoe/googleAPI/services/directions/directions.html
Distance Matrix
http://people.ucsc.edu/~ajolicoe/googleAPI/services/distance/distance.html
Elevation
http://people.ucsc.edu/~ajolicoe/googleAPI/services/elevation/elevation.html
Geocoding/Reverse Geocoding
http://people.ucsc.edu/~ajolicoe/googleAPI/services/geolocation/geocoder.html
Maximum Zoom Imagery (for satellite photos)
http://people.ucsc.edu/~ajolicoe/googleAPI/services/maxZoom/maxZoom.html
Libraries
adSense: embed ads in your map application
Drawing: provides a graphical interface for users to draw polygons, rectangles, polylines, circles, and markers on the map.
Geometry: provides utility functions for the computation of geometric data on the surface of the Earth. The library includes three namespaces:
Panoramio: allows you to add Panoramio photos as layers to your application
More Libraries
Places: With the Places Service you can perform three kinds of searches:
The information returned can include establishments — such as restaurants, stores, and offices — as well as 'geocode' results, which indicate addresses, political areas such as towns and cities, and other points of interest.
http://people.ucsc.edu/~ajolicoe/googleAPI/libraries/places/search_places.html
Weather: You can enable the display of weather data or cloud imagery on your map. Enabling the cloud layer will add cloud coverage imagery to your map, visible at zoom levels 0-6. Enabling the weather layer will show current weather conditions from weather.com on your map, including icons that denote sun, clouds, rain and so on.