目录

1 Map Controls

Map controls are used by users to interacting with the map. HUAWEI Map Kit provides various UI controls, including the scale, zoom slider, zoom button, compass, pan button, and copyright information.

Figure 1: Map controls

1. Scale: You can use scaleControl to specify whether to display the scale and use scaleControlOptions to set the scale unit. The default value is false, indicating that the scale is not displayed. If the value is true, the scale will be displayed in the lower right corner of the map.

  1. var mapOptions = {};
  2. mapOptions.center = {lat: 48.856613, lng: 2.352222};
  3. mapOptions.zoom = 10;
  4. mapOptions.scaleControl = true;    // Set to display the scale.
  5. mapOptions.scaleControlOptions = {
  6.    units: "imperial"             // Set the scale unit to inch.  
  7. };

2. Zoom slider: You can use zoomSlider to specify whether to display the zoom slider. The default value is false, indicating that the zoom slider is not displayed. If the value is true, the zoom slider will be displayed in the lower right corner of the map.

  1. var mapOptions = {};
  2. mapOptions.center = {lat: 48.856613, lng: 2.352222};
  3. mapOptions.zoom = 10;
  4. mapOptions.zoomSlider = true;      // Set to display the zoom slider.

3. Zoom button: You can use zoomControl to specify whether to display the zoom button. The default value is true, indicating that the zoom button will be displayed in the lower right corner of the map.

  1. var mapOptions = {};
  2. mapOptions.center = {lat: 48.856613, lng: 2.352222};
  3. mapOptions.zoom = 10;
  4. mapOptions.zoomControl = false;      // Set not to display the zoom button.

4. Compass: You can use rotateControl to specify whether to display the compass. The default value is false, indicating that the compass is not displayed. If the value is true, the compass will be automatically displayed in the upper right corner when the map does not head north.

  1. var mapOptions = {};
  2. mapOptions.center = {lat: 48.856613, lng: 2.352222};
  3. mapOptions.zoom = 10;
  4. mapOptions.rotateControl= true;      // Set to display the compass.

5. Pan button: You can use navigationControl to specify whether to display the pan button. The default value is false, indicating that the pan button is not displayed. If the value is true, the pan button will be displayed in the upper right corner of the map.

  1. var mapOptions = {};
  2. mapOptions.center = {lat: 48.856613, lng: 2.352222};
  3. mapOptions.zoom = 10;
  4. mapOptions.navigationControl= true;      // Set to display the pan button.

6. Copyright information: You can use copyrightControl to specify whether to display the copyright information and use copyrightControlOptions to customize the copyright information and style. The default value is false, indicating that the copyright information is not displayed.

  1. var mapOptions = {};
  2. mapOptions.center = {lat: 48.856613, lng: 2.352222};
  3. mapOptions.zoom = 10;
  4. mapOptions.copyrightControl = true;    // Set to display the copyright information.  
  5. mapOptions.copyrightControlOptions = {value: "<font style='position: absolute; left: 20px; bottom: 20px; z-index: 101' size='5' color='red'>HUAWEI</font>",}// Set the copyright information.
Note:
You can set map controls before creating a map, or use the set method to set map controls after creating a map.

2 Camera

1. Map moving: You can call the map.panTo(latLng) method to move the map center to a specified coordinate point.

  1. <tr>
  2.    <td>X/Latitude:</td>
  3.    <td><input id="xLatInput" type="text" value="48.8"/></td>
  4. </tr>
  5. <tr>
  6.    <td>Y/Longitude:</td>
  7.    <td><input id="yLngInput" type="text" value="2.3"/></td>
  8. </tr>
  1. var lat = Number(document.getElementById("xLatInput").value);
  2. var lng = Number(document.getElementById("yLngInput").value);
  3. map.panTo({lat: lat, lng: lng});

2. Map shift: You can call the map.panBy(x, y) method to shift the map center by the specified x and y values (in pixels).

  1. <tr>
  2.    <td>X/Latitude:</td>
  3.    <td><input id="xLatInput" type="text" value="100"/></td>
  4. </tr>
  5. <tr>
  6.    <td>Y/Longitude:</td>
  7.    <td><input id="yLngInput" type="text" value="100"/></td>
  8. </tr>
  1. var x = Number(document.getElementById("xLatInput").value);
  2. var y = Number(document.getElementById("yLngInput").value);
  3. map.panBy(x, y);

3. Zoom: You can use the map.setZoom(zoom) method to set the zoom level of a map. You can also use the map.zoomIn() or map.zoomOut() method to increase or decrease the zoom level by one each time.

  1. <tr>
  2.     <td>Super Input:</td>
  3.     <td><input id="superInput" type="text" value=""/></td>
  4. </tr>
  1. var zoom = Number(document.getElementById("superInput").value);
  2. map.setZoom(zoom);// Set the map zoom coefficient.
  3. map.zoomIn();// Zoom in the map by one level.
  4. setTimeout(function () {
  5.    map.zoomOut();// Zoom out the map by one level.
  6. }, 5000);

4. Area control: You can use map.fitBounds(bounds) to set the map display scope.

  1. <tr>
  2.    <td>SouthWestLat:</td>
  3.    <td><input id="swLatInput" type="text" value="39.82"/></td>
  4. </tr>
  5. <tr>
  6.    <td>SouthWestLng:</td>
  7.    <td><input id="swLngInput" type="text" value="116.3"/></td>
  8. </tr>
  9. <tr>
  10.    <td>NorthEastLat:</td>
  11.    <td><input id="neLatInput" type="text" value="40.0"/></td>
  12. </tr>
  13. <tr>
  14.    <td>NorthEastLng:</td>
  15.    <td><input id="neLngInput" type="text" value="116.5"/></td>
  16. </tr>
  1. var swLat = Number(document.getElementById("swLatInput").value);
  2. var swLng = Number(document.getElementById("swLngInput").value);
  3. var neLat = Number(document.getElementById("neLatInput").value);
  4. var neLng = Number(document.getElementById("neLngInput").value);
  5. map.fitBounds({
  6.    sw: {lng: swLng, lat: swLat},
  7.    ne: {lng: neLng, lat: neLat},
  8. });

3 Map Events

1. Map click event:

  1. map.on('click', () => {
  2.      map.zoomIn();
  3. });

2. Map center change event:

  1. map.onCenterChanged(centerChangePost);
  2. function centerChangePost() {
  3.    var center = map.getCenter();
  4.    alert( 'Lng:'+center.lng+'\n'+'Lat:'+center.lat);
  5. }

3. Map heading change event:

  1. map.onHeadingChanged(headingChangePost);
  2. function headingChangePost() {
  3.    alert('Heading Changed!')
  4. }

4. Map zoom level change event:

  1. map.onZoomChanged(zoomChangePost);
  2. function zoomChangePost() {
  3.    alert('Zoom Changed!')
  4. }

The following table describes the map events.

EventDescriptionUsage
clickClick on the left button of the mouse once or more.map.on('click', callback): The callback parameter indicates a customized callback function.
singleclickClick on the left button of the mouse once.map.on('singleclick', callback): The callback parameter indicates a customized callback function.
dblclickDouble-click on the left button of the mouse.map.on('dblclick', callback): The callback parameter indicates a customized callback function.
contextmenuClick on the right button of the mouse.map.on('contextmenu', callback): The callback parameter indicates a customized callback function.
pointermoveMoving of the mouse pointer.map.on('pointermove', callback): The callback parameter indicates a customized callback function.
pointerdownMouse button press.map.on('pointerdown', callback): The callback parameter indicates a customized callback function.
pointerupMouse button release.map.on('pointerup', callback): The callback parameter indicates a customized callback function.
movestartMap moving start.map.on('movestart', callback): The callback parameter indicates a customized callback function.
moveendMap moving stop.map.on('moveend', callback): The callback parameter indicates a customized callback function.
pointerdragMap dragging.map.on('pointerdrag', callback): The callback parameter indicates a customized callback function.
onCenterChangedMap center change.map.onCenterChanged(callback): The callback parameter indicates a customized callback function.
onHeadingChangedMap heading change.map.onHeadingChanged(callback): The callback parameter indicates a customized callback function.
onZoomChangedMap zoom level change.map.onZoomChanged(callback): The callback parameter indicates a customized callback function.