jq之google地图

The basic HTML page

Because everything on the web is made up of HTML, we'll start there. The following code creates the simplest of web pages:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
  </body>
</html>

None of this is specific to Google Maps - it's the basis for any HTML page. Open your text editor and add this code, then save the file to your desktop as google-maps.html (or any other filename that ends with .html).

Your Google Map requires a page element in which to appear; add a div tag to the body with an id attribute of map-canvas. This creates a container that we'll reference later in the lesson.

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

Set the width and height of the div element using CSS. By default, a div has a height of 0, meaning that any map you place inside it won't be visible. Use a style tag in the head to set the map to any size; in this case 500 pixels wide and 400 pixels high.

<!DOCTYPE html>
<html>
  <head>
    <style>
      #map-canvas {
        width: 500px;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

Load the HTML file in a web browser by dragging it from your desktop into the address bar of your browser. You'll notice that nothing is displayed - there's nothing in the div yet. If you'd like to see the div on your page, add a background-color CSS declaration to your existing <style> tag:

<style>
  #map-canvas {
    width: 500px;
    height: 400px;
    background-color: #CCC;
  }
</style>

Reloading the page will display a grey box; that's your div.

To bring forth a map, you must add two pieces of JavaScript to your page. The first loads the Google Maps JavaScript API; the second creates and configures the map.

Loading the API

Load the Google Maps API by adding a <script> tag to the <head> section of your HTML. This script downloads the code required to display maps on your page.

<!DOCTYPE html>
<html>
  <head>
    <style>
      #map-canvas {
        width: 500px;
        height: 400px;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js"></script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

Create and configure the map

The final piece of code is the JavaScript that creates the map. The code contains a function to run once the page has loaded. In this example, and all of the examples in the Maps API documentation, this function is named initialize.

<script>
  function initialize() {}
</script>

Add this code immediately after the <script> tag you created in the last step.

The google.maps.Map object

The first thing the initialize function needs to do is create a new Google Maps object:

var map = new google.maps.Map();

The Map object constructor takes two arguments:

  • A reference to the div that the map will be loaded into. We use the JavaScript getElementById function to obtain this.

    <script>
      function initialize() {
        var mapCanvas = document.getElementById('map-canvas');
        var map = new google.maps.Map(mapCanvas);
      }
    </script>
    
  • Options for the map, such as the center, zoom level, and the map type. There are many more options that can be set, but these three are required.

    <script>
      function initialize() {
        var mapCanvas = document.getElementById('map-canvas');
        var mapOptions = {
          center: new google.maps.LatLng(44.5403, -78.5463),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(mapCanvas, mapOptions);
      }
    </script>
    
    • center is a Google Maps LatLng object that tells the the API where to center the map.
    • zoom is a number between 0 (farthest) and 22 that sets the zoom level of the map.
    • mapTypeId is used to specify what type of map to use. Your choices are ROADMAPSATELLITEHYBRID, or TERRAIN.

Executing the JavaScript function

Add an event listener to the window object that will call the initialize function once the page has loaded. Calling initialize before the page has finished loading will cause problems, since the div it's looking for may not have been created yet; this function waits until the HTML elements on the page have been created before calling initialize.

    <script>
      function initialize() {
        ...
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>

The finished code

This is the final code you've put together in this lesson. It:

  • Creates a div, and gives it a size.
  • Loads the Google Maps JavaScript API v3.
  • Creates and displays a Google Map in the div.
<!DOCTYPE html>
<html>
  <head>
    <style>
      #map-canvas {
        width: 500px;
        height: 400px;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script>
      function initialize() {
        var mapCanvas = document.getElementById('map-canvas');
        var mapOptions = {
          center: new google.maps.LatLng(44.5403, -78.5463),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(mapCanvas, mapOptions)
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

We recommend reading the Customizing Google Maps class next. For inspiration, check out some of the applications at morethanamap.com.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值