在地图上,如何添加新的UI按钮?

时间:2018-11-29 17:02:24

标签: javascript vue.js here-api

我有一个默认的交互式诺基亚此处地图v3地图设置,它在地图上的一个组中有多个标记。

我想在地图上添加一个按钮或其他ui元素,可以按下该按钮或其他ui元素,当它确实调用该函数时,我将围绕组中的所有标记尽可能缩小。但是,除了信息气泡或地图的默认ui元素之外,我找不到添加不需要的新示例ui的示例代码。

我想要的是类似于pano按钮的功能,但是在地图的左上角,当单击该按钮时,它将调用我的setBounds函数以缩小以包含该gorup中的所有标记。

这是我当前用于地图的javascript代码。

// VUE
var vue1 = new Vue({
  el: '#app',
  data: () => ({
      behavior: null,
      centerCoords: { lng: #centerLon#, lat: #centerLat# },
      defaultLayers: null,
      devices: null,
      markerGroup: null,
      map: null,
      platform: null,
      ui: null,
  }),
  created: function(){
    // Initialize the platform object:
    this.platform = new H.service.Platform({
      'app_id': 'AN ID WOULD GO HERE',
      'app_code': 'A CODE WOULD GO HERE'
    });

    this.defaultLayers = this.platform.createDefaultLayers();
  },
  mounted: function(){
    // Instantiate (and display) a map object:
    this.map = new H.Map(
      document.getElementById('mapContainer'),
      this.defaultLayers.satellite.traffic,
      {
          center: this.centerCoords,
          zoom: 15,
      }
    );

    // Make Map interactive
    // MapEvents enables the event system
    // Behavior implements default interactions for pan/zoom (also on mobile touch environments)
    this.behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(this.map));

    // Create the default UI Components
    this.ui = H.ui.UI.createDefault(this.map, this.defaultLayers, 'en-US');
    this.ui.setUnitSystem(H.ui.UnitSystem.IMPERIAL);

    this.setMarkers();
    setTimeout(this.setBounds, 250);
    setInterval(this.setMarkers, 1 * 60 * 1000);
  },
  computed:{
  },
  methods:{
      setBounds: function(){
          this.map.setViewBounds(this.markerGroup.getBounds());
      },
      setMarkers: function(){
          let self = this;
          // if already present remove markerGroup from map
          if(self.markerGroup){
              self.markerGroup.removeAll();
          }

          //get request
          $.get(
              '/api/v1/getMarkers',
              data => {
                  let zIndex = 1;
                  self.devices = data;

                  // create new marker group from get request.
                  self.markerGroup = new H.map.Group();

                  // add marker group to the map
                  self.map.addObject(self.markerGroup);

                  // add each marker to the marker group
                  self.devices.forEach((el, ind, arr) => {
                      self.addMarkerToGroup(
                          self.markerGroup,
                          {lat: el.latitude, lng: el.longitude},
                          '<div>' + el.serial + '</div>'
                      );
                  });

                  self.map.addEventListener('tap', evt => {
                      if(evt.target instanceof mapsjs.map.Marker){
                          // increase z-index of the marker that was tapped
                          evt.target.setZIndex(zIndex++);
                      }
                  });

                  self.markerGroup.addEventListener('tap', evt => {
                      let bubble = new H.ui.InfoBubble(evt.target.getPosition(), {
                          content: evt.target.getData()
                      });
                      self.ui.addBubble(bubble);
                  }, false);
              },
              'json'
          );
      },
      addMarkerToGroup: function(group, coordinate, html){
          let marker = new H.map.Marker(coordinate);
          marker.setData(html);
          group.addObject(marker);
      }
  }
});

1 个答案:

答案 0 :(得分:0)

您可以尝试一下:

inherits = function(childCtor, parentCtor) {   function tempCtor() {}  tempCtor.prototype = parentCtor.prototype;   childCtor.superClass_ = parentCtor.prototype;   childCtor.prototype = new tempCtor();   childCtor.prototype.constructor = childCtor;   childCtor.base = function(me, methodName, var_args) {
    var args = new Array(arguments.length - 2);
    for (var i = 2; i < arguments.length; i++) {
      args[i - 2] = arguments[i];
    }
    return parentCtor.prototype[methodName].apply(me, args);   }; };

var ZoomTo = function(opt_options) {   'use strict';   var options = opt_options || {};

  H.ui.Control.call(this);   this.onButtonClick = this.onButtonClick.bind(this);

  // create a button element   this.increaseBtn_ = new H.ui.base.Button({
    'label': '<svg class="H_icon H_icon" viewBox="0 0 25 25">' +
      '<path d="M 18.5,11 H 14 V 6.5 c 0,-.8 -.7,-1.5 -1.5,-1.5 -.8,0 -1.5,.7 -1.5,1.5 V 11 H 6' +
      '.5 C 5.7,11 5,11.7 5,12.5 5,13.3 5.7,14 6.5,14 H 11 v 4.5 c 0,.8 .7,1.5 1.5,1.5 .8,0 1.5,' +
      '-.7 1.5,-1.5 V 14 h 4.5 C 19.3,14 20,13.3 20,12.5 20,11.7 19.3,11 18.5,11 z" />' +
      '</svg>',
    'onStateChange': this.onButtonClick   });

  //add the buttons as this control's children   this.addChild(this.increaseBtn_);

  this.setAlignment(options['alignment'] || 'top-right');

  this.options_ = options; }; inherits(ZoomTo, H.ui.Control);

ZoomTo.prototype.onButtonClick = function(evt) {   'use strict';   if (evt.currentTarget.getState() === 'down') {
    console.log('Zoom to the set of markers');   } };

var zoomTo = new ZoomTo(); ui.addControl('zoomToMarkers', zoomTo);