5

I have a google maps implementation where I use map tiles which are located online. But if I go outside the tile area it throws me errors because it cant find that images.. How do I make an if statement to return null when they don't exist?

What I was thinking was about this because I include jquery:

$.get(url)
.done(function() { 
  // exists code
  return `http://www.blablabla.com/grey-tiles/${zoom}/${coord.x}/${coord.y}.png`
}).fail(function() { 
    // not exists code
   return null;
})

but this is not working for me this is the constructor of my component:

 constructor(){
this.imageMapOptions = {
  getTileUrl: (coord: ImageMapTypeCoord, zoom: number) => {
     return `http://www.blablabla.com/grey-tiles/${zoom}/${coord.x}/${coord.y}.png`
  },
   tileSize: { height: 256, width: 256 },
   maxZoom: 18,
   minZoom: 16,
   radius: 1738000,
   name: 'Beeksebergen'
 };    
}
1
  • You can not return from an Asynchronous call. Commented Jun 8, 2016 at 13:08

1 Answer 1

2

Something like this might work:

var url = 'https://example.com/image.png';
return new Promise((resolve, reject) => {
  return $('<img>')
    .on('error', reject.bind(null, url))
    .on("load", resolve.bind(null, url))
    .appendTo(document.body)
    .css({ "position": "absolute", top: -9999, left: -9999 })
    .on("error load", $.fn.remove)
    .attr("src", url);
});
Sign up to request clarification or add additional context in comments.

4 Comments

how do I implement this in the constructor function?
@Beginnerprogrammer The thing is that you would need to return a promise, and then use getTileUrl().done(url => ...). Since checking the url is asynchronous.
I do not understand what you mean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.