目录

1 Map Creation

This section describes how to create a simple web-based map using the HUAWEI Map Kit API for JavaScript.

1. Create an HTML file and declare the following content in the file header so that the file is supported in most browsers:

  1. <!DOCTYPE html>

2. Add the style tag pair in the head tag pair to set the map container dimensions.

  1. <head>
  2.    <meta charset="UTF-8">
  3.    <style>
  4.        #map {
  5.            height: 800px;
  6.            width: 80%;
  7.            margin: 0 auto;
  8.        }
  9.    </style>
  10. </head>

3. Provide the HUAWEI Map Kit API file. The key must be transcoded using the URL.

  1. <script src="https://mapapi.cloud.huawei.com/mapjs/v1/api/js?callback=initMap&key=API KEY"></script>

4. Create map container elements in the body.

  1. <body>
  2. <div id="map"></div>
  3. </body>

Create a script for initializing the map. The following sample code is to create a map with Paris as the center and a zoom level of 8:

  1. <script>
  2.    function initMap() {
  3.        var mapOptions = {};
  4.        mapOptions.center = {lat: 48.856613, lng: 2.352222};
  5.        mapOptions.zoom = 8;
  6.        mapOptions.language='ENG';
  7.        var map = new HWMapJsSDK.HWMap(document.getElementById('map'), mapOptions);
  8.    }
  9. </script>

5. View the created map.

Figure 1: Created map

2 Map Attribute

You can modify the attributes of a map, such as the map center, zoom coefficient, and map heading. The following sample code is to modify the map center:

  1. <tr>
  2.    <td>Latitude:</td>
  3.    <td><input id="latInput" type="text" value="48.95"/></td>
  4. </tr>
  5. <tr>
  6.    <td>Longitude:</td>
  7.    <td><input id="lngInput" type="text" value="2.35"/></td>
  8. </tr>
  1. var lat = Number(document.getElementById("latInput").value);
  2. var lng = Number(document.getElementById("lngInput").value);
  3. map.setCenter({lat: lat, lng: lng});

The following table describes the map attributes that can be customized.

AttributeDescription
setCenter(latlng)Sets the coordinates of the map center point.
setHeading(heading)Sets the map heading, that is, the angle between the map and the north direction.
setOpacity(opacity)Sets the transparency of the map.
setTitle(title)Sets the prompt information for the container where the map is located.
setZoom(zoom)Sets the zoom level of a map.
zoomOut()Zooms out the map by one level.
zoomIn()Zooms in the map by one level.