Google Maps Basics
Create a simple Google Map
Now let us create a simple Google Map.
The following is a Google Map showing London, UK:
Example
<!
DOCTYPE
html<
html
<
head
<
script
src
=
"
https://maps.googleapis.com/maps/api/js?key=AIzaSyDJW4jsPlNKgv6jFm3B5Edp5ywgdqLWdmc&callback=initMap
"
async
defer
script
<
scriptfunction 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
Try it »
Example Parsing
We use the above example to analyze the process of creating a Google Map.
Why should applications declare HTML5?
Most browsers use the "standard mode" HTML5 document to render pages, which means your application is compatible with major browsers.
In addition, if there is no DOCTYPE tag, the browser uses quirks mode to render page content.
Tip: Note that some CSS in "quirks mode" cannot be used in standard mode. In specific applications, all percentage-based sizes must be inherited from the parent block element. If no size is specified in the parent module, the default value is 0 x 0 pixels. If you want to use percentages, you can
This style declaration indicates that the map module (GoogleMap) should have an HTML height of 100%.
Add Google Maps API Key
In the following example, the first
Place the API key generated by google into key parameter (key=YOUR_API_KEY).
The sensor The parameter is required and is used to indicate whether the application uses a sensor (such as GPS navigation) to locate the user's position. The parameter value can be set to true or false.
HTTPS
If your application is a secure HTTP (HTTPS: HTTP Secure) application, you can use HTTPS to load the Google Maps API:
Asynchronous loading
We can also load the Google Maps API after the page is fully loaded.
The following example uses window.onload to load the Google Maps API after the page is fully loaded. The loadScript() function creates the loading of the Google Maps API