Download GoogleMapsAPIs - Getting Started In computer programming, an

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

URL redirection wikipedia , lookup

Transcript
GoogleMapsAPIs - Getting Started
In computer programming, an application programming interface (API) is a set of
functions, protocols, and tools for building application software.
Audience
This documentation is designed for people familiar with JavaScript programming
and object-oriented programming concepts. You should also be familiar
with Google Maps from a user's point of view. There are many JavaScript
tutorials available on the Web.
This conceptual documentation is designed to let you quickly start exploring and
developing applications with the Google Maps JavaScript API. We also publish
the Google Maps JavaScript API Reference.
Hello, World
The easiest way to start learning about the Google Maps JavaScript API is to
see a simple example. The following web page displays a map centered on
Sydney, New South Wales, Australia:
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
<div id="map"></div>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
<!-- Replace the value of the key parameter with your own API key. ->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0y
rcCQoCvLwzdM6M8s5qk&callback=initMap" async defer></script>
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<metaname="viewport"content="initial-scale=1.0">
<metacharset="utf-8">
<style>
/* Always set the map height explicitly to define the size of
the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<divid="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
</script>
<scriptsrc="https://maps.googleapis.com/maps/api/js?key=YOUR_API_
KEY&callback=initMap"
asyncdefer></script>
</body>
</html>
View example (map-simple.html)
Even in this simple example, there are a few things to note:
1.
We declare the application as HTML5 using the <!DOCTYPE
html> declaration.
[ Invented about in 1990 and released in 1993 by TimLee (yes, the same of
the WorldWideWebWWW), Hypertext Markup Language (HTML) is the
standard high-level language for creating web pages]
2.
We create a div element named "map" to hold the Map.
3.
We define a JavasScript function that creates a map in the div.
4.
We load the Maps JavaScript API using a script tag.
These steps are explained below.
Declaring Your Application as HTML5
We recommend that you declare a true DOCTYPE within your web application.
Within the examples here, we've declared our applications as HTML5 using the
simple HTML5 DOCTYPE as shown below:
<!DOCTYPE html>
Most current browsers will render content that is declared with this DOCTYPE in
"standards mode" which means that your application should be more crossbrowser compliant. The DOCTYPE is also designed to degrade gracefully; browsers
(A WebBrowser is an application software that provides to the user a webPage
obtained from internet) that don't understand it will ignore it, and use "quirks
mode" to display their content:
[ it refers to a technique used by web browsers for the sake of maintaining
backward compatibility with web pages designed for Internet Explorer 5 and
earlier].
Note that some CSS (Cascading Style Sheets is a language used for describing
the presentation of a HTML document.It controls layout of many docs
from one single style sheet, apply different layout to different
media types (screens, printers)) that works within quirks mode is not
valid in standards mode. In specific, all percentage-based sizes must inherit
from parent block elements, and if any of those ancestors fail to specify a size,
they are assumed to be sized at 0 x 0 pixels. For that reason, we include the
following <style> declaration:
<style>
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
This CSS declaration indicates that the map container <div> (with id map) should
take up 100% of the height of the HTML body. Note that we must specifically
declare those percentages for <body> and <html> as well.
Loading the Google Maps JavaScript API
(Application Programming Interface through
which computers can interact with each other)
To load the Google Maps JavaScript API, use a script tag like the one in the
following example:
<scriptasyncdefer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&c
allback=initMap">
</script>
The URL contained in the script tag is the location of a JavaScript file that loads
all of the symbols and definitions you need for using the Maps JavaScript API.
This script tag is required.
The async (Asynchronous) attribute lets the browser render the rest of your
website while the Maps JavaScript API loads. When the API is ready, it will call
the function specified using the callback parameter.
The key parameter contains your application's API key. See Get a Key for more
information.
Note: Google Maps APIs Premium Plan customers may use either an API key
or a valid client ID when loading the API. Get more information on authentication
parameters for Premium Plan customers.
HTTPS or HTTP
The Hypertext Transfer Protocol (HTTP) is an application protocol to exchange
or transfer hypertextFiles on the internet;Hypertext Markup Language
was created in 1991 by Tim Barners Lee who came up with the
idea of sharing info no matter where a computer was located by
using Hyperlinks coded links that connected one source to
another, a communication protocol for web servers and web
users and URL (Uniform Resource Locator) a streamline address
system for every web page.
URL (Uniform Resource Locator) is a reference to a web resource that specifies
its location on a computer network and a mechanism for retrieving it.
We think security on the web is pretty important, and recommend using HTTPS
whenever possible. As part of our efforts to make the web more secure, we've
made all of the Maps JavaScript API available over HTTPS. Using HTTPS
encryption makes your site more secure, and more resistant to snooping or
tampering.
We recommend loading the Maps JavaScript API over HTTPS using
the <script> tag provided above.
If required, you can load the Maps JavaScript API over HTTP by
requesting http://maps.googleapis.com/, orhttp://maps.google.cn for users in
China.
TAGS= are works or acronyms surrounded by brackets
HTML TAGS= are written as pair: there must be a beginning tag and an
ending tag in order to make the code display correctly. The first tag
designates how the following text will be grouped or displayed and the
closing tag (signaled with a back slash) designates the end of this group
or display.
<p> this is an example of an opening paragraph tag </p> closing
paragraph tag.
Web browsers read th HTLM code contained in web pages and translate it
not a readable content. This markup can contain the basic building block
of a web page: the title, the headline, the paragraph, the body text and the
links.
Libraries
When loading the Maps JavaScript API via the URL you may optionally load
additional libraries through use of thelibraries URL parameter. Libraries are
modules of code that provide additional functionality to the main Maps
JavaScript API but are not loaded unless you specifically request them. For
more information, see Libraries in the Maps JavaScript API.
Synchronously Loading the API
In the script tag that loads the Maps JavaScript API, it is possible to omit
the async attribute and the callbackparameter. This will cause the loading of the
API to block until the API is downloaded.
This will probably slow your page load. But it means you can write subsequent
script tags assuming that the API is already loaded.
Map DOM Elements
<divid="map"></div>
For the map to display on a web page, we must reserve a spot for it. Commonly,
we do this by creating a named divelement and obtaining a reference to this
element in the browser's document object model (DOM:
The Document Object Model (DOM) is a cross-platform and language that treats
an HTML, XHTML, or XML document as a tree structure wherein each node is
an object representing a part of the document).
In the example above, we used CSS to set the height of the map div to "100%".
This will expand to fit the size on mobile devices. You may need to adjust the
width and height values based on the browser's screensize and padding. Note
that divs usually take their width from their containing element, and empty divs
usually have 0 height. For this reason, you must always set a height on
the <div> explicitly.
Div element = It is used to layout a web page, it does not
inherently represent anything. Use it to group elements for
purposes such as styling, marking a section of a document in a
different language. It should be used only when no other
semantic element is appropriate.
Map Options
There are two required options for every map: center and zoom.
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
Zoom Levels
The initial resolution at which to display the map is set by the zoom property,
where zoom 0 corresponds to a map of the Earth fully zoomed out, and larger
zoom levels zoom in at a higher resolution. Specify zoom level as an integer.
zoom: 8
Offering a map of the entire Earth as a single image would either require an
immense map, or a small map with very low resolution. As a result, map images
within Google Maps and the Maps JavaScript API are broken up into map "tiles"
and "zoom levels." At low zoom levels, a small set of map tiles covers a wide
area; at higher zoom levels, the tiles are of higher resolution and cover a smaller
area. The following list shows the approximate level of detail you can expect to
see at each zoom level:

1: World

5: Landmass/continent

10: City

15: Streets

20: Buildings
The following three images reflect the same location of Tokyo at zoom levels 0,
7 and 18.
For information on how the Maps JavaScript API loads tiles based on the current
zoom level, see Tile Coordinates in the Map Types documentation.
The Map Object
map = new google.maps.Map(document.getElementById("map"), {...});
The JavaScript class that represents a map is the Map class. Objects of this class
define a single map on a page. (You may create more than one instance of this
class — each object will define a separate map on the page.) We create a new
instance of this class using the JavaScript new operator.
When you create a new map instance, you specify a <div> HTML element in the
page as a container for the map. HTML nodes are children of the
JavaScript document object, and we obtain a reference to this element via
thedocument.getElementById() method.