Search code examples
javapolygonjmapviewer

coordinates point is in jmapviewer polygon?


I'm making a program in Java that displays a map using JMapViewer and draws a static polygon. When the user clicks in the map the program say "the point where you have clicked is in polygon" or "not in polygon". How I can do for calculate when the point is in polygon or not? Is there any ready method/class to do that?

Thank you, Luca


Solution

  • In outline,

    • Construct a MapPolygon as shown here.

    • Detect mouse clicks using a custom DefaultMapController as shown here.

    • In mouseClicked(), construct a java.awt.Polygon by iterating through the List<ICoordinate> returned by MapPolygon::getPoints; use the JMapViewer::getPosition method to convert each ICoordinate to a Point.

    • Use Polygon::contains to determine whether the mouse coordinates are inside this Polygon.

    image