Custom map overlay heremaps js api v3

天涯浪子 提交于 2019-12-25 08:49:49

问题


I am trying to switch a project from here maps 2.5.4 to 3.0.5. I have a map with a custom animated image overlay. In 2.5.4 it is realized via ImageProvider:

var imageProvider = new nokia.maps.map.provider.ImageProvider({
    opacity: 0.8,
    getBoundingBox: function() {
        return new nokia.maps.geo.BoundingBox(
            new nokia.maps.geo.Coordinate(55.599073, 3.550307),
            new nokia.maps.geo.Coordinate(47.27036232672, 15.434621365086)
        )},
    getUrl: function() {
        return images[index]; // return the current image
    },
    updateCycle: 86400,
    cache: new nokia.maps.util.Cache(100)
});

//add listener to show next image
imageProvider.addListener("update", function(evt) {
    index++;
}

In v3.0.5 there is no ImageProvider and I didn't find another solution in the api documentation. Does anyone know how to realize it in v3 ?


回答1:


Yup, there seems to be no ImageProvider (yet?). You can hack it in, though:

// assuming you have a H.Map instance call "map"

// that's where we want the image
var rect = new H.geo.Rect(51, 12, 54, 15),
  // that's the images we want
  images = [
    'http://newnation.sg/wp-content/uploads/random-pic-internet-06.jpg',
    'http://newnation.sg/wp-content/uploads/random-pic-internet-04.jpg',
    'http://newnation.sg/wp-content/uploads/random-pic-internet-06.jpg'
  ],
  current = 0,
  // that the image node we'll use
  image = document.createElement('img');

// you could probably use CSS3 matrix transforms to improve performance
// but for demo purposes, I'lluse position:absolute and top/left for the image
image.style.position = "absolute";
image.style.opacity = "0.8";

// this function updates the image whenever something changes
var update = function() {
  // project the rectangle's geo-coords to screen space
  var topLeft = map.geoToScreen(rect.getTopLeft());
  var bottomRight = map.geoToScreen(rect.getBottomRight());
  // calculate top/left and width/height
  var offsetX = topLeft.x;
  var offsetY = topLeft.y;
  var width = bottomRight.x - topLeft.x;
  var height = bottomRight.y - topLeft.y;

  // set image source (update is also called, when we choose another image)
  image.src = images[current];
  // set image position and size
  image.style.top = offsetY + "px";
  image.style.left = offsetX + "px";
  image.style.width = width + "px";
  image.style.height = height + "px";
};

// append the image
map.getViewPort().element.appendChild(image);
// set initial values
update();

// update whenever viewport or viewmodel changes
map.getViewPort().addEventListener('sync', function() {
  update();
});
map.getViewModel().addEventListener('sync', function() {
  update();
});

// zoom to rectangle (just to get the images nicely in view)
map.setViewBounds(rect);

// start the image change interval
setInterval(function() {
  current = (current + 1) % 3;
  update();
}, 3000);


来源:https://stackoverflow.com/questions/26406396/custom-map-overlay-heremaps-js-api-v3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!