-1

I am using openlayers 5 along with angular6.

I have created a simple form that user can give a X and Y of EPSG3857 coordinates, submit and a point will be rendered there and the map will zoom there. I am using a vector layer for that.

I want to check if the XY are valid EPSG3857 coordinates somehow.

I tried using a try/catch block, but this works the second time I submit the form.

I put some crazy XY like 6.34 2401111118651.91, I submit, the point is created and the map zooms in nowhere, in a white background. I have to submit the same form a second time, for the try/catch to actually catch the error.

How can I fix that and check the coords right away? This is the code I wrote

  coordsSubmitted(){
    let point = [this.coordsForm.controls.coordsx.value, this.coordsForm.controls.coordsy.value];          
    try {
      this.coordFeature.setGeometry(new Point(point));  
      this.coordsource.addFeatures([this.coordFeature]) ;  
      this.olmap.getView().animate({
        center: point,
        duration: 2500,
        zoom:10
      });     
    } catch (error) {
      this.coordsFormErrorMsg = 'Error. Only EPSG 3857 coords.';
    }       
  }

Thanks

slevin
  • 4,166
  • 20
  • 69
  • 129

1 Answers1

1

You can check that your inputs are within the projection bounds. The acceptable values for this projection are:

Min X: -20026376.39
Min Y: -20048966.10
Max X: 20026376.39
Max Y: 20048966.10

You could restrict these limits to the area you are mapping.

JGH
  • 15,928
  • 4
  • 31
  • 48