Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

In my app I added a mapquest layer with open layers 3, drawed points and lines...

Now i need to know the pixel coordinates from a long/lat point in the map (visible area) using

map.getPixelFromCoordinate(coordinate).

this function always returns null (testing):

center = map.getView().getCenter();
px = map.getPixelFromCoordinate(center);
alert(JSON.stringify(px));

What i'm doing wrong or what i didn't understand properly?

I'd be careful with this. You might get wrong results, e.g. when the map does not have the final layout yet. It is better to wait with the first coordinate to pixel conversion until the map is rendered. You do not need a timeout for this, we have the 'postrender' event on ol.Map. So in your initialisation code, you could do something like this:

map.once('postrender', function() {
  // safe to call map.getPixelFromCoordinate from now on

Source: github.com/openlayers/ol3/issues/5456

I hope this helps.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.