We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesMap KitJavaScriptBasic MapMap Creation

Map Creation

You can create a simple web-based map by using JavaScript APIs. The procedure is as follows:

JavaScript APIs provide two authentication modes for map initialization. You are advised to use access token authentication.

Access Token Authentication

NOTE
  • The access token is valid for 1 hour. You need to update it in time.
  • If both the API key and access token are set, the access token will be used for authentication.
  • If access token authentication is used, you are advised to apply for a new access token to access Map Kit.
  • A request that carries traceId can be used for fault tracing and locating.
  1. Create an HTML file and declare the following content in the file header so that the file is supported in most browsers:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <!DOCTYPE html>

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

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

  3. Import the Map Kit API file.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <script src="https://mapapi.cloud.huawei.com/mapjs/v1/mapkit.js"></script>
    If the routing address is required, set the accessRegion parameter.
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <script src="https://mapapi.cloud.huawei.com/mapjs/v1/mapkit.js?accessRegion=CN"></script>

  4. Create map container elements in the body.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <body>
    2. <div id="map"></div>
    3. </body>

  5. Call the HWMapJsSDK.HWMap(div, mapOptions) method, pass the map initialization parameter MapOptions to set the initial map status, and set an access token. The following sample code creates a map centered on Paris:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <script>
    2. function initMap() {
    3. var mapOptions = {};
    4. // Set the coordinates of the map center point.
    5. mapOptions.center = {lat:
      48.856613
      , lng:
      2.352222
      };
    6. // Set the initial zoom level of the map.
    7. mapOptions.zoom =
      8
      ;
    8. // Set the map language.
    9. mapOptions.language='
      ENG
      ';
    10. // Set the type of tiles loaded to the map, which can be vector or raster.
    11. mapOptions.sourceType = '
      raster
      ';
    12. // Set an access token.
    13. mapOptions.authOptions = {accessToken: token}
    14. // Create a map object.
    15. var map = new HWMapJsSDK.HWMap(document.getElementById('map'), mapOptions);
    16. }
    17. </script>

    The figure below shows the map effect.

API Key Authentication

NOTE

A request that carries traceId can be used for fault tracing and locating.

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

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <!DOCTYPE html>

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

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

  3. Import the Map Kit API file. The key must be transcoded using the URL. For details about how to obtain the API key, please refer to Obtaining the API Key.

    Collapse
    Word wrap
    Dark theme
    Copy code
    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.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <body>
    2. <div id="map"></div>
    3. </body>

  5. Call the HWMapJsSDK.HWMap(div, mapOptions) method and pass the map initialization parameter MapOptions to set the initial map status. The following sample code creates a map centered on Paris:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. <script>
    2. function initMap() {
    3. var mapOptions = {};
    4. // Set the coordinates of the map center point.
    5. mapOptions.center = {lat:
      48.856613
      , lng:
      2.352222
      };
    6. // Set the initial zoom level of the map.
    7. mapOptions.zoom =
      8
      ;
    8. // Set the map language.
    9. mapOptions.language='
      ENG
      ';
    10. // Set the type of tiles loaded to the map, which can be vector or raster.
    11. mapOptions.sourceType = '
      raster
      ';
    12. // Create a map object.
    13. var map = new HWMapJsSDK.HWMap(document.getElementById('map'), mapOptions);
    14. }
    15. </script>

    The figure below shows the map effect.

Search in Guides
Enter a keyword.