├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── doc │ └── app.png └── workflows │ ├── github-action-build.yml │ └── jekyll-gh-pages.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── pom.xml ├── settings.xml └── src ├── main ├── .DS_Store ├── java │ └── io │ │ └── github │ │ └── makbn │ │ └── jlmap │ │ ├── JLMapCallbackHandler.java │ │ ├── JLMapController.java │ │ ├── JLMapView.java │ │ ├── JLProperties.java │ │ ├── exception │ │ ├── JLException.java │ │ ├── JLGeoJsonParserException.java │ │ └── JLMapNotReadyException.java │ │ ├── geojson │ │ ├── JLGeoJsonContent.java │ │ ├── JLGeoJsonFile.java │ │ ├── JLGeoJsonObject.java │ │ ├── JLGeoJsonSource.java │ │ └── JLGeoJsonURL.java │ │ ├── layer │ │ ├── JLControlLayer.java │ │ ├── JLGeoJsonLayer.java │ │ ├── JLLayer.java │ │ ├── JLUiLayer.java │ │ ├── JLVectorLayer.java │ │ └── leaflet │ │ │ ├── LeafletControlLayerInt.java │ │ │ ├── LeafletGeoJsonLayerInt.java │ │ │ ├── LeafletUILayerInt.java │ │ │ └── LeafletVectorLayerInt.java │ │ ├── listener │ │ ├── OnJLMapViewListener.java │ │ ├── OnJLObjectActionListener.java │ │ └── event │ │ │ ├── ClickEvent.java │ │ │ ├── Event.java │ │ │ ├── MoveEvent.java │ │ │ └── ZoomEvent.java │ │ └── model │ │ ├── JLBounds.java │ │ ├── JLCircle.java │ │ ├── JLCircleMarker.java │ │ ├── JLLatLng.java │ │ ├── JLMapOption.java │ │ ├── JLMarker.java │ │ ├── JLMultiPolyline.java │ │ ├── JLObject.java │ │ ├── JLOptions.java │ │ ├── JLPolygon.java │ │ ├── JLPolyline.java │ │ └── JLPopup.java └── resources │ ├── .DS_Store │ ├── index.html │ └── log4j2.xml └── test └── java └── io └── github └── makbn └── jlmap ├── Leaflet.java └── model ├── JLBoundsTest.java ├── JLLatLngTest.java ├── JLMarkerTest.java └── ModelTest.java /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/doc/app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/makbn/java_leaflet/5026f523a1ceb4e34d46e4ba9efbe40f90780aa6/.github/doc/app.png -------------------------------------------------------------------------------- /.github/workflows/github-action-build.yml: -------------------------------------------------------------------------------- 1 | name: Java Leaflet Build 2 | # 3 | on: [ push ] 4 | # 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | # This step uses the `actions/checkout` action to download a copy of your repository on the runner. 10 | - uses: actions/checkout@v3 11 | - name: Set up JDK 17 12 | uses: actions/setup-java@v3 13 | with: 14 | java-version: '17' 15 | distribution: 'corretto' 16 | 17 | - name: Build with Maven 18 | run: mvn --batch-mode --update-snapshots package 19 | 20 | publish: 21 | needs: [ "build" ] 22 | if: contains(github.ref_name, 'release/') 23 | permissions: 24 | contents: read 25 | packages: write 26 | runs-on: ubuntu-latest 27 | steps: 28 | - uses: actions/checkout@v3 29 | - uses: actions/setup-java@v3 30 | with: 31 | java-version: '17' 32 | distribution: 'corretto' 33 | 34 | - name: Deploy to GitHub Packages 35 | env: 36 | GITHUB_USERNAME: makbn 37 | GITHUB_ACTOR: makbn 38 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 39 | run: mvn --settings settings.xml deploy 40 | 41 | -------------------------------------------------------------------------------- /.github/workflows/jekyll-gh-pages.yml: -------------------------------------------------------------------------------- 1 | # Sample workflow for building and deploying a Jekyll site to GitHub Pages 2 | name: Deploy Jekyll with GitHub Pages dependencies preinstalled 3 | 4 | on: 5 | # Runs on pushes targeting the default branch 6 | push: 7 | branches: ["master"] 8 | 9 | # Allows you to run this workflow manually from the Actions tab 10 | workflow_dispatch: 11 | 12 | # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages 13 | permissions: 14 | contents: read 15 | pages: write 16 | id-token: write 17 | 18 | # Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. 19 | # However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. 20 | concurrency: 21 | group: "pages" 22 | cancel-in-progress: false 23 | 24 | jobs: 25 | # Build job 26 | build: 27 | runs-on: ubuntu-latest 28 | steps: 29 | - name: Checkout 30 | uses: actions/checkout@v4 31 | - name: Setup Pages 32 | uses: actions/configure-pages@v4 33 | - name: Build with Jekyll 34 | uses: actions/jekyll-build-pages@v1 35 | with: 36 | source: ./ 37 | destination: ./_site 38 | - name: Upload artifact 39 | uses: actions/upload-pages-artifact@v3 40 | 41 | # Deployment job 42 | deploy: 43 | environment: 44 | name: github-pages 45 | url: ${{ steps.deployment.outputs.page_url }} 46 | runs-on: ubuntu-latest 47 | needs: build 48 | steps: 49 | - name: Deploy to GitHub Pages 50 | id: deployment 51 | uses: actions/deploy-pages@v4 52 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | out 3 | .idea 4 | *.iml 5 | application.properties 6 | /src/main/java/META-INF/ 7 | /src/main/java/io/github/makbn/jlmap/META-INF/ 8 | /src/main/resources/final.geo.json 9 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at mehdi74akbarian@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Java Leaflet 2 | Java wrapper for Leaflet, JavaScript library for mobile-friendly interactive maps. 3 | 4 | * Current version: **v1.9.4** 5 | * Previous version: [v1..6.0](https://github.com/makbn/java_leaflet/tree/release/1.6.0) 6 | 7 | Project Source Code: https://github.com/makbn/java_leaflet 8 | 9 | ![Java-Leaflet Test](https://github.com/makbn/java_leaflet/blob/master/.github/doc/app.png?raw=true) 10 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fmakbn%2Fjava_leaflet.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fmakbn%2Fjava_leaflet?ref=badge_shield) 11 | 12 | > Leaflet is the leading open-source JavaScript library for mobile-friendly interactive maps. Weighing just about 38 KB of JS, it has all the mapping features most > developers ever need. 13 | > Leaflet is designed with simplicity, performance and usability in mind. It works efficiently across all major desktop and mobile platforms, can be extended with > lots of plugins, has a beautiful, easy to use and well-documented API and a simple, readable source code that is a joy to contribute to. 14 | 15 | 16 | ## Getting start 17 | 18 | Since you are working on JavaFX application you know you need to have the JavaFX runtime. 19 | Read more about installing JavaFx: 20 | * https://openjfx.io/openjfx-docs/#introduction 21 | 22 | To run the [Leaflet example](src/test/java/io/github/makbn/jlmap/Leaflet.java) class you need to set the module path 23 | as VM options: 24 | 25 | ```java 26 | --module-path [PATH_TO_JAVA_FX_LIB_FOLDER] 27 | --add-exports javafx.web/com.sun.javafx.webkit=ALL-UNNAMED 28 | --add-modules=javafx.graphics,javafx.web 29 | ``` 30 | 31 | There is an issue with JavaFX v17.X javafx.web engine and OSM tiles, I tried JavaFX v19.0.2.1 and it works fine! 32 | 33 | First, you need to initialize an instance of `JLMapView`: 34 | 35 | ```java 36 | final JLMapView map = JLMapView 37 | .builder() 38 | .mapType(JLProperties.MapType.OSM_MAPNIK) 39 | .showZoomController(true) 40 | .startCoordinate(JLLatLng.builder() 41 | .lat(51.044) 42 | .lng(114.07) 43 | .build()) 44 | .build(); 45 | 46 | ``` 47 | 48 | Based on Leaflet JS, you can interact with map in different layers. in this project, you can access different functions with this layer: 49 | * `map` for direct changes on map 50 | * `map.getUiLayer()` for changes on UI layer like markers. 51 | * `map.getVectorLayer()` represents the Vector layer on Leaflet map. 52 | * `map.getControlLayer()` represents the control layer for setting the zoom level. 53 | * `map.getGeoJsonLayer()` represents the GeoJson layer. 54 | 55 | 56 | ### Map functions 57 | 58 | Some map view functionalities are available in map layer like `setView` or `setMapListener` as a callback for map events: 59 | 60 | * Change the current coordinate of map center: 61 | 62 | ```java 63 | map.setView(JLLatLng.builder() 64 | .lng(10) 65 | .lat(10) 66 | .build()); 67 | ``` 68 | 69 | * Add a listener for map events: 70 | 71 | ```java 72 | map.setMapListener(new OnJLMapViewListener() { 73 | @Override 74 | public void mapLoadedSuccessfully(JLMapView mapView) { 75 | 76 | } 77 | 78 | @Override 79 | public void mapFailed() { 80 | log.error("map failed!"); 81 | } 82 | 83 | @Override 84 | public void onAction(Event event) { 85 | if (event instanceof MoveEvent moveEvent) { 86 | log.info("move event: " + moveEvent.action() + " c:" + moveEvent.center() 87 | + " \t bounds:" + moveEvent.bounds() + "\t z:" + moveEvent.zoomLevel()); 88 | } else if (event instanceof ClickEvent clickEvent) { 89 | log.info("click event: " + clickEvent.center()); 90 | map.getUiLayer().addPopup(clickEvent.center(), "New Click Event!", JLOptions.builder() 91 | .closeButton(false) 92 | .autoClose(false).build()); 93 | } else if (event instanceof ZoomEvent zoomEvent) { 94 | log.info("zoom event: " + zoomEvent.zoomLevel()); 95 | } 96 | } 97 | } 98 | ``` 99 | 100 | Read more about map events from `OnJLMapViewListener.Action`. 101 | 102 | ### UI Layer 103 | 104 | Layer for adding/removing markers and popup. you can access UI layer from `map.getUiLayer()`. As an example: 105 | 106 | ```java 107 | map.getUiLayer() 108 | .addMarker(JLLatLng.builder() 109 | .lat(35.63) 110 | .lng(51.45) 111 | .build(), "Tehran", true) 112 | .setOnActionListener(getListener()); 113 | ``` 114 | 115 | Controlling map's zoom level: 116 | ```java 117 | // map zoom functionalities 118 | map.getControlLayer().setZoom(5); 119 | map.getControlLayer().zoomIn(2); 120 | map.getControlLayer().zoomOut(1); 121 | ``` 122 | 123 | you can add a listener for some Objects on the map: 124 | 125 | ```java 126 | marker.setOnActionListener(new OnJLObjectActionListener() { 127 | @Override 128 | public void click(JLMarker object, Action action) { 129 | System.out.println("object click listener for marker:" + object); 130 | } 131 | 132 | @Override 133 | public void move(JLMarker object, Action action) { 134 | System.out.println("object move listener for marker:" + object); 135 | } 136 | }); 137 | ``` 138 | 139 | 140 | ### Vector layer 141 | 142 | Represents the Vector layer for Leaflet map. Poly lines, Polygons, and shapes are available in this layer. 143 | 144 | ```java 145 | map.getVectorLayer() 146 | .addCircleMarker(JLLatLng.builder() 147 | .lat(35.63) 148 | .lng(40.45) 149 | .build() 150 | ); 151 | ``` 152 | 153 | ### GeoJson layer 154 | Represents the GeoJson layer for Leaflet map and defines methods for adding and managing 155 | GeoJSON data layers in a Leaflet map. 156 | ```java 157 | JLGeoJsonObject geoJsonObject = map.getGeoJsonLayer() 158 | .addFromUrl("https://pkgstore.datahub.io/examples/geojson-tutorial/example/data/db696b3bf628d9a273ca9907adcea5c9/example.geojson"); 159 | 160 | ``` 161 | You can add GeoJson data from three different sources: 162 | * From a file using `map.getGeoJsonLayer().addFromFile([FILE])` 163 | * From a URL using `map.getGeoJsonLayer().addFromUrl([URL])` 164 | * From a GeoJson content `map.getGeoJsonLayer().addFromContent([CONTENT])` 165 | ### Styling 166 | 167 | You can pass `JLOptions` to each method for changing the default style! 168 | 169 | ```java 170 | map.getVectorLayer() 171 | .addCircle(JLLatLng.builder() 172 | .lat(35.63) 173 | .lng(51.45) 174 | .build(), 30000, 175 | 176 | JLOptions.builder() 177 | .color(Color.BLACK) 178 | .build() 179 | ); 180 | ``` 181 | 182 | For the map itself, you can choose between themes available in `JLProperties.MapType` class. The `JLProperties.MapType.OSM_MAPNIK` is available to be used without any access key but for the rest of them, you need to define your own map using `JLProperties.MapType` and passing proper list of key-values containing all the necessary access keys. 183 | ```java 184 | JLProperties.MapType myMapType = new JLProperties.MapType("HEREv3.terrainDay", 185 | Set.of(new JLMapOption.Parameter("apiKey", ""))); 186 | 187 | JLMapView map = JLMapView 188 | .builder() 189 | .mapType(myMapType) 190 | .showZoomController(true) 191 | .startCoordinate(JLLatLng.builder() 192 | .lat(51.044) 193 | .lng(114.07) 194 | .build()) 195 | .build(); 196 | ``` 197 | 198 | Read more: 199 | 200 | * https://github.com/leaflet-extras/leaflet-providers! 201 | 202 | 203 | ## TODO 204 | 205 | - [X] Adding GeoJson Support 206 | - [ ] Adding better support for Map providers 207 | - [ ] Adding SVG support 208 | - [ ] Adding animation support 209 | - [ ] Separating JS and HTML 210 | - [ ] Publishing package on GitHub 211 | 212 | **Disclaimer**: I've implemented this project for one of my academic paper in the area of geo-visualization. So, im not contributing actively! One more thing, I'm not a Javascript developer! 213 | 214 | 215 | 216 | 217 | ## License 218 | [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fmakbn%2Fjava_leaflet.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fmakbn%2Fjava_leaflet?ref=badge_large) 219 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.makbn 8 | jlmap 9 | 1.9.4 10 | jar 11 | Java Leaflet (JLeaflet) 12 | 13 | 14 | 15 | Mehdi Akbarian Rastaghi 16 | makbn 17 | mehdi74akbarian@gmail.com 18 | 19 | 20 | 21 | 22 | 23 | GNU General Public License, Version 3.0 24 | https://www.gnu.org/licenses/gpl-3.0.html 25 | repo 26 | 27 | 28 | 29 | 30 | 31 | 32 | 3.11.0 33 | org.apache.maven.plugins 34 | maven-compiler-plugin 35 | 36 | 17 37 | 17 38 | 39 | 40 | 41 | org.openjfx 42 | javafx-maven-plugin 43 | 0.0.8 44 | 45 | 46 | maven-assembly-plugin 47 | 48 | 49 | package 50 | 51 | single 52 | 53 | 54 | 55 | 56 | 57 | 58 | true 59 | io.github.makbn.jlmap.Leaflet 60 | 61 | 62 | 63 | jar-with-dependencies 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | org.junit 74 | junit-bom 75 | 5.10.0 76 | pom 77 | import 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | org.openjfx 86 | javafx-controls 87 | 19.0.2.1 88 | 89 | 90 | org.openjfx 91 | javafx-base 92 | 19.0.2.1 93 | 94 | 95 | org.openjfx 96 | javafx-swing 97 | 19.0.2.1 98 | 99 | 100 | org.openjfx 101 | javafx-web 102 | 19.0.2.1 103 | 104 | 105 | org.openjfx 106 | javafx-graphics 107 | 17 108 | 109 | 110 | org.projectlombok 111 | lombok 112 | 1.18.28 113 | provided 114 | 115 | 116 | org.apache.logging.log4j 117 | log4j-api 118 | 2.20.0 119 | 120 | 121 | org.apache.logging.log4j 122 | log4j-core 123 | 2.20.0 124 | 125 | 126 | com.google.code.gson 127 | gson 128 | 2.10.1 129 | 130 | 131 | com.fasterxml.jackson.core 132 | jackson-databind 133 | 2.15.2 134 | 135 | 136 | de.grundid.opendatalab 137 | geojson-jackson 138 | 1.14 139 | 140 | 141 | com.fasterxml.jackson.core 142 | jackson-databind 143 | 144 | 145 | 146 | 147 | org.jetbrains 148 | annotations 149 | 24.0.1 150 | compile 151 | 152 | 153 | org.junit.jupiter 154 | junit-jupiter 155 | test 156 | 157 | 158 | 159 | 160 | 161 | github 162 | GitHub makbn Apache Maven Packages 163 | https://maven.pkg.github.com/makbn/java_leaflet 164 | 165 | 166 | -------------------------------------------------------------------------------- /settings.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | github 6 | 7 | 8 | 9 | 10 | github 11 | 12 | 13 | github 14 | GitHub makbn Apache Maven Packages 15 | https://maven.pkg.github.com/makbn/java_leaflet 16 | 17 | true 18 | 19 | 20 | true 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | github 30 | ${env.GITHUB_ACTOR} 31 | ${env.GITHUB_TOKEN} 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/main/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/makbn/java_leaflet/5026f523a1ceb4e34d46e4ba9efbe40f90780aa6/src/main/.DS_Store -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/JLMapCallbackHandler.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap; 2 | 3 | import com.google.gson.Gson; 4 | import io.github.makbn.jlmap.listener.OnJLMapViewListener; 5 | import io.github.makbn.jlmap.listener.OnJLObjectActionListener; 6 | import io.github.makbn.jlmap.listener.event.ClickEvent; 7 | import io.github.makbn.jlmap.listener.event.MoveEvent; 8 | import io.github.makbn.jlmap.listener.event.ZoomEvent; 9 | import io.github.makbn.jlmap.model.*; 10 | import lombok.AccessLevel; 11 | import lombok.experimental.FieldDefaults; 12 | import lombok.extern.log4j.Log4j2; 13 | 14 | import java.io.Serializable; 15 | import java.util.HashMap; 16 | 17 | /** 18 | * @author Mehdi Akbarian Rastaghi (@makbn) 19 | */ 20 | @Log4j2 21 | @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) 22 | public class JLMapCallbackHandler implements Serializable { 23 | 24 | transient JLMapView mapView; 25 | transient HashMap>, HashMap>> jlObjects; 26 | transient Gson gson; 27 | HashMap>[]> classMap; 28 | 29 | public JLMapCallbackHandler(JLMapView mapView) { 30 | this.mapView = mapView; 31 | this.jlObjects = new HashMap<>(); 32 | this.gson = new Gson(); 33 | this.classMap = new HashMap<>(); 34 | initClassMap(); 35 | } 36 | @SuppressWarnings("unchecked") 37 | private void initClassMap() { 38 | 39 | classMap.put("marker", new Class[]{JLMarker.class}); 40 | classMap.put("marker_circle", new Class[]{JLCircleMarker.class}); 41 | classMap.put("polyline", new Class[]{JLPolyline.class, JLMultiPolyline.class}); 42 | classMap.put("polygon", new Class[]{JLPolygon.class}); 43 | } 44 | 45 | /** 46 | * @param functionName name of source function from js 47 | * @param param1 name of object class 48 | * @param param2 id of object 49 | * @param param3 additional param 50 | * @param param4 additional param 51 | * @param param5 additional param 52 | */ 53 | @SuppressWarnings("unchecked") 54 | public void functionCalled(String functionName, Object param1, Object param2, 55 | Object param3, Object param4, Object param5) { 56 | log.debug(String.format("function: %s \tparam1: %s \tparam2: %s " + 57 | "\tparam3: %s param4: %s \tparam5: %s%n" 58 | , functionName, param1, param2, param3, param4, param5)); 59 | try { 60 | //get target class of Leaflet layer in JL Application 61 | Class[] targetClasses = classMap.get(param1); 62 | 63 | //function called by an known class 64 | if (targetClasses != null) { 65 | //one Leaflet class may map to multiple class in JL Application 66 | // like ployLine mapped to JLPolyline and JLMultiPolyline 67 | for (Class targetClass : targetClasses) { 68 | if (targetClass != null) { 69 | //search for the other JLObject class if available 70 | if (!jlObjects.containsKey(targetClass)) 71 | break; 72 | 73 | JLObject jlObject = jlObjects.get(targetClass) 74 | .get(Integer.parseInt(String.valueOf(param2))); 75 | 76 | //search for the other JLObject object if available 77 | if (jlObject == null) 78 | break; 79 | 80 | if (jlObject.getOnActionListener() == null) 81 | return; 82 | 83 | //call listener and exit loop 84 | if (callListenerOnObject(functionName, 85 | (JLObject>) jlObject, param1, 86 | param2, param3, param4, param5)) 87 | return; 88 | } 89 | } 90 | } else if (param1.equals("main_map") 91 | && mapView.getMapListener().isPresent()) { 92 | switch (functionName) { 93 | case "move" -> mapView.getMapListener() 94 | .get() 95 | .onAction(new MoveEvent(OnJLMapViewListener.Action.MOVE, 96 | gson.fromJson(String.valueOf(param4), JLLatLng.class), 97 | gson.fromJson(String.valueOf(param5), JLBounds.class), 98 | Integer.parseInt(String.valueOf(param3)))); 99 | case "movestart" -> mapView.getMapListener() 100 | .get() 101 | .onAction(new MoveEvent(OnJLMapViewListener.Action.MOVE_START, 102 | gson.fromJson(String.valueOf(param4), JLLatLng.class), 103 | gson.fromJson(String.valueOf(param5), JLBounds.class), 104 | Integer.parseInt(String.valueOf(param3)))); 105 | case "moveend" -> mapView.getMapListener() 106 | .get() 107 | .onAction(new MoveEvent(OnJLMapViewListener.Action.MOVE_END, 108 | gson.fromJson(String.valueOf(param4), JLLatLng.class), 109 | gson.fromJson(String.valueOf(param5), JLBounds.class), 110 | Integer.parseInt(String.valueOf(param3)))); 111 | case "click" -> mapView.getMapListener() 112 | .get() 113 | .onAction(new ClickEvent(gson.fromJson(String.valueOf(param3), 114 | JLLatLng.class))); 115 | 116 | case "zoom" -> mapView.getMapListener() 117 | .get() 118 | .onAction(new ZoomEvent(OnJLMapViewListener.Action.ZOOM, 119 | Integer.parseInt(String.valueOf(param3)))); 120 | default -> log.error(functionName + " not implemented!"); 121 | } 122 | } 123 | } catch (Exception e) { 124 | log.error(e); 125 | } 126 | } 127 | 128 | private boolean callListenerOnObject( 129 | String functionName, JLObject> jlObject, Object... params) { 130 | switch (functionName) { 131 | case "move" -> { 132 | jlObject.getOnActionListener() 133 | .move(jlObject, OnJLObjectActionListener.Action.MOVE); 134 | return true; 135 | } 136 | case "movestart" -> { 137 | jlObject.getOnActionListener() 138 | .move(jlObject, OnJLObjectActionListener.Action.MOVE_START); 139 | return true; 140 | } 141 | case "moveend" -> { 142 | //update coordinate of the JLObject 143 | jlObject.update("moveend", gson.fromJson(String.valueOf(params[3]), JLLatLng.class)); 144 | jlObject.getOnActionListener() 145 | .move(jlObject, OnJLObjectActionListener.Action.MOVE_END); 146 | return true; 147 | } 148 | case "click" -> { 149 | jlObject.getOnActionListener() 150 | .click(jlObject, OnJLObjectActionListener.Action.CLICK); 151 | return true; 152 | } 153 | default -> log.error(functionName + " not implemented!"); 154 | } 155 | return false; 156 | } 157 | 158 | @SuppressWarnings("unchecked") 159 | public void addJLObject(JLObject object) { 160 | if (jlObjects.containsKey(object.getClass())) { 161 | jlObjects.get(object.getClass()) 162 | .put(object.getId(), object); 163 | } else { 164 | HashMap> map = new HashMap<>(); 165 | map.put(object.getId(), object); 166 | jlObjects.put((Class>) object.getClass(), map); 167 | } 168 | } 169 | 170 | public void remove(Class> targetClass, int id) { 171 | if (!jlObjects.containsKey(targetClass)) 172 | return; 173 | JLObject object = jlObjects.get(targetClass).remove(id); 174 | if (object != null) 175 | log.error(targetClass.getSimpleName() + " id:" + object.getId() + " removed"); 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/JLMapController.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap; 2 | 3 | import io.github.makbn.jlmap.exception.JLMapNotReadyException; 4 | import io.github.makbn.jlmap.layer.*; 5 | import io.github.makbn.jlmap.model.JLLatLng; 6 | import io.github.makbn.jlmap.model.JLMapOption; 7 | import javafx.concurrent.Worker; 8 | import javafx.scene.layout.AnchorPane; 9 | import javafx.scene.web.WebView; 10 | import lombok.AccessLevel; 11 | import lombok.NonNull; 12 | import lombok.experimental.FieldDefaults; 13 | 14 | import java.util.HashMap; 15 | 16 | /** 17 | * @author Mehdi Akbarian Rastaghi (@makbn) 18 | */ 19 | @FieldDefaults(makeFinal = true, level = AccessLevel.PROTECTED) 20 | abstract class JLMapController extends AnchorPane { 21 | JLMapOption mapOption; 22 | 23 | JLMapController(@NonNull JLMapOption mapOption) { 24 | this.mapOption = mapOption; 25 | } 26 | 27 | protected abstract WebView getWebView(); 28 | 29 | protected abstract void addControllerToDocument(); 30 | 31 | protected abstract HashMap, JLLayer> getLayers(); 32 | 33 | /** 34 | * handle all functions for add/remove layers from UI layer 35 | * @return current instance of {{@link JLUiLayer}} 36 | */ 37 | public JLUiLayer getUiLayer(){ 38 | checkMapState(); 39 | return (JLUiLayer) getLayers().get(JLUiLayer.class); 40 | } 41 | 42 | /** 43 | * handle all functions for add/remove layers from Vector layer 44 | * @return current instance of {{@link JLVectorLayer}} 45 | */ 46 | public JLVectorLayer getVectorLayer(){ 47 | checkMapState(); 48 | return (JLVectorLayer) getLayers().get(JLVectorLayer.class); 49 | } 50 | 51 | public JLControlLayer getControlLayer() { 52 | checkMapState(); 53 | return (JLControlLayer) getLayers().get(JLControlLayer.class); 54 | } 55 | 56 | public JLGeoJsonLayer getGeoJsonLayer() { 57 | checkMapState(); 58 | return (JLGeoJsonLayer) getLayers().get(JLGeoJsonLayer.class); 59 | } 60 | 61 | /** 62 | * Sets the view of the map (geographical center). 63 | * @param latLng Represents a geographical point with a certain latitude 64 | * and longitude. 65 | */ 66 | public void setView(JLLatLng latLng){ 67 | checkMapState(); 68 | getWebView().getEngine() 69 | .executeScript(String.format("jlmap.panTo([%f, %f]);", 70 | latLng.getLat(), latLng.getLng())); 71 | } 72 | 73 | /** 74 | * Sets the view of the map (geographical center) with animation duration. 75 | * @param duration Represents the duration of transition animation. 76 | * @param latLng Represents a geographical point with a certain latitude 77 | * and longitude. 78 | */ 79 | public void setView(JLLatLng latLng, int duration){ 80 | checkMapState(); 81 | getWebView().getEngine() 82 | .executeScript(String.format("setLatLng(%f, %f,%d);", 83 | latLng.getLat(), latLng.getLng(), duration)); 84 | } 85 | 86 | private void checkMapState() { 87 | if (getWebView() == null || 88 | getWebView().getEngine() 89 | .getLoadWorker().getState() != Worker.State.SUCCEEDED) { 90 | throw JLMapNotReadyException.builder().build(); 91 | } 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/JLMapView.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap; 2 | 3 | import com.sun.javafx.webkit.WebConsoleListener; 4 | import io.github.makbn.jlmap.layer.*; 5 | import io.github.makbn.jlmap.listener.OnJLMapViewListener; 6 | import io.github.makbn.jlmap.model.JLLatLng; 7 | import io.github.makbn.jlmap.model.JLMapOption; 8 | import javafx.animation.Interpolator; 9 | import javafx.animation.Transition; 10 | import javafx.concurrent.Worker; 11 | import javafx.geometry.Insets; 12 | import javafx.scene.effect.GaussianBlur; 13 | import javafx.scene.layout.Background; 14 | import javafx.scene.layout.BackgroundFill; 15 | import javafx.scene.layout.CornerRadii; 16 | import javafx.scene.paint.Color; 17 | import javafx.scene.web.WebEngine; 18 | import javafx.scene.web.WebView; 19 | import javafx.util.Duration; 20 | import lombok.AccessLevel; 21 | import lombok.Builder; 22 | import lombok.Getter; 23 | import lombok.NonNull; 24 | import lombok.experimental.FieldDefaults; 25 | import lombok.experimental.NonFinal; 26 | import lombok.extern.log4j.Log4j2; 27 | import netscape.javascript.JSObject; 28 | import org.jetbrains.annotations.Nullable; 29 | 30 | import java.awt.*; 31 | import java.io.*; 32 | import java.net.URISyntaxException; 33 | import java.net.URL; 34 | import java.nio.file.Files; 35 | import java.util.HashMap; 36 | import java.util.Objects; 37 | import java.util.Optional; 38 | import java.util.Set; 39 | import java.util.stream.Collectors; 40 | 41 | /** 42 | * @author Mehdi Akbarian Rastaghi (@makbn) 43 | */ 44 | @Log4j2 45 | @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) 46 | public class JLMapView extends JLMapController { 47 | 48 | @Builder 49 | public JLMapView(@NonNull JLProperties.MapType mapType, 50 | @NonNull JLLatLng startCoordinate, boolean showZoomController) { 51 | super(JLMapOption.builder() 52 | .startCoordinate(startCoordinate) 53 | .mapType(mapType) 54 | .additionalParameter(Set.of(new JLMapOption.Parameter("zoomControl", 55 | Objects.toString(showZoomController)))) 56 | .build()); 57 | this.webView = new WebView(); 58 | this.jlMapCallbackHandler = new JLMapCallbackHandler(this); 59 | initialize(); 60 | } 61 | 62 | @Getter 63 | WebView webView; 64 | JLMapCallbackHandler jlMapCallbackHandler; 65 | @NonFinal 66 | HashMap, JLLayer> layers; 67 | @NonFinal 68 | boolean controllerAdded = false; 69 | @NonFinal 70 | @Nullable 71 | OnJLMapViewListener mapListener; 72 | 73 | private void removeMapBlur() { 74 | Transition gt = new MapTransition(webView); 75 | gt.play(); 76 | } 77 | 78 | private void initialize() { 79 | 80 | webView.getEngine().onStatusChangedProperty().addListener((observable, oldValue, newValue) 81 | -> log.debug(String.format("Old Value: %s\tNew Value: %s", oldValue, newValue))); 82 | webView.getEngine().onErrorProperty().addListener((observable, oldValue, newValue) 83 | -> log.debug(String.format("Old Value: %s\tNew Value: %s", oldValue, newValue))); 84 | webView.getEngine().getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> { 85 | checkForBrowsing(webView.getEngine()); 86 | if (newValue == Worker.State.FAILED) { 87 | log.info("failed to load!"); 88 | } else if (newValue == Worker.State.SUCCEEDED) { 89 | removeMapBlur(); 90 | webView.getEngine().executeScript("removeNativeAttr()"); 91 | addControllerToDocument(); 92 | 93 | if (mapListener != null) { 94 | mapListener.mapLoadedSuccessfully(this); 95 | } 96 | 97 | } else { 98 | setBlurEffectForMap(); 99 | } 100 | }); 101 | 102 | WebConsoleListener.setDefaultListener((view, message, lineNumber, sourceId) 103 | -> log.error(String.format("sid: %s ln: %d m:%s", sourceId, lineNumber, message))); 104 | 105 | File index = null; 106 | try (InputStream in = getClass().getResourceAsStream("/index.html")) { 107 | BufferedReader reader = new BufferedReader(new InputStreamReader(Objects.requireNonNull(in))); 108 | index = File.createTempFile("jlmapindex", ".html"); 109 | Files.write(index.toPath(), reader.lines().collect(Collectors.joining("\n")).getBytes()); 110 | } catch (IOException e) { 111 | log.error(e); 112 | } 113 | 114 | webView.getEngine() 115 | .load(String.format("file:%s%s", Objects.requireNonNull(index).getAbsolutePath(), mapOption.toQueryString())); 116 | 117 | setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY))); 118 | getChildren().add(webView); 119 | customizeWebviewStyles(); 120 | } 121 | 122 | private void checkForBrowsing(WebEngine engine) { 123 | String address = 124 | engine.getLoadWorker().getMessage().trim(); 125 | log.debug("link: " + address); 126 | if (address.contains("http://") || address.contains("https://")) { 127 | engine.getLoadWorker().cancel(); 128 | try { 129 | String os = System.getProperty("os.name", "generic"); 130 | if (os.toLowerCase().contains("mac")) { 131 | Runtime.getRuntime().exec("open " + address); 132 | } else if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { 133 | Desktop.getDesktop().browse(new URL(address).toURI()); 134 | } else { 135 | Runtime.getRuntime().exec("xdg-open " + address); 136 | } 137 | } catch (IOException | URISyntaxException e) { 138 | log.debug(e); 139 | } 140 | } 141 | } 142 | 143 | @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) 144 | private static class MapTransition extends Transition { 145 | WebView webView; 146 | 147 | public MapTransition(WebView webView) { 148 | this.webView = webView; 149 | setCycleDuration(Duration.millis(2000)); 150 | setInterpolator(Interpolator.EASE_IN); 151 | } 152 | 153 | @Override 154 | protected void interpolate(double frac) { 155 | GaussianBlur eff = ((GaussianBlur) webView.getEffect()); 156 | eff.setRadius(JLProperties.START_ANIMATION_RADIUS * (1 - frac)); 157 | } 158 | } 159 | 160 | private void setBlurEffectForMap() { 161 | if (webView.getEffect() == null) { 162 | GaussianBlur gaussianBlur = new GaussianBlur(); 163 | gaussianBlur.setRadius(JLProperties.START_ANIMATION_RADIUS); 164 | webView.setEffect(gaussianBlur); 165 | } 166 | } 167 | 168 | private void customizeWebviewStyles() { 169 | setLeftAnchor(webView, 0.0); 170 | setRightAnchor(webView, 0.0); 171 | setTopAnchor(webView, 0.0); 172 | setBottomAnchor(webView, 0.0); 173 | 174 | setLeftAnchor(this, 0.5); 175 | setRightAnchor(this, 0.5); 176 | setTopAnchor(this, 0.5); 177 | setBottomAnchor(this, 0.5); 178 | } 179 | 180 | @Override 181 | protected HashMap, JLLayer> getLayers() { 182 | if (layers == null) { 183 | layers = new HashMap<>(); 184 | layers.put(JLUiLayer.class, new JLUiLayer(getWebView().getEngine(), jlMapCallbackHandler)); 185 | layers.put(JLVectorLayer.class, new JLVectorLayer(getWebView().getEngine(), jlMapCallbackHandler)); 186 | layers.put(JLControlLayer.class, new JLControlLayer(getWebView().getEngine(), jlMapCallbackHandler)); 187 | layers.put(JLGeoJsonLayer.class, new JLGeoJsonLayer(getWebView().getEngine(), jlMapCallbackHandler)); 188 | } 189 | return layers; 190 | } 191 | 192 | @Override 193 | protected void addControllerToDocument() { 194 | JSObject window = (JSObject) webView.getEngine().executeScript("window"); 195 | if (!controllerAdded) { 196 | window.setMember("app", jlMapCallbackHandler); 197 | log.debug("controller added to js scripts"); 198 | controllerAdded = true; 199 | } 200 | webView.getEngine().setOnError(webErrorEvent -> log.error(webErrorEvent.getMessage())); 201 | } 202 | 203 | public Optional getMapListener() { 204 | return Optional.ofNullable(mapListener); 205 | } 206 | 207 | public void setMapListener(@NonNull OnJLMapViewListener mapListener) { 208 | this.mapListener = mapListener; 209 | } 210 | } -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/JLProperties.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap; 2 | 3 | import io.github.makbn.jlmap.model.JLMapOption; 4 | 5 | import java.util.Collections; 6 | import java.util.Set; 7 | 8 | /** 9 | * @author Mehdi Akbarian Rastaghi (@makbn) 10 | */ 11 | public class JLProperties { 12 | public static final int INIT_MIN_WIDTH = 1024; 13 | public static final int INIT_MIN_HEIGHT = 576; 14 | public static final int EARTH_RADIUS = 6367; 15 | public static final int DEFAULT_CIRCLE_RADIUS = 200; 16 | public static final int DEFAULT_CIRCLE_MARKER_RADIUS = 10; 17 | public static final int INIT_MIN_WIDTH_STAGE = INIT_MIN_WIDTH; 18 | public static final int INIT_MIN_HEIGHT_STAGE = INIT_MIN_HEIGHT; 19 | public static final double START_ANIMATION_RADIUS = 10; 20 | 21 | public record MapType(String name, Set parameters) { 22 | 23 | public MapType(String name) { 24 | this(name, Collections.emptySet()); 25 | } 26 | 27 | public static final MapType OSM_MAPNIK = new MapType("OpenStreetMap.Mapnik"); 28 | public static final MapType OSM_HOT = new MapType("OpenStreetMap.HOT"); 29 | public static final MapType OPEN_TOPO = new MapType("OpenTopoMap"); 30 | 31 | public static MapType getDefault() { 32 | return OSM_MAPNIK; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/exception/JLException.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.exception; 2 | 3 | /** 4 | * Internal JLMap application's Exception base class. 5 | * @author Mehdi Akbarian Rastaghi (@makbn) 6 | */ 7 | 8 | public class JLException extends RuntimeException{ 9 | public JLException(String message) { 10 | super(message); 11 | } 12 | public JLException(Throwable cause) { 13 | super(cause); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/exception/JLGeoJsonParserException.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.exception; 2 | 3 | import lombok.Builder; 4 | 5 | /** 6 | * @author Mehdi Akbarian Rastaghi (@makbn) 7 | */ 8 | public class JLGeoJsonParserException extends JLException { 9 | 10 | @Builder 11 | public JLGeoJsonParserException(String message) { 12 | super(message); 13 | } 14 | 15 | public JLGeoJsonParserException(Throwable cause) { 16 | super(cause); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/exception/JLMapNotReadyException.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.exception; 2 | 3 | import lombok.Builder; 4 | 5 | /** 6 | * JLMap Exception which is thrown when changing the map before it gets ready! 7 | * Leaflet map gets fully ready after the {@link javafx.concurrent.Worker.State} 8 | * of {@link javafx.scene.web.WebEngine} changes to 9 | * {@link javafx.concurrent.Worker.State#SUCCEEDED} 10 | * 11 | * @author Mehdi Akbarian Rastaghi (@makbn) 12 | */ 13 | public class JLMapNotReadyException extends JLException { 14 | private static final String MAP_IS_NOT_READY_YET = "Map is not ready yet!"; 15 | 16 | @Builder 17 | public JLMapNotReadyException() { 18 | super(MAP_IS_NOT_READY_YET); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/geojson/JLGeoJsonContent.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.geojson; 2 | 3 | import com.google.gson.JsonParseException; 4 | import io.github.makbn.jlmap.exception.JLGeoJsonParserException; 5 | import lombok.AccessLevel; 6 | import lombok.experimental.FieldDefaults; 7 | 8 | /** 9 | * @author Mehdi Akbarian Rastaghi (@makbn) 10 | */ 11 | @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) 12 | public class JLGeoJsonContent extends JLGeoJsonSource { 13 | 14 | @Override 15 | public String load(String content) throws JLGeoJsonParserException { 16 | if (content == null || content.isEmpty()) 17 | throw JLGeoJsonParserException.builder() 18 | .message("json is empty!") 19 | .build(); 20 | try { 21 | validateJson(content); 22 | return content; 23 | } catch (JsonParseException e) { 24 | throw new JLGeoJsonParserException(e.getMessage()); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/geojson/JLGeoJsonFile.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.geojson; 2 | 3 | import com.google.gson.JsonParseException; 4 | import io.github.makbn.jlmap.exception.JLGeoJsonParserException; 5 | import lombok.AccessLevel; 6 | import lombok.experimental.FieldDefaults; 7 | 8 | import java.io.File; 9 | import java.io.IOException; 10 | import java.nio.file.Files; 11 | 12 | /** 13 | * @author Mehdi Akbarian Rastaghi (@makbn) 14 | */ 15 | @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) 16 | public class JLGeoJsonFile extends JLGeoJsonSource { 17 | 18 | @Override 19 | public String load(File file) throws JLGeoJsonParserException { 20 | try { 21 | String fileContent = Files.readString(file.toPath()); 22 | validateJson(fileContent); 23 | return fileContent; 24 | } catch (IOException | JsonParseException e) { 25 | throw new JLGeoJsonParserException(e.getMessage()); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/geojson/JLGeoJsonObject.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.geojson; 2 | 3 | import lombok.AccessLevel; 4 | import lombok.Builder; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.experimental.FieldDefaults; 8 | import lombok.experimental.NonFinal; 9 | 10 | /** 11 | * @author Mehdi Akbarian Rastaghi (@makbn) 12 | */ 13 | @Getter 14 | @Setter 15 | @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) 16 | public class JLGeoJsonObject { 17 | @NonFinal 18 | int id; 19 | String geoJsonContent; 20 | 21 | @Builder 22 | public JLGeoJsonObject(int id, String geoJsonContent) { 23 | this.id = id; 24 | this.geoJsonContent = geoJsonContent; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/geojson/JLGeoJsonSource.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.geojson; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.JsonSyntaxException; 5 | import io.github.makbn.jlmap.exception.JLGeoJsonParserException; 6 | import lombok.AccessLevel; 7 | import lombok.experimental.FieldDefaults; 8 | import lombok.experimental.NonFinal; 9 | 10 | /** 11 | * The base abstract class for a GeoJSON data source. Implementations of this class are expected 12 | * to provide functionality for loading and accessing GeoJSON objects. 13 | * @author Mehdi Akbarian Rastaghi (@makbn) 14 | * 15 | * @param source type 16 | */ 17 | @FieldDefaults(makeFinal = true, level = AccessLevel.PROTECTED) 18 | public abstract class JLGeoJsonSource { 19 | 20 | /** 21 | * Gson object for JSON serialization and deserialization. 22 | */ 23 | Gson gson; 24 | 25 | /** 26 | * The GeoJSON object loaded from this source. 27 | */ 28 | @NonFinal 29 | JLGeoJsonObject geoJsonObject; 30 | 31 | /** 32 | * Initializes a new instance of {@code JLGeoJsonSource} and sets up the Gson object. 33 | */ 34 | protected JLGeoJsonSource() { 35 | this.gson = new Gson(); 36 | } 37 | 38 | /** 39 | * Loads the GeoJSON data from the source and parses it into a GeoJSON object. 40 | * 41 | * @throws JLGeoJsonParserException If an error occurs during data loading or parsing. 42 | */ 43 | public abstract String load(S source) throws JLGeoJsonParserException; 44 | 45 | protected void validateJson(String jsonInString) throws JsonSyntaxException { 46 | gson.fromJson(jsonInString, Object.class); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/geojson/JLGeoJsonURL.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.geojson; 2 | 3 | import com.google.gson.JsonParseException; 4 | import io.github.makbn.jlmap.exception.JLGeoJsonParserException; 5 | 6 | import java.io.BufferedReader; 7 | import java.io.IOException; 8 | import java.io.InputStreamReader; 9 | import java.net.URL; 10 | 11 | /** 12 | * @author Mehdi Akbarian Rastaghi (@makbn) 13 | */ 14 | public class JLGeoJsonURL extends JLGeoJsonSource { 15 | 16 | @Override 17 | public String load(String url) throws JLGeoJsonParserException { 18 | try { 19 | URL jsonUrl = new URL(url); 20 | // Open a connection to the URL and create a BufferedReader to read the content 21 | try (BufferedReader reader = new BufferedReader(new InputStreamReader(jsonUrl.openStream()))) { 22 | StringBuilder content = new StringBuilder(); 23 | String line; 24 | // Read the JSON data line by line 25 | while ((line = reader.readLine()) != null) { 26 | content.append(line); 27 | } 28 | 29 | validateJson(content.toString()); 30 | return content.toString(); 31 | 32 | } 33 | } catch (IOException | JsonParseException e) { 34 | throw new JLGeoJsonParserException(e.getMessage()); 35 | } 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/layer/JLControlLayer.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.layer; 2 | 3 | import io.github.makbn.jlmap.JLMapCallbackHandler; 4 | import io.github.makbn.jlmap.layer.leaflet.LeafletControlLayerInt; 5 | import io.github.makbn.jlmap.model.JLBounds; 6 | import io.github.makbn.jlmap.model.JLLatLng; 7 | import javafx.scene.web.WebEngine; 8 | 9 | /** 10 | * Represents the Control layer on Leaflet map. 11 | * 12 | * @author Mehdi Akbarian Rastaghi (@makbn) 13 | */ 14 | public class JLControlLayer extends JLLayer implements LeafletControlLayerInt { 15 | public JLControlLayer(WebEngine engine, 16 | JLMapCallbackHandler callbackHandler) { 17 | super(engine, callbackHandler); 18 | } 19 | 20 | @Override 21 | public void zoomIn(int delta) { 22 | engine.executeScript(String.format("getMap().zoomIn(%d)", delta)); 23 | } 24 | 25 | @Override 26 | public void zoomOut(int delta) { 27 | engine.executeScript(String.format("getMap().zoomOut(%d)", delta)); 28 | } 29 | 30 | @Override 31 | public void setZoom(int level) { 32 | engine.executeScript(String.format("getMap().setZoom(%d)", level)); 33 | } 34 | 35 | @Override 36 | public void setZoomAround(JLLatLng latLng, int zoom) { 37 | engine.executeScript( 38 | String.format("getMap().setZoomAround(L.latLng(%f, %f), %d)", 39 | latLng.getLat(), latLng.getLng(), zoom)); 40 | } 41 | 42 | @Override 43 | public void fitBounds(JLBounds bounds) { 44 | engine.executeScript(String.format("getMap().fitBounds(%s)", 45 | bounds.toString())); 46 | } 47 | 48 | @Override 49 | public void fitWorld() { 50 | engine.executeScript("getMap().fitWorld()"); 51 | } 52 | 53 | @Override 54 | public void panTo(JLLatLng latLng) { 55 | engine.executeScript(String.format("getMap().panTo(L.latLng(%f, %f))", 56 | latLng.getLat(), latLng.getLng())); 57 | } 58 | 59 | @Override 60 | public void flyTo(JLLatLng latLng, int zoom) { 61 | engine.executeScript( 62 | String.format("getMap().flyTo(L.latLng(%f, %f), %d)", 63 | latLng.getLat(), latLng.getLng(), zoom)); 64 | } 65 | 66 | @Override 67 | public void flyToBounds(JLBounds bounds) { 68 | engine.executeScript(String.format("getMap().flyToBounds(%s)", 69 | bounds.toString())); 70 | } 71 | 72 | @Override 73 | public void setMaxBounds(JLBounds bounds) { 74 | engine.executeScript(String.format("getMap().setMaxBounds(%s)", 75 | bounds.toString())); 76 | } 77 | 78 | @Override 79 | public void setMinZoom(int zoom) { 80 | engine.executeScript(String.format("getMap().setMinZoom(%d)", zoom)); 81 | } 82 | 83 | @Override 84 | public void setMaxZoom(int zoom) { 85 | engine.executeScript(String.format("getMap().setMaxZoom(%d)", zoom)); 86 | } 87 | 88 | @Override 89 | public void panInsideBounds(JLBounds bounds) { 90 | engine.executeScript(String.format("getMap().panInsideBounds(%s)", 91 | bounds.toString())); 92 | } 93 | 94 | @Override 95 | public void panInside(JLLatLng latLng) { 96 | engine.executeScript( 97 | String.format("getMap().panInside(L.latLng(%f, %f))", 98 | latLng.getLat(), latLng.getLng())); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/layer/JLGeoJsonLayer.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.layer; 2 | 3 | import io.github.makbn.jlmap.JLMapCallbackHandler; 4 | import io.github.makbn.jlmap.exception.JLException; 5 | import io.github.makbn.jlmap.geojson.JLGeoJsonContent; 6 | import io.github.makbn.jlmap.geojson.JLGeoJsonFile; 7 | import io.github.makbn.jlmap.geojson.JLGeoJsonObject; 8 | import io.github.makbn.jlmap.geojson.JLGeoJsonURL; 9 | import io.github.makbn.jlmap.layer.leaflet.LeafletGeoJsonLayerInt; 10 | import javafx.scene.web.WebEngine; 11 | import lombok.NonNull; 12 | import netscape.javascript.JSObject; 13 | 14 | import java.io.File; 15 | import java.util.UUID; 16 | 17 | /** 18 | * Represents the GeoJson (other) layer on Leaflet map. 19 | * @author Mehdi Akbarian Rastaghi (@makbn) 20 | */ 21 | public class JLGeoJsonLayer extends JLLayer implements LeafletGeoJsonLayerInt { 22 | private static final String MEMBER_PREFIX = "geoJson"; 23 | private static final String WINDOW_OBJ = "window"; 24 | JLGeoJsonURL fromUrl; 25 | JLGeoJsonFile fromFile; 26 | JLGeoJsonContent fromContent; 27 | JSObject window; 28 | 29 | public JLGeoJsonLayer(WebEngine engine, 30 | JLMapCallbackHandler callbackHandler) { 31 | super(engine, callbackHandler); 32 | this.fromUrl = new JLGeoJsonURL(); 33 | this.fromFile = new JLGeoJsonFile(); 34 | this.fromContent = new JLGeoJsonContent(); 35 | this.window = (JSObject) engine.executeScript(WINDOW_OBJ); 36 | } 37 | 38 | @Override 39 | public JLGeoJsonObject addFromFile(@NonNull File file) throws JLException { 40 | String json = fromFile.load(file); 41 | return addGeoJson(json); 42 | } 43 | 44 | @Override 45 | public JLGeoJsonObject addFromUrl(@NonNull String url) throws JLException { 46 | String json = fromUrl.load(url); 47 | return addGeoJson(json); 48 | } 49 | 50 | @Override 51 | public JLGeoJsonObject addFromContent(@NonNull String content) 52 | throws JLException { 53 | String json = fromContent.load(content); 54 | return addGeoJson(json); 55 | } 56 | 57 | @Override 58 | public boolean removeGeoJson(@NonNull JLGeoJsonObject object) { 59 | return Boolean.parseBoolean(engine.executeScript( 60 | String.format("removeGeoJson(%d)", object.getId())).toString()); 61 | } 62 | 63 | private JLGeoJsonObject addGeoJson(String jlGeoJsonObject) { 64 | try { 65 | String identifier = MEMBER_PREFIX + UUID.randomUUID(); 66 | this.window.setMember(identifier, jlGeoJsonObject); 67 | String returnedId = engine.executeScript( 68 | String.format("addGeoJson(\"%s\")", identifier)).toString(); 69 | 70 | return JLGeoJsonObject.builder() 71 | .id(Integer.parseInt(returnedId)) 72 | .geoJsonContent(jlGeoJsonObject) 73 | .build(); 74 | } catch (Exception e) { 75 | throw new JLException(e); 76 | } 77 | 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/layer/JLLayer.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.layer; 2 | 3 | import io.github.makbn.jlmap.JLMapCallbackHandler; 4 | import javafx.scene.web.WebEngine; 5 | import lombok.AccessLevel; 6 | import lombok.experimental.FieldDefaults; 7 | 8 | /** 9 | * Represents the basic layer. 10 | * 11 | * @author Mehdi Akbarian Rastaghi (@makbn) 12 | */ 13 | @FieldDefaults(level = AccessLevel.PROTECTED) 14 | public abstract class JLLayer { 15 | WebEngine engine; 16 | JLMapCallbackHandler callbackHandler; 17 | 18 | protected JLLayer(WebEngine engine, JLMapCallbackHandler callbackHandler) { 19 | this.engine = engine; 20 | this.callbackHandler = callbackHandler; 21 | } 22 | 23 | /** 24 | * Forces the subclasses to implement 25 | * {@link #JLLayer(WebEngine, JLMapCallbackHandler)} constructor! 26 | */ 27 | private JLLayer() { 28 | // NO-OP 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/layer/JLUiLayer.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.layer; 2 | 3 | import io.github.makbn.jlmap.JLMapCallbackHandler; 4 | import io.github.makbn.jlmap.layer.leaflet.LeafletUILayerInt; 5 | import io.github.makbn.jlmap.model.JLLatLng; 6 | import io.github.makbn.jlmap.model.JLMarker; 7 | import io.github.makbn.jlmap.model.JLOptions; 8 | import io.github.makbn.jlmap.model.JLPopup; 9 | import javafx.scene.web.WebEngine; 10 | 11 | /** 12 | * Represents the UI layer on Leaflet map. 13 | * 14 | * @author Mehdi Akbarian Rastaghi (@makbn) 15 | */ 16 | public class JLUiLayer extends JLLayer implements LeafletUILayerInt { 17 | 18 | public JLUiLayer(WebEngine engine, JLMapCallbackHandler callbackHandler) { 19 | super(engine, callbackHandler); 20 | } 21 | 22 | /** 23 | * Add a {{@link JLMarker}} to the map with given text as content and {{@link JLLatLng}} as position. 24 | * 25 | * @param latLng position on the map. 26 | * @param text content of the related popup if available! 27 | * @return the instance of added {{@link JLMarker}} on the map. 28 | */ 29 | @Override 30 | public JLMarker addMarker(JLLatLng latLng, String text, boolean draggable) { 31 | String result = engine.executeScript(String.format("addMarker(%f, %f, '%s', %b)", latLng.getLat(), latLng.getLng(), text, draggable)) 32 | .toString(); 33 | int index = Integer.parseInt(result); 34 | JLMarker marker = new JLMarker(index, text, latLng); 35 | callbackHandler.addJLObject(marker); 36 | return marker; 37 | } 38 | 39 | /** 40 | * Remove a {{@link JLMarker}} from the map. 41 | * 42 | * @param id of the marker for removing. 43 | * @return {{@link Boolean#TRUE}} if removed successfully. 44 | */ 45 | @Override 46 | public boolean removeMarker(int id) { 47 | String result = engine.executeScript(String.format("removeMarker(%d)", id)).toString(); 48 | callbackHandler.remove(JLMarker.class, id); 49 | return Boolean.parseBoolean(result); 50 | } 51 | 52 | /** 53 | * Add a {{@link JLPopup}} to the map with given text as content and 54 | * {@link JLLatLng} as position. 55 | * 56 | * @param latLng position on the map. 57 | * @param text content of the popup. 58 | * @param options see {{@link JLOptions}} for customizing 59 | * @return the instance of added {{@link JLPopup}} on the map. 60 | */ 61 | @Override 62 | public JLPopup addPopup(JLLatLng latLng, String text, JLOptions options) { 63 | String result = engine.executeScript(String.format("addPopup(%f, %f, \"%s\", %b , %b)", latLng.getLat(), latLng.getLng(), text, options.isCloseButton(), options.isAutoClose())) 64 | .toString(); 65 | 66 | int index = Integer.parseInt(result); 67 | return new JLPopup(index, text, latLng, options); 68 | } 69 | 70 | /** 71 | * Add popup with {{@link JLOptions#DEFAULT}} options 72 | * 73 | * @see JLUiLayer#addPopup(JLLatLng, String, JLOptions) 74 | */ 75 | @Override 76 | public JLPopup addPopup(JLLatLng latLng, String text) { 77 | return addPopup(latLng, text, JLOptions.DEFAULT); 78 | } 79 | 80 | /** 81 | * Remove a {@link JLPopup} from the map. 82 | * 83 | * @param id of the marker for removing. 84 | * @return true if removed successfully. 85 | */ 86 | @Override 87 | public boolean removePopup(int id) { 88 | String result = engine.executeScript(String.format("removePopup(%d)", id)) 89 | .toString(); 90 | return Boolean.parseBoolean(result); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/layer/JLVectorLayer.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.layer; 2 | 3 | import io.github.makbn.jlmap.JLMapCallbackHandler; 4 | import io.github.makbn.jlmap.JLProperties; 5 | import io.github.makbn.jlmap.layer.leaflet.LeafletVectorLayerInt; 6 | import io.github.makbn.jlmap.model.*; 7 | import javafx.scene.paint.Color; 8 | import javafx.scene.web.WebEngine; 9 | 10 | /** 11 | * Represents the Vector layer on Leaflet map. 12 | * @author Mehdi Akbarian Rastaghi (@makbn) 13 | */ 14 | public class JLVectorLayer extends JLLayer implements LeafletVectorLayerInt { 15 | 16 | public JLVectorLayer(WebEngine engine, 17 | JLMapCallbackHandler callbackHandler) { 18 | super(engine, callbackHandler); 19 | } 20 | 21 | /** 22 | * Drawing polyline overlays on the map with {@link JLOptions#DEFAULT} 23 | * options 24 | * 25 | * @see JLVectorLayer#addPolyline(JLLatLng[], JLOptions) 26 | */ 27 | @Override 28 | public JLPolyline addPolyline(JLLatLng[] vertices) { 29 | return addPolyline(vertices, JLOptions.DEFAULT); 30 | } 31 | 32 | /** 33 | * Drawing polyline overlays on the map. 34 | * 35 | * @param vertices arrays of LatLng points 36 | * @param options see {@link JLOptions} for customizing 37 | * @return the added {@link JLPolyline} to map 38 | */ 39 | @Override 40 | public JLPolyline addPolyline(JLLatLng[] vertices, JLOptions options) { 41 | String latlngs = convertJLLatLngToString(vertices); 42 | String hexColor = convertColorToString(options.getColor()); 43 | String result = engine.executeScript( 44 | String.format("addPolyLine(%s, '%s', %d, %b, %f, %f)", 45 | latlngs, hexColor, options.getWeight(), 46 | options.isStroke(), options.getOpacity(), 47 | options.getSmoothFactor())) 48 | .toString(); 49 | 50 | int index = Integer.parseInt(result); 51 | JLPolyline polyline = new JLPolyline(index, options, vertices); 52 | callbackHandler.addJLObject(polyline); 53 | return polyline; 54 | } 55 | 56 | /** 57 | * Remove a polyline from the map by id. 58 | * 59 | * @param id of polyline 60 | * @return {@link Boolean#TRUE} if removed successfully 61 | */ 62 | @Override 63 | public boolean removePolyline(int id) { 64 | String result = engine.executeScript( 65 | String.format("removePolyLine(%d)", id)).toString(); 66 | 67 | callbackHandler.remove(JLPolyline.class, id); 68 | callbackHandler.remove(JLMultiPolyline.class, id); 69 | 70 | return Boolean.parseBoolean(result); 71 | } 72 | 73 | /** 74 | * Drawing multi polyline overlays on the map with 75 | * {@link JLOptions#DEFAULT} options. 76 | * 77 | * @return the added {@link JLMultiPolyline} to map 78 | * @see JLVectorLayer#addMultiPolyline(JLLatLng[][], JLOptions) 79 | */ 80 | @Override 81 | public JLMultiPolyline addMultiPolyline(JLLatLng[][] vertices) { 82 | return addMultiPolyline(vertices, JLOptions.DEFAULT); 83 | } 84 | 85 | /** 86 | * Drawing MultiPolyline shape overlays on the map with 87 | * multi-dimensional array. 88 | * 89 | * @param vertices arrays of LatLng points 90 | * @param options see {@link JLOptions} for customizing 91 | * @return the added {@link JLMultiPolyline} to map 92 | */ 93 | @Override 94 | public JLMultiPolyline addMultiPolyline(JLLatLng[][] vertices, 95 | JLOptions options) { 96 | String latlngs = convertJLLatLngToString(vertices); 97 | String hexColor = convertColorToString(options.getColor()); 98 | String result = engine.executeScript( 99 | String.format("addPolyLine(%s, '%s', %d, %b, %f, %f)", 100 | latlngs, hexColor, options.getWeight(), 101 | options.isStroke(), options.getOpacity(), 102 | options.getSmoothFactor())) 103 | .toString(); 104 | 105 | int index = Integer.parseInt(result); 106 | JLMultiPolyline multiPolyline = 107 | new JLMultiPolyline(index, options, vertices); 108 | callbackHandler.addJLObject(multiPolyline); 109 | return multiPolyline; 110 | } 111 | 112 | 113 | /** 114 | * @see JLVectorLayer#removePolyline(int) 115 | */ 116 | @Override 117 | public boolean removeMultiPolyline(int id) { 118 | return removePolyline(id); 119 | } 120 | 121 | /** 122 | * Drawing polygon overlays on the map. 123 | * Note that points you pass when creating a polygon shouldn't have an additional 124 | * last point equal to the first one. 125 | * You can also pass an array of arrays of {{@link JLLatLng}}, 126 | * with the first dimension representing the outer shape and the other 127 | * dimension representing holes in the outer shape! 128 | * Additionally, you can pass a multi-dimensional array to represent a MultiPolygon shape! 129 | * 130 | * @param vertices array of {{@link JLLatLng}} points 131 | * @param options see {{@link JLOptions}} 132 | * @return Instance of the created {{@link JLPolygon}} 133 | */ 134 | @Override 135 | public JLPolygon addPolygon(JLLatLng[][][] vertices, JLOptions options) { 136 | String latlngs = convertJLLatLngToString(vertices); 137 | 138 | String result = engine.executeScript(String.format( 139 | "addPolygon(%s, '%s', '%s', %d, %b, %b, %f, %f, %f)", 140 | latlngs, convertColorToString(options.getColor()), 141 | convertColorToString(options.getFillColor()), 142 | options.getWeight(), options.isStroke(), 143 | options.isFill(), options.getOpacity(), 144 | options.getFillOpacity(), options.getSmoothFactor())) 145 | .toString(); 146 | 147 | int index = Integer.parseInt(result); 148 | JLPolygon polygon = new JLPolygon(index, options, vertices); 149 | callbackHandler.addJLObject(polygon); 150 | return polygon; 151 | } 152 | 153 | /** 154 | * Drawing polygon overlays on the map with {@link JLOptions#DEFAULT} 155 | * option. 156 | * 157 | * @see JLVectorLayer#addMultiPolyline(JLLatLng[][], JLOptions) 158 | */ 159 | @Override 160 | public JLPolygon addPolygon(JLLatLng[][][] vertices) { 161 | return addPolygon(vertices, JLOptions.DEFAULT); 162 | } 163 | 164 | /** 165 | * Remove a {{@link JLPolygon}} from the map by id. 166 | * 167 | * @param id of Polygon 168 | * @return {{@link Boolean#TRUE}} if removed successfully 169 | */ 170 | @Override 171 | public boolean removePolygon(int id) { 172 | String result = engine.executeScript( 173 | String.format("removePolygon(%d)", id)).toString(); 174 | callbackHandler.remove(JLPolygon.class, id); 175 | return Boolean.parseBoolean(result); 176 | } 177 | 178 | /** 179 | * Add a {@link JLCircle} to the map; 180 | * 181 | * @param center coordinate of the circle. 182 | * @param radius radius of circle in meter. 183 | * @param options see {@link JLOptions} 184 | * @return the instance of created {@link JLCircle} 185 | */ 186 | @Override 187 | public JLCircle addCircle(JLLatLng center, int radius, JLOptions options) { 188 | String result = engine.executeScript(String.format( 189 | "addCircle([%f, %f], %d, '%s', '%s', %d, %b, %b, %f, %f, %f)", 190 | center.getLat(), center.getLng(), radius, 191 | convertColorToString(options.getColor()), 192 | convertColorToString(options.getFillColor()), 193 | options.getWeight(), options.isStroke(), 194 | options.isFill(), options.getOpacity(), 195 | options.getFillOpacity(), options.getSmoothFactor())) 196 | .toString(); 197 | 198 | int index = Integer.parseInt(result); 199 | JLCircle circle = new JLCircle(index, radius, center, options); 200 | callbackHandler.addJLObject(circle); 201 | return circle; 202 | } 203 | 204 | /** 205 | * Add {{@link JLCircle}} to the map with {@link JLOptions#DEFAULT} options. 206 | * Default value for radius is {@link JLProperties#DEFAULT_CIRCLE_RADIUS} 207 | * 208 | * @see JLVectorLayer#addCircle(JLLatLng, int, JLOptions) 209 | */ 210 | @Override 211 | public JLCircle addCircle(JLLatLng center) { 212 | return addCircle(center, JLProperties.DEFAULT_CIRCLE_RADIUS, 213 | JLOptions.DEFAULT); 214 | } 215 | 216 | /** 217 | * Remove a {@link JLCircle} from the map by id. 218 | * 219 | * @param id of Circle 220 | * @return {@link Boolean#TRUE} if removed successfully 221 | */ 222 | @Override 223 | public boolean removeCircle(int id) { 224 | String result = engine.executeScript(String.format("removeCircle(%d)", id)) 225 | .toString(); 226 | callbackHandler.remove(JLCircle.class, id); 227 | return Boolean.parseBoolean(result); 228 | } 229 | 230 | /** 231 | * Add a {@link JLCircleMarker} to the map; 232 | * 233 | * @param center coordinate of the circle. 234 | * @param radius radius of circle in meter. 235 | * @param options see {@link JLOptions} 236 | * @return the instance of created {@link JLCircleMarker} 237 | */ 238 | @Override 239 | public JLCircleMarker addCircleMarker(JLLatLng center, int radius, 240 | JLOptions options) { 241 | String result = engine.executeScript(String.format( 242 | "addCircleMarker([%f, %f], %d, '%s', '%s', %d, %b, %b, %f, %f, %f)", 243 | center.getLat(), center.getLng(), radius, 244 | convertColorToString(options.getColor()), 245 | convertColorToString(options.getFillColor()), 246 | options.getWeight(), options.isStroke(), 247 | options.isFill(), options.getOpacity(), 248 | options.getFillOpacity(), options.getSmoothFactor())) 249 | .toString(); 250 | 251 | int index = Integer.parseInt(result); 252 | JLCircleMarker circleMarker = 253 | new JLCircleMarker(index, radius, center, options); 254 | callbackHandler.addJLObject(circleMarker); 255 | return circleMarker; 256 | } 257 | 258 | /** 259 | * Add {@link JLCircleMarker} to the map with {@link JLOptions#DEFAULT} 260 | * options. Default value for radius is 261 | * {@link JLProperties#DEFAULT_CIRCLE_MARKER_RADIUS} 262 | * 263 | * @see JLVectorLayer#addCircle(JLLatLng, int, JLOptions) 264 | */ 265 | @Override 266 | public JLCircleMarker addCircleMarker(JLLatLng center) { 267 | return addCircleMarker(center, 268 | JLProperties.DEFAULT_CIRCLE_MARKER_RADIUS, JLOptions.DEFAULT); 269 | } 270 | 271 | 272 | /** 273 | * Remove a {@link JLCircleMarker} from the map by id. 274 | * 275 | * @param id of Circle 276 | * @return {@link Boolean#TRUE} if removed successfully 277 | */ 278 | @Override 279 | public boolean removeCircleMarker(int id) { 280 | String result = engine.executeScript( 281 | String.format("removeCircleMarker(%d)", id)).toString(); 282 | callbackHandler.remove(JLCircleMarker.class, id); 283 | return Boolean.parseBoolean(result); 284 | } 285 | 286 | private String convertJLLatLngToString(JLLatLng[] latLngs) { 287 | StringBuilder sb = new StringBuilder(); 288 | sb.append("["); 289 | for (JLLatLng latLng : latLngs) { 290 | sb.append(String.format("%s, ", latLng.toString())); 291 | } 292 | sb.append("]"); 293 | return sb.toString(); 294 | } 295 | 296 | private String convertJLLatLngToString(JLLatLng[][] latLngsList) { 297 | StringBuilder sb = new StringBuilder(); 298 | sb.append("["); 299 | for (JLLatLng[] latLngs : latLngsList) { 300 | sb.append(convertJLLatLngToString(latLngs)).append(","); 301 | } 302 | sb.append("]"); 303 | return sb.toString(); 304 | } 305 | 306 | private String convertJLLatLngToString(JLLatLng[][][] latLngList) { 307 | StringBuilder sb = new StringBuilder(); 308 | sb.append("["); 309 | for (JLLatLng[][] latLng2DArray : latLngList) { 310 | sb.append(convertJLLatLngToString(latLng2DArray)).append(","); 311 | } 312 | sb.append("]"); 313 | return sb.toString(); 314 | } 315 | 316 | private String convertColorToString(Color c) { 317 | int r = (int) Math.round(c.getRed() * 255.0); 318 | int g = (int) Math.round(c.getGreen() * 255.0); 319 | int b = (int) Math.round(c.getBlue() * 255.0); 320 | int a = (int) Math.round(c.getOpacity() * 255.0); 321 | return String.format("#%02x%02x%02x%02x", r, g, b, a); 322 | } 323 | } 324 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/layer/leaflet/LeafletControlLayerInt.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.layer.leaflet; 2 | 3 | import io.github.makbn.jlmap.model.JLBounds; 4 | import io.github.makbn.jlmap.model.JLLatLng; 5 | 6 | /** 7 | * The {@code LeafletControlLayerInt} interface defines methods for controlling 8 | * the zoom and view of a Leaflet map. Leaflet is a popular JavaScript library 9 | * for creating interactive maps, and this interface provides a Java API for 10 | * manipulating the map's zoom level, view, and geographical bounds. 11 | * 12 | * @author Mehdi Akbarian Rastaghi (@makbn) 13 | */ 14 | public interface LeafletControlLayerInt { 15 | 16 | /** 17 | * Increases the zoom of the map by delta 18 | * 19 | * @see leafletjs.com/reference.html#map-zoomin 20 | */ 21 | void zoomIn(int delta); 22 | 23 | /** 24 | * Decreases the zoom of the map by delta 25 | * 26 | * @see 27 | * leafletjs.com/reference.html#map-zoomout 28 | */ 29 | void zoomOut(int delta); 30 | 31 | /** 32 | * Sets the zoom of the map. 33 | * 34 | * @see 35 | * leafletjs.com/reference.html#map-setzoom 36 | */ 37 | void setZoom(int level); 38 | 39 | /** 40 | * Zooms the map while keeping a specified geographical point on the map 41 | * stationary (e.g. used internally for scroll zoom and double-click zoom) 42 | * 43 | * @see 44 | * leafletjs.com/reference.html#map-setzoomaround 45 | */ 46 | void setZoomAround(JLLatLng latLng, int zoom); 47 | 48 | 49 | /** 50 | * Sets a map view that contains the given geographical bounds with the 51 | * maximum zoom level possible. 52 | * 53 | * @param bounds The geographical bounds to fit. 54 | * @see 55 | * leafletjs.com/reference.html#map-fitbounds 56 | */ 57 | void fitBounds(JLBounds bounds); 58 | 59 | /** 60 | * Sets a map view that mostly contains the whole world with the maximum 61 | * zoom level possible. 62 | * 63 | * @see 64 | * leafletjs.com/reference.html#map-fitworld 65 | */ 66 | void fitWorld(); 67 | 68 | /** 69 | * Pans the map to a given center. 70 | * 71 | * @param latLng The new center of the map. 72 | * @see 73 | * leafletjs.com/reference.html#map-panto 74 | */ 75 | void panTo(JLLatLng latLng); 76 | 77 | /** 78 | * Sets the view of the map (geographical center and zoom) performing a 79 | * smooth pan-zoom animation. 80 | * 81 | * @param latLng The new center of the map. 82 | * @param zoom The new zoom level (optional). 83 | * @see 84 | * leafletjs.com/reference.html#map-flyto 85 | */ 86 | void flyTo(JLLatLng latLng, int zoom); 87 | 88 | /** 89 | * Sets the view of the map with a smooth animation like flyTo, but 90 | * takes a bounds parameter like fitBounds. 91 | * 92 | * @param bounds The bounds to fit the map view to. 93 | * @see 94 | * leafletjs.com/reference.html#map-flytobounds 95 | */ 96 | void flyToBounds(JLBounds bounds); 97 | 98 | /** 99 | * Restricts the map view to the given bounds. 100 | * 101 | * @param bounds The geographical bounds to restrict the map view to. 102 | * @see 103 | * leafletjs.com/reference.html#map-setmaxbounds 104 | */ 105 | void setMaxBounds(JLBounds bounds); 106 | 107 | /** 108 | * Sets the lower limit for the available zoom levels. 109 | * 110 | * @param zoom The minimum zoom level. 111 | * @see 112 | * leafletjs.com/reference.html#map-setminzoom 113 | */ 114 | void setMinZoom(int zoom); 115 | 116 | /** 117 | * Sets the upper limit for the available zoom levels. 118 | * 119 | * @param zoom The maximum zoom level. 120 | * @see 121 | * leafletjs.com/reference.html#map-setmaxzoom 122 | */ 123 | void setMaxZoom(int zoom); 124 | 125 | /** 126 | * Pans the map to the closest view that would lie inside the given bounds. 127 | * 128 | * @param bounds The bounds to pan inside. 129 | * @see 130 | * leafletjs.com/reference.html#map-paninsidebounds 131 | */ 132 | void panInsideBounds(JLBounds bounds); 133 | 134 | /** 135 | * Pans the map the minimum amount to make the latLng visible. 136 | * 137 | * @param latLng The geographical point to make visible. 138 | * @see 139 | * leafletjs.com/reference.html#map-paninside 140 | */ 141 | void panInside(JLLatLng latLng); 142 | 143 | } 144 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/layer/leaflet/LeafletGeoJsonLayerInt.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.layer.leaflet; 2 | 3 | import io.github.makbn.jlmap.exception.JLException; 4 | import io.github.makbn.jlmap.geojson.JLGeoJsonObject; 5 | import lombok.NonNull; 6 | 7 | import java.io.File; 8 | 9 | /** 10 | * The {@code LeafletGeoJsonLayerInt} interface defines methods for adding 11 | * and managing GeoJSON data layers in a Leaflet map. 12 | *

13 | * Implementations of this interface should provide methods to add GeoJSON 14 | * data from various sources, such as files, URLs, or raw content, as well 15 | * as the ability to remove GeoJSON objects from the map. 16 | * 17 | * @author Mehdi Akbarian Rastaghi (@makbn) 18 | */ 19 | public interface LeafletGeoJsonLayerInt { 20 | 21 | /** 22 | * Adds a GeoJSON object from a file to the Leaflet map. 23 | * 24 | * @param file The {@link File} object representing the GeoJSON file to be 25 | * added. 26 | * @return The {@link JLGeoJsonObject} representing the added GeoJSON data. 27 | * @throws JLException If there is an error while adding the GeoJSON data. 28 | */ 29 | JLGeoJsonObject addFromFile(@NonNull File file) throws JLException; 30 | 31 | /** 32 | * Adds a GeoJSON object from a URL to the Leaflet map. 33 | * 34 | * @param url The URL of the GeoJSON data to be added. 35 | * @return The {@link JLGeoJsonObject} representing the added GeoJSON data. 36 | * @throws JLException If there is an error while adding the GeoJSON data. 37 | */ 38 | JLGeoJsonObject addFromUrl(@NonNull String url) throws JLException; 39 | 40 | /** 41 | * Adds a GeoJSON object from raw content to the Leaflet map. 42 | * 43 | * @param content The raw GeoJSON content to be added. 44 | * @return The {@link JLGeoJsonObject} representing the added GeoJSON data. 45 | * @throws JLException If there is an error while adding the GeoJSON data. 46 | */ 47 | JLGeoJsonObject addFromContent(@NonNull String content) throws JLException; 48 | 49 | /** 50 | * Removes a GeoJSON object from the Leaflet map. 51 | * 52 | * @param object The {@link JLGeoJsonObject} to be removed from the map. 53 | * @return {@code true} if the removal was successful, {@code false} 54 | * if the object was not found or could not be removed. 55 | */ 56 | boolean removeGeoJson(@NonNull JLGeoJsonObject object); 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/layer/leaflet/LeafletUILayerInt.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.layer.leaflet; 2 | 3 | import io.github.makbn.jlmap.model.JLLatLng; 4 | import io.github.makbn.jlmap.model.JLMarker; 5 | import io.github.makbn.jlmap.model.JLOptions; 6 | import io.github.makbn.jlmap.model.JLPopup; 7 | 8 | /** 9 | * The {@code LeafletUILayerInt} interface defines methods for adding and 10 | * managing user interface elements like markers and popups in a Leaflet map. 11 | * Leaflet is a popular JavaScript library for creating interactive maps, 12 | * and this interface provides a Java API for working with user interface 13 | * elements within Leaflet. 14 | * 15 | * @author Mehdi Akbarian Rastaghi (@makbn) 16 | */ 17 | public interface LeafletUILayerInt { 18 | 19 | /** 20 | * Adds a marker to the Leaflet map at the specified geographical 21 | * coordinates. 22 | * 23 | * @param latLng The geographical coordinates (latitude and longitude) 24 | * where the marker should be placed. 25 | * @param text The text content associated with the marker. 26 | * @param draggable {@code true} if the marker should be draggable, 27 | * {@code false} otherwise. 28 | * @return The {@link JLMarker} representing the added marker on the map. 29 | */ 30 | JLMarker addMarker(JLLatLng latLng, String text, boolean draggable); 31 | 32 | /** 33 | * Removes a marker from the Leaflet map based on its identifier. 34 | * 35 | * @param id The unique identifier of the marker to be removed. 36 | * @return {@code true} if the marker was successfully removed, 37 | * {@code false} if the marker with the specified identifier was not found. 38 | * 39 | */ 40 | boolean removeMarker(int id); 41 | 42 | /** 43 | * Adds a popup to the Leaflet map at the specified geographical 44 | * coordinates with custom options. 45 | * 46 | * @param latLng The geographical coordinates (latitude and longitude) 47 | * where the popup should be displayed. 48 | * @param text The text content of the popup. 49 | * @param options Custom options for configuring the appearance and 50 | * behavior of the popup. 51 | * @return The {@link JLPopup} representing the added popup on the map. 52 | */ 53 | JLPopup addPopup(JLLatLng latLng, String text, JLOptions options); 54 | 55 | /** 56 | * Adds a popup to the Leaflet map at the specified geographical 57 | * coordinates with default options. 58 | * 59 | * @param latLng The geographical coordinates (latitude and longitude) 60 | * where the popup should be displayed. 61 | * @param text The text content of the popup. 62 | * @return The {@link JLPopup} representing the added popup on the map. 63 | */ 64 | JLPopup addPopup(JLLatLng latLng, String text); 65 | 66 | /** 67 | * Removes a popup from the Leaflet map based on its identifier. 68 | * 69 | * @param id The unique identifier of the popup to be removed. 70 | * @return {@code true} if the popup was successfully removed, 71 | * {@code false} if the popup with the specified identifier 72 | * was not found. 73 | */ 74 | boolean removePopup(int id); 75 | } 76 | 77 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/layer/leaflet/LeafletVectorLayerInt.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.layer.leaflet; 2 | 3 | import io.github.makbn.jlmap.model.*; 4 | 5 | /** 6 | * The {@code LeafletVectorLayerInt} interface defines methods for adding and managing 7 | * vector-based elements such as polylines, polygons, circles, and circle markers in a 8 | * Leaflet map. Leaflet is a popular JavaScript library for creating interactive maps, and 9 | * this interface provides a Java API for working with vector-based layers within Leaflet. 10 | * 11 | * @author Mehdi Akbarian Rastaghi (@makbn) 12 | */ 13 | public interface LeafletVectorLayerInt { 14 | 15 | /** 16 | * Adds a polyline to the Leaflet map with the provided array of vertices. 17 | * 18 | * @param vertices An array of geographical coordinates (latitude and longitude) that define 19 | * the vertices of the polyline. 20 | * @return The {@link JLPolyline} representing the added polyline on the map. 21 | */ 22 | JLPolyline addPolyline(JLLatLng[] vertices); 23 | 24 | /** 25 | * Adds a polyline to the Leaflet map with the provided array of vertices and custom options. 26 | * 27 | * @param vertices An array of geographical coordinates (latitude and longitude) that define 28 | * the vertices of the polyline. 29 | * @param options Custom options for configuring the appearance and behavior of the polyline. 30 | * @return The {@link JLPolyline} representing the added polyline on the map. 31 | */ 32 | JLPolyline addPolyline(JLLatLng[] vertices, JLOptions options); 33 | 34 | /** 35 | * Removes a polyline from the Leaflet map based on its identifier. 36 | * 37 | * @param id The unique identifier of the polyline to be removed. 38 | * @return {@code true} if the polyline was successfully removed, {@code false} if the 39 | * polyline with the specified identifier was not found. 40 | */ 41 | 42 | boolean removePolyline(int id); 43 | 44 | /** 45 | * Adds a multi-polyline to the Leaflet map with the provided array of arrays of vertices. 46 | * 47 | * @param vertices An array of arrays of geographical coordinates (latitude and longitude) that 48 | * define the vertices of multiple polylines. 49 | * @return The {@link JLMultiPolyline} representing the added multi-polyline on the map. 50 | */ 51 | JLMultiPolyline addMultiPolyline(JLLatLng[][] vertices); 52 | 53 | /** 54 | * Adds a multi-polyline to the Leaflet map with the provided array of arrays of vertices and custom options. 55 | * 56 | * @param vertices An array of arrays of geographical coordinates (latitude and longitude) that 57 | * define the vertices of multiple polylines. 58 | * @param options Custom options for configuring the appearance and behavior of the multi-polyline. 59 | * @return The {@link JLMultiPolyline} representing the added multi-polyline on the map. 60 | */ 61 | JLMultiPolyline addMultiPolyline(JLLatLng[][] vertices, JLOptions options); 62 | 63 | /** 64 | * Removes a multi-polyline from the Leaflet map based on its identifier. 65 | * 66 | * @param id The unique identifier of the multi-polyline to be removed. 67 | * @return {@code true} if the multi-polyline was successfully removed, {@code false} if the 68 | * multi-polyline with the specified identifier was not found. 69 | */ 70 | boolean removeMultiPolyline(int id); 71 | 72 | /** 73 | * Adds a polygon to the Leaflet map with the provided array of arrays of vertices and custom options. 74 | * 75 | * @param vertices An array of arrays of geographical coordinates (latitude and longitude) that 76 | * define the vertices of the polygon. 77 | * @param options Custom options for configuring the appearance and behavior of the polygon. 78 | * @return The {@link JLPolygon} representing the added polygon on the map. 79 | */ 80 | JLPolygon addPolygon(JLLatLng[][][] vertices, JLOptions options); 81 | 82 | /** 83 | * Adds a polygon to the Leaflet map with the provided array of arrays of vertices. 84 | * 85 | * @param vertices An array of arrays of geographical coordinates (latitude and longitude) that 86 | * define the vertices of the polygon. 87 | * @return The {@link JLPolygon} representing the added polygon on the map. 88 | */ 89 | JLPolygon addPolygon(JLLatLng[][][] vertices); 90 | 91 | /** 92 | * Removes a polygon from the Leaflet map based on its identifier. 93 | * 94 | * @param id The unique identifier of the polygon to be removed. 95 | * @return {@code true} if the polygon was successfully removed, {@code false} if the 96 | * polygon with the specified identifier was not found. 97 | */ 98 | boolean removePolygon(int id); 99 | 100 | /** 101 | * Adds a circle to the Leaflet map with the provided center coordinates, radius, and custom options. 102 | * 103 | * @param center The geographical coordinates (latitude and longitude) of the circle's center. 104 | * @param radius The radius of the circle in meters. 105 | * @param options Custom options for configuring the appearance and behavior of the circle. 106 | * @return The {@link JLCircle} representing the added circle on the map. 107 | */ 108 | JLCircle addCircle(JLLatLng center, int radius, JLOptions options); 109 | 110 | /** 111 | * Adds a circle to the Leaflet map with the provided center coordinates and radius. 112 | * 113 | * @param center The geographical coordinates (latitude and longitude) of the circle's center. 114 | * @return The {@link JLCircle} representing the added circle on the map. 115 | */ 116 | JLCircle addCircle(JLLatLng center); 117 | 118 | /** 119 | * Removes a circle from the Leaflet map based on its identifier. 120 | * 121 | * @param id The unique identifier of the circle to be removed. 122 | * @return {@code true} if the circle was successfully removed, {@code false} if the 123 | * circle with the specified identifier was not found. 124 | */ 125 | boolean removeCircle(int id); 126 | 127 | /** 128 | * Adds a circle marker to the Leaflet map with the provided center coordinates, radius, and custom options. 129 | * 130 | * @param center The geographical coordinates (latitude and longitude) of the circle marker's center. 131 | * @param radius The radius of the circle marker in pixels. 132 | * @param options Custom options for configuring the appearance and behavior of the circle marker. 133 | * @return The {@link JLCircleMarker} representing the added circle marker on the map. 134 | */ 135 | JLCircleMarker addCircleMarker(JLLatLng center, int radius, JLOptions options); 136 | 137 | /** 138 | * Adds a circle marker to the Leaflet map with the provided center coordinates and radius. 139 | * 140 | * @param center The geographical coordinates (latitude and longitude) of the circle marker's center. 141 | * @return The {@link JLCircleMarker} representing the added circle marker on the map. 142 | */ 143 | JLCircleMarker addCircleMarker(JLLatLng center); 144 | 145 | /** 146 | * Removes a circle marker from the Leaflet map based on its identifier. 147 | * 148 | * @param id The unique identifier of the circle marker to be removed. 149 | * @return {@code true} if the circle marker was successfully removed, {@code false} if the 150 | * circle marker with the specified identifier was not found. 151 | */ 152 | boolean removeCircleMarker(int id); 153 | } 154 | 155 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/listener/OnJLMapViewListener.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.listener; 2 | 3 | import io.github.makbn.jlmap.JLMapView; 4 | import io.github.makbn.jlmap.listener.event.Event; 5 | import lombok.NonNull; 6 | 7 | 8 | public interface OnJLMapViewListener { 9 | 10 | /** 11 | * called after the map is fully loaded 12 | * 13 | * @param mapView loaded map 14 | */ 15 | void mapLoadedSuccessfully(@NonNull JLMapView mapView); 16 | 17 | /** 18 | * called after the map got an exception on loading 19 | */ 20 | void mapFailed(); 21 | 22 | default void onAction(Event event) { 23 | 24 | } 25 | 26 | 27 | enum Action { 28 | /** 29 | * zoom level changes continuously 30 | */ 31 | ZOOM, 32 | /** 33 | * zoom level stats to change 34 | */ 35 | ZOOM_START, 36 | /** 37 | * zoom leve changes end 38 | */ 39 | ZOOM_END, 40 | 41 | /** 42 | * map center changes continuously 43 | */ 44 | MOVE, 45 | /** 46 | * user starts to move the map 47 | */ 48 | MOVE_START, 49 | /** 50 | * user ends to move the map 51 | */ 52 | MOVE_END, 53 | /** 54 | * user click on the map 55 | */ 56 | CLICK 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/listener/OnJLObjectActionListener.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.listener; 2 | 3 | import io.github.makbn.jlmap.model.JLObject; 4 | import lombok.Getter; 5 | 6 | 7 | public abstract class OnJLObjectActionListener> { 8 | 9 | public abstract void click(T t, Action action); 10 | 11 | public abstract void move(T t, Action action); 12 | 13 | 14 | @Getter 15 | public enum Action { 16 | /** 17 | * Fired when the marker is moved via setLatLng or by dragging. 18 | * Old and new coordinates are included in event arguments as oldLatLng, 19 | * {@link io.github.makbn.jlmap.model.JLLatLng}. 20 | */ 21 | MOVE("move"), 22 | MOVE_START("movestart"), 23 | MOVE_END("moveend"), 24 | /** 25 | * Fired when the user clicks (or taps) the layer. 26 | */ 27 | CLICK("click"), 28 | /** 29 | * Fired when the user zooms. 30 | */ 31 | ZOOM("zoom"); 32 | 33 | final String jsEventName; 34 | 35 | Action(String name) { 36 | this.jsEventName = name; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/listener/event/ClickEvent.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.listener.event; 2 | 3 | import io.github.makbn.jlmap.model.JLLatLng; 4 | 5 | public record ClickEvent(JLLatLng center) implements Event { 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/listener/event/Event.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.listener.event; 2 | 3 | public interface Event { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/listener/event/MoveEvent.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.listener.event; 2 | 3 | import io.github.makbn.jlmap.listener.OnJLMapViewListener; 4 | import io.github.makbn.jlmap.model.JLBounds; 5 | import io.github.makbn.jlmap.model.JLLatLng; 6 | 7 | /** 8 | * 9 | * @param action movement action 10 | * @param center coordinate of map 11 | * @param bounds bounds of map 12 | * @param zoomLevel zoom level of map 13 | */ 14 | public record MoveEvent(OnJLMapViewListener.Action action, JLLatLng center, 15 | JLBounds bounds, int zoomLevel) implements Event { 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/listener/event/ZoomEvent.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.listener.event; 2 | 3 | import io.github.makbn.jlmap.listener.OnJLMapViewListener; 4 | 5 | public record ZoomEvent(OnJLMapViewListener.Action action, int zoomLevel) 6 | implements Event { 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLBounds.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | import java.util.Objects; 8 | 9 | /** 10 | * Represents a rectangular geographical area on a map. 11 | * 12 | * @author Mehdi Akbarian Rastaghi (@makbn) 13 | */ 14 | @Getter 15 | @Setter 16 | @Builder 17 | public class JLBounds { 18 | /** 19 | * the north-east point of the bounds. 20 | */ 21 | private JLLatLng northEast; 22 | /** 23 | * the south-west point of the bounds. 24 | */ 25 | private JLLatLng southWest; 26 | 27 | /** 28 | * @return the west longitude of the bounds 29 | */ 30 | public double getWest() { 31 | return southWest.getLng(); 32 | } 33 | 34 | /** 35 | * @return the south latitude of the bounds 36 | */ 37 | public double getSouth() { 38 | return southWest.getLat(); 39 | } 40 | 41 | /** 42 | * @return the east longitude of the bounds 43 | */ 44 | public double getEast() { 45 | return northEast.getLng(); 46 | } 47 | 48 | /** 49 | * @return the north latitude of the bounds 50 | */ 51 | public double getNorth() { 52 | return northEast.getLat(); 53 | } 54 | 55 | /** 56 | * @return the south-east point of the bounds 57 | */ 58 | public JLLatLng getSouthEast() { 59 | return JLLatLng.builder() 60 | .lat(southWest.getLat()) 61 | .lng(northEast.getLng()) 62 | .build(); 63 | } 64 | 65 | /** 66 | * @return the north-west point of the bounds. 67 | */ 68 | public JLLatLng getNorthWest() { 69 | return JLLatLng.builder() 70 | .lat(northEast.getLat()) 71 | .lng(southWest.getLng()) 72 | .build(); 73 | } 74 | 75 | public JLLatLng getCenter() { 76 | return JLLatLng.builder() 77 | .lat((northEast.getLat() + southWest.getLat()) / 2) 78 | .lng((northEast.getLng() + southWest.getLng()) / 2) 79 | .build(); 80 | } 81 | 82 | /** 83 | * @return a string with bounding box coordinates in a 84 | * 'southwest_lng,southwest_lat,northeast_lng,northeast_lat' format. 85 | * Useful for sending requests to web services that return geo data. 86 | */ 87 | public String toBBoxString() { 88 | return String.format("%f,%f,%f,%f", southWest.getLng(), 89 | southWest.getLat(), northEast.getLng(), northEast.getLat()); 90 | } 91 | 92 | /** 93 | * @param bounds given bounds 94 | * @return {@code true} if the rectangle contains the given bounds. 95 | */ 96 | public boolean contains(JLBounds bounds) { 97 | return (bounds.getSouthWest().getLat() >= southWest.getLat()) 98 | && (bounds.getNorthEast().getLat() <= northEast.getLat()) 99 | && (bounds.getSouthWest().getLng() >= southWest.getLng()) 100 | && (bounds.getNorthEast().getLng() <= northEast.getLng()); 101 | } 102 | 103 | /** 104 | * @param point given point 105 | * @return {@code true} if the rectangle contains the given point. 106 | */ 107 | public boolean contains(JLLatLng point) { 108 | return (point.getLat() >= southWest.getLat()) 109 | && (point.getLat() <= northEast.getLat()) 110 | && (point.getLng() >= southWest.getLng()) 111 | && (point.getLng() <= northEast.getLng()); 112 | } 113 | 114 | /** 115 | * @return {{@link Boolean#TRUE}} if the bounds are properly initialized. 116 | */ 117 | public boolean isValid() { 118 | return northEast != null && southWest != null; 119 | } 120 | 121 | /** 122 | * @param bufferRatio extending or retracting value 123 | * @return bounds created by extending or retracting the current bounds 124 | * by a given ratio in each direction. For example, a ratio of 0.5 extends 125 | * the bounds by 50% in each direction. 126 | * Negative values will retract the bounds. 127 | */ 128 | public JLBounds pad(double bufferRatio) { 129 | double latBuffer = 130 | Math.abs(southWest.getLat() - northEast.getLat()) * bufferRatio; 131 | double lngBuffer = 132 | Math.abs(southWest.getLng() - northEast.getLng()) * bufferRatio; 133 | 134 | return new JLBounds( 135 | new JLLatLng(southWest.getLat() - latBuffer, 136 | southWest.getLng() - lngBuffer), 137 | new JLLatLng(northEast.getLat() + latBuffer, 138 | northEast.getLng() + lngBuffer)); 139 | } 140 | 141 | /** 142 | * @param bounds the given bounds 143 | * @param maxMargin The margin of error 144 | * @return true if the rectangle is equivalent 145 | * (within a small margin of error) to the given bounds. 146 | */ 147 | public boolean equals(JLBounds bounds, float maxMargin) { 148 | if (bounds == null) { 149 | return false; 150 | } 151 | 152 | return this.getSouthWest().equals(bounds.getSouthWest(), maxMargin) && 153 | this.getNorthEast().equals(bounds.getNorthEast(), maxMargin); 154 | } 155 | 156 | @Override 157 | public boolean equals(Object o) { 158 | if (this == o) return true; 159 | if (o == null || getClass() != o.getClass()) return false; 160 | JLBounds jlBounds = (JLBounds) o; 161 | return Objects.equals(northEast, jlBounds.northEast) && 162 | Objects.equals(southWest, jlBounds.southWest); 163 | } 164 | 165 | @Override 166 | public int hashCode() { 167 | return Objects.hash(northEast, southWest); 168 | } 169 | 170 | @Override 171 | public String toString() { 172 | return String.format("[%s, %s]", northEast, southWest); 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLCircle.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import lombok.*; 4 | 5 | /** 6 | * A class for drawing circle overlays on a map 7 | * @author Mehdi Akbarian Rastaghi (@makbn) 8 | */ 9 | @Getter 10 | @Setter 11 | @Builder 12 | @AllArgsConstructor 13 | @ToString 14 | public class JLCircle extends JLObject { 15 | /** 16 | * id of object! this is an internal id for JLMap Application and not 17 | * related to Leaflet! 18 | */ 19 | protected int id; 20 | /** 21 | * Radius of the circle, in meters. 22 | */ 23 | private double radius; 24 | /** 25 | * Coordinates of the JLMarker on the map 26 | */ 27 | private JLLatLng latLng; 28 | /** 29 | * theming options for JLCircle. all options are not available! 30 | */ 31 | private JLOptions options; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLCircleMarker.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import lombok.*; 4 | 5 | /** 6 | * A circle of a fixed size with radius specified in pixels. 7 | * @author Mehdi Akbarian Rastaghi (@makbn) 8 | */ 9 | @Getter 10 | @Setter 11 | @Builder 12 | @AllArgsConstructor 13 | @ToString 14 | public class JLCircleMarker extends JLObject { 15 | /** 16 | * id of object! this is an internal id for JLMap Application and not 17 | * related to Leaflet! 18 | */ 19 | protected int id; 20 | /** 21 | * Radius of the circle, in meters. 22 | */ 23 | private double radius; 24 | /** 25 | * Coordinates of the JLCircleMarker on the map 26 | */ 27 | private JLLatLng latLng; 28 | /** 29 | * theming options for JLCircleMarker. all options are not available! 30 | */ 31 | private JLOptions options; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLLatLng.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import io.github.makbn.jlmap.JLProperties; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Builder; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | 9 | import java.util.Objects; 10 | 11 | /** 12 | * Represents a geographical point with a certain latitude and longitude. 13 | * @author Mehdi Akbarian Rastaghi (@makbn) 14 | */ 15 | @Getter 16 | @Setter 17 | @Builder 18 | @AllArgsConstructor 19 | public class JLLatLng { 20 | /** geographical given latitude in degrees */ 21 | private final double lat; 22 | /** geographical given longitude in degrees */ 23 | private final double lng; 24 | 25 | /** 26 | * Calculate distance between two points in latitude and longitude taking 27 | * into account height difference.Uses Haversine method as its base. 28 | * @param dest Destination coordinate {{@link JLLatLng}} 29 | * @return Distance in Meters 30 | * @author David George 31 | */ 32 | public double distanceTo(JLLatLng dest){ 33 | double latDistance = Math.toRadians(dest.getLat() - lat); 34 | double lonDistance = Math.toRadians(dest.getLng() - lng); 35 | double a = Math.sin(latDistance / 2) 36 | * Math.sin(latDistance / 2) 37 | + Math.cos(Math.toRadians(lat)) 38 | * Math.cos(Math.toRadians(dest.getLat())) 39 | * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2); 40 | double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 41 | double distance = JLProperties.EARTH_RADIUS * c * 1000; 42 | 43 | distance = Math.pow(distance, 2); 44 | return Math.sqrt(distance); 45 | } 46 | 47 | /** 48 | * 49 | * @param o The given point 50 | * @return Returns true if the given {{@link JLLatLng}} point is exactly 51 | * at the same position. 52 | */ 53 | @Override 54 | public boolean equals(Object o) { 55 | if (this == o) return true; 56 | if (o == null || getClass() != o.getClass()) return false; 57 | JLLatLng latLng = (JLLatLng) o; 58 | return Double.compare(latLng.lat, lat) == 0 && 59 | Double.compare(latLng.lng, lng) == 0; 60 | } 61 | 62 | /** 63 | * 64 | * @param o The given point 65 | * @param maxMargin The margin of error 66 | * @return Returns true if the given {{@link JLLatLng}} point is at the 67 | * same position (within a small margin of error). 68 | * The margin of error can be overridden by setting maxMargin. 69 | */ 70 | public boolean equals(Object o, float maxMargin) { 71 | if (this == o) return true; 72 | if (o == null || getClass() != o.getClass()) return false; 73 | JLLatLng latLng = (JLLatLng) o; 74 | return distanceTo(latLng) <= maxMargin; 75 | } 76 | 77 | @Override 78 | public int hashCode() { 79 | return Objects.hash(lat, lng); 80 | } 81 | 82 | @Override 83 | public String toString() { 84 | return String.format("[%f, %f]", lat, lng); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLMapOption.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import io.github.makbn.jlmap.JLProperties; 4 | import lombok.Builder; 5 | import lombok.NonNull; 6 | import lombok.Value; 7 | 8 | import java.util.HashSet; 9 | import java.util.Set; 10 | import java.util.stream.Collectors; 11 | import java.util.stream.Stream; 12 | 13 | /** 14 | * The {@code JLMapOption} class represents options for configuring a Leaflet 15 | * map. It is designed to be used with the Builder pattern and immutability 16 | * provided by Lombok's {@code @Builder} and {@code @Value} annotations. 17 | * This class allows you to specify various map configuration parameters, 18 | * such as the starting coordinates, map type, and additional parameters. 19 | * 20 | * @author Mehdi Akbarian Rastaghi (@makbn) 21 | */ 22 | @Builder 23 | @Value 24 | public class JLMapOption { 25 | 26 | /** 27 | * The starting geographical coordinates (latitude and longitude) 28 | * for the map. 29 | * Default value is (0.00, 0.00). 30 | */ 31 | @Builder.Default 32 | @NonNull 33 | JLLatLng startCoordinate = JLLatLng.builder() 34 | .lat(0.00) 35 | .lng(0.00) 36 | .build(); 37 | /** 38 | * The map type for configuring the map's appearance and behavior. 39 | * Default value is the default map type. 40 | */ 41 | @Builder.Default 42 | @NonNull 43 | JLProperties.MapType mapType = JLProperties.MapType.getDefault(); 44 | 45 | /** 46 | * Converts the map options to a query string format, including both 47 | * map-specific parameters and additional parameters. 48 | * 49 | * @return The map options as a query string. 50 | */ 51 | @NonNull 52 | public String toQueryString() { 53 | return Stream.concat( 54 | getParameters().stream(), additionalParameter.stream()) 55 | .map(Parameter::toString) 56 | .collect(Collectors.joining("&", 57 | String.format("?mapid=%s&", getMapType().name()), "")); 58 | } 59 | 60 | /** 61 | * Additional parameters to include in the map configuration. 62 | */ 63 | @Builder.Default 64 | Set additionalParameter = new HashSet<>(); 65 | 66 | /** 67 | * Gets the map-specific parameters based on the selected map type. 68 | * 69 | * @return A set of map-specific parameters. 70 | */ 71 | public Set getParameters() { 72 | return mapType.parameters(); 73 | } 74 | 75 | /** 76 | * Represents a key-value pair used for additional parameters in the map 77 | * configuration. 78 | */ 79 | public record Parameter(String key, String value) { 80 | @Override 81 | public String toString() { 82 | return String.format("%s=%s", key, value); 83 | } 84 | } 85 | } 86 | 87 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLMarker.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | 4 | import io.github.makbn.jlmap.listener.OnJLObjectActionListener; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Builder; 7 | import lombok.Data; 8 | import lombok.EqualsAndHashCode; 9 | 10 | /** 11 | * JLMarker is used to display clickable/draggable icons on the map! 12 | * 13 | * @author Mehdi Akbarian Rastaghi (@makbn) 14 | */ 15 | @Data 16 | @Builder 17 | @AllArgsConstructor 18 | @EqualsAndHashCode(callSuper = true) 19 | public class JLMarker extends JLObject { 20 | /** 21 | * id of object! this is an internal id for JLMap Application and not 22 | * related to Leaflet! 23 | */ 24 | protected int id; 25 | /** 26 | * optional text for showing on created JLMarker tooltip. 27 | */ 28 | private String text; 29 | /** 30 | * Coordinates of the JLMarker on the map 31 | */ 32 | private JLLatLng latLng; 33 | 34 | 35 | @Override 36 | public void update(Object... params) { 37 | super.update(params); 38 | if (params != null && params.length > 0 39 | && String.valueOf(params[0]).equals( 40 | OnJLObjectActionListener.Action.MOVE_END.getJsEventName()) 41 | && params[1] != null) { 42 | latLng = (JLLatLng) params[1]; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLMultiPolyline.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import lombok.*; 4 | 5 | /** 6 | * A class for drawing polyline overlays on a map 7 | * @author Mehdi Akbarian Rastaghi (@makbn) 8 | */ 9 | @Getter 10 | @Setter 11 | @AllArgsConstructor 12 | @Builder 13 | @ToString 14 | public class JLMultiPolyline extends JLObject { 15 | /** 16 | * id of JLMultiPolyline! this is an internal id for JLMap Application 17 | * and not related to Leaflet! 18 | */ 19 | private int id; 20 | /** 21 | * theming options for JLMultiPolyline. all options are not available! 22 | */ 23 | private JLOptions options; 24 | /** 25 | * The array of {@link io.github.makbn.jlmap.model.JLLatLng} points 26 | * of JLMultiPolyline 27 | */ 28 | private JLLatLng[][] vertices; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLObject.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import io.github.makbn.jlmap.listener.OnJLObjectActionListener; 4 | 5 | /** 6 | * Represents basic object classes for interacting with Leaflet 7 | * @author Mehdi Akbarian Rastaghi (@makbn) 8 | */ 9 | public abstract class JLObject> { 10 | private OnJLObjectActionListener listener; 11 | 12 | public OnJLObjectActionListener getOnActionListener() { 13 | return listener; 14 | } 15 | 16 | public void setOnActionListener(OnJLObjectActionListener listener) { 17 | this.listener = listener; 18 | } 19 | 20 | public abstract int getId(); 21 | 22 | public void update(Object... params) { 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLOptions.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import javafx.scene.paint.Color; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Builder; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | 9 | /** 10 | * Optional value for theming objects inside the map! 11 | * Note that all options are not available for all objects! 12 | * Read more at Leaflet Official Docs. 13 | * @author Mehdi Akbarian Rastaghi (@makbn) 14 | */ 15 | @Getter 16 | @Setter 17 | @Builder 18 | @AllArgsConstructor 19 | public class JLOptions { 20 | 21 | /** Default value for theming options. */ 22 | public static final JLOptions DEFAULT = JLOptions.builder().build(); 23 | 24 | /** Stroke color. Default is {{@link Color#BLUE}} */ 25 | @Builder.Default 26 | private Color color = Color.BLUE; 27 | 28 | /** Fill color. Default is {{@link Color#BLUE}} */ 29 | @Builder.Default 30 | private Color fillColor = Color.BLUE; 31 | 32 | /** Stroke width in pixels. Default is 3 */ 33 | @Builder.Default 34 | private int weight = 3; 35 | 36 | /** 37 | * Whether to draw stroke along the path. Set it to false for disabling 38 | * borders on polygons or circles. 39 | */ 40 | @Builder.Default 41 | private boolean stroke = true; 42 | 43 | /** Whether to fill the path with color. Set it to false fo disabling 44 | * filling on polygons or circles. 45 | */ 46 | @Builder.Default 47 | private boolean fill = true; 48 | 49 | /** Stroke opacity */ 50 | @Builder.Default 51 | private double opacity = 1.0; 52 | 53 | /** Fill opacity. */ 54 | @Builder.Default 55 | private double fillOpacity = 0.2; 56 | 57 | /** How much to simplify the polyline on each zoom level. 58 | * greater value means better performance and smoother 59 | * look, and smaller value means more accurate representation. 60 | */ 61 | @Builder.Default 62 | private double smoothFactor = 1.0; 63 | 64 | /** Controls the presence of a close button in the popup. 65 | */ 66 | @Builder.Default 67 | private boolean closeButton = true; 68 | 69 | /** Set it to false if you want to override the default behavior 70 | * of the popup closing when another popup is opened. 71 | */ 72 | @Builder.Default 73 | private boolean autoClose = true; 74 | 75 | /** Whether the marker is draggable with mouse/touch or not. 76 | */ 77 | @Builder.Default 78 | private boolean draggable = false; 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLPolygon.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import lombok.*; 4 | 5 | /** 6 | * A class for drawing polygon overlays on the map. 7 | * Note that points you pass when creating a polygon shouldn't 8 | * have an additional last point equal to the first one. 9 | * @author Mehdi Akbarian Rastaghi (@makbn) 10 | */ 11 | @Getter 12 | @Setter 13 | @AllArgsConstructor 14 | @Builder 15 | @ToString 16 | public class JLPolygon extends JLObject { 17 | /** 18 | * id of JLPolygon! this is an internal id for JLMap Application and not 19 | * related to Leaflet! 20 | */ 21 | private int id; 22 | /** 23 | * theming options for JLMultiPolyline. all options are not available! 24 | */ 25 | private JLOptions options; 26 | 27 | /** 28 | * The arrays of latlngs, with the first array representing the outer 29 | * shape and the other arrays representing holes in the outer shape. 30 | * Additionally, you can pass a multi-dimensional array to represent 31 | * a MultiPolygon shape. 32 | */ 33 | private JLLatLng[][][] vertices; 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLPolyline.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import lombok.*; 4 | 5 | /** 6 | * A class for drawing polyline overlays on the map. 7 | * @author Mehdi Akbarian Rastaghi (@makbn) 8 | */ 9 | @Getter 10 | @Setter 11 | @AllArgsConstructor 12 | @Builder 13 | @ToString 14 | public class JLPolyline extends JLObject { 15 | /** 16 | * id of JLPolyline! this is an internal id for JLMap Application and not 17 | * related to Leaflet! 18 | */ 19 | private int id; 20 | /** 21 | * theming options for JLPolyline. all options are not available! 22 | */ 23 | private JLOptions options; 24 | /** 25 | * The array of {@link io.github.makbn.jlmap.model.JLLatLng} points of 26 | * JLPolyline 27 | */ 28 | private JLLatLng[] vertices; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/io/github/makbn/jlmap/model/JLPopup.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import lombok.*; 4 | 5 | /** 6 | * Used to open popups in certain places of the map. 7 | * @author Mehdi Akbarian Rastaghi (@makbn) 8 | */ 9 | @Getter 10 | @Setter 11 | @AllArgsConstructor 12 | @Builder 13 | @ToString 14 | public class JLPopup { 15 | /** 16 | * id of JLPopup! this is an internal id for JLMap Application and not 17 | * related to Leaflet! 18 | */ 19 | private int id; 20 | /** Content of the popup.*/ 21 | private String text; 22 | /** Coordinates of the popup on the map. */ 23 | private JLLatLng latLng; 24 | /** Theming options for JLPopup. all options are not available! */ 25 | private JLOptions options; 26 | } 27 | -------------------------------------------------------------------------------- /src/main/resources/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/makbn/java_leaflet/5026f523a1ceb4e34d46e4ba9efbe40f90780aa6/src/main/resources/.DS_Store -------------------------------------------------------------------------------- /src/main/resources/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Java - Leaflet 4 | 5 | 6 | 8 | 10 | 11 | 97 | 98 | 99 |

102 |
103 |
104 |
105 |
106 | 107 |
Java-Leaflet 110 | | Map data © OpenStreetMap contributors, CC-BY-SA 112 |
113 |
114 |
115 |
116 | 343 | 344 | -------------------------------------------------------------------------------- /src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/test/java/io/github/makbn/jlmap/Leaflet.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap; 2 | 3 | import io.github.makbn.jlmap.geojson.JLGeoJsonObject; 4 | import io.github.makbn.jlmap.listener.OnJLMapViewListener; 5 | import io.github.makbn.jlmap.listener.OnJLObjectActionListener; 6 | import io.github.makbn.jlmap.listener.event.ClickEvent; 7 | import io.github.makbn.jlmap.listener.event.Event; 8 | import io.github.makbn.jlmap.listener.event.MoveEvent; 9 | import io.github.makbn.jlmap.listener.event.ZoomEvent; 10 | import io.github.makbn.jlmap.model.JLLatLng; 11 | import io.github.makbn.jlmap.model.JLMarker; 12 | import io.github.makbn.jlmap.model.JLOptions; 13 | import io.github.makbn.jlmap.model.JLPolygon; 14 | import javafx.application.Application; 15 | import javafx.geometry.Rectangle2D; 16 | import javafx.scene.Scene; 17 | import javafx.scene.layout.AnchorPane; 18 | import javafx.scene.layout.Background; 19 | import javafx.scene.paint.Color; 20 | import javafx.stage.Screen; 21 | import javafx.stage.Stage; 22 | import lombok.NonNull; 23 | import org.apache.logging.log4j.LogManager; 24 | import org.apache.logging.log4j.Logger; 25 | 26 | 27 | /** 28 | * @author Mehdi Akbarian Rastaghi (@makbn) 29 | */ 30 | public class Leaflet extends Application { 31 | static final Logger log = LogManager.getLogger(Leaflet.class); 32 | 33 | @Override 34 | public void start(Stage stage) { 35 | //building a new map view 36 | final JLMapView map = JLMapView 37 | .builder() 38 | .mapType(JLProperties.MapType.OSM_MAPNIK) 39 | .showZoomController(true) 40 | .startCoordinate(JLLatLng.builder() 41 | .lat(51.044) 42 | .lng(114.07) 43 | .build()) 44 | .build(); 45 | //creating a window 46 | AnchorPane root = new AnchorPane(map); 47 | root.setBackground(Background.EMPTY); 48 | root.setMinHeight(JLProperties.INIT_MIN_HEIGHT_STAGE); 49 | root.setMinWidth(JLProperties.INIT_MIN_WIDTH_STAGE); 50 | Scene scene = new Scene(root); 51 | 52 | stage.setMinHeight(JLProperties.INIT_MIN_HEIGHT_STAGE); 53 | stage.setMinWidth(JLProperties.INIT_MIN_WIDTH_STAGE); 54 | scene.setFill(Color.TRANSPARENT); 55 | stage.setTitle("Java-Leaflet Test"); 56 | stage.setScene(scene); 57 | stage.show(); 58 | 59 | Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds(); 60 | stage.setX((primScreenBounds.getWidth() - stage.getWidth()) / 2); 61 | stage.setY(100); 62 | 63 | //set listener fo map events 64 | map.setMapListener(new OnJLMapViewListener() { 65 | @Override 66 | public void mapLoadedSuccessfully(@NonNull JLMapView mapView) { 67 | log.info("map loaded!"); 68 | addMultiPolyline(map); 69 | addPolyline(map); 70 | addPolygon(map); 71 | 72 | map.setView(JLLatLng.builder() 73 | .lng(10) 74 | .lat(10) 75 | .build()); 76 | map.getUiLayer() 77 | .addMarker(JLLatLng.builder() 78 | .lat(35.63) 79 | .lng(51.45) 80 | .build(), "Tehran", true) 81 | .setOnActionListener(getListener()); 82 | 83 | map.getVectorLayer() 84 | .addCircleMarker(JLLatLng.builder() 85 | .lat(35.63) 86 | .lng(40.45) 87 | .build()); 88 | 89 | map.getVectorLayer() 90 | .addCircle(JLLatLng.builder() 91 | .lat(35.63) 92 | .lng(51.45) 93 | .build(), 30000, JLOptions.DEFAULT); 94 | 95 | // map zoom functionalities 96 | map.getControlLayer().setZoom(5); 97 | map.getControlLayer().zoomIn(2); 98 | map.getControlLayer().zoomOut(1); 99 | 100 | 101 | JLGeoJsonObject geoJsonObject = map.getGeoJsonLayer() 102 | .addFromUrl("https://pkgstore.datahub.io/examples/geojson-tutorial/example/data/db696b3bf628d9a273ca9907adcea5c9/example.geojson"); 103 | 104 | } 105 | 106 | @Override 107 | public void mapFailed() { 108 | log.error("map failed!"); 109 | } 110 | 111 | @Override 112 | public void onAction(Event event) { 113 | if (event instanceof MoveEvent moveEvent) { 114 | log.info("move event: " + moveEvent.action() + " c:" + moveEvent.center() 115 | + " \t bounds:" + moveEvent.bounds() + "\t z:" + moveEvent.zoomLevel()); 116 | } else if (event instanceof ClickEvent clickEvent) { 117 | log.info("click event: " + clickEvent.center()); 118 | map.getUiLayer().addPopup(clickEvent.center(), "New Click Event!", JLOptions.builder() 119 | .closeButton(false) 120 | .autoClose(false).build()); 121 | } else if (event instanceof ZoomEvent zoomEvent) { 122 | log.info("zoom event: " + zoomEvent.zoomLevel()); 123 | } 124 | 125 | 126 | } 127 | }); 128 | } 129 | 130 | private OnJLObjectActionListener getListener() { 131 | return new OnJLObjectActionListener() { 132 | @Override 133 | public void click(JLMarker object, Action action) { 134 | log.info("object click listener for marker:" + object); 135 | } 136 | 137 | @Override 138 | public void move(JLMarker object, Action action) { 139 | log.info("object move listener for marker:" + object); 140 | } 141 | }; 142 | } 143 | 144 | private void addMultiPolyline(JLMapView map) { 145 | JLLatLng[][] verticesT = new JLLatLng[2][]; 146 | 147 | verticesT[0] = new JLLatLng[]{ 148 | new JLLatLng(41.509, 20.08), 149 | new JLLatLng(31.503, -10.06), 150 | new JLLatLng(21.51, -0.047) 151 | }; 152 | 153 | verticesT[1] = new JLLatLng[]{ 154 | new JLLatLng(51.509, 10.08), 155 | new JLLatLng(55.503, 15.06), 156 | new JLLatLng(42.51, 20.047) 157 | }; 158 | 159 | map.getVectorLayer().addMultiPolyline(verticesT); 160 | } 161 | 162 | private void addPolyline(JLMapView map) { 163 | JLLatLng[] vertices = new JLLatLng[]{ 164 | new JLLatLng(51.509, -0.08), 165 | new JLLatLng(51.503, -0.06), 166 | new JLLatLng(51.51, -0.047) 167 | }; 168 | 169 | map.getVectorLayer().addPolyline(vertices); 170 | } 171 | 172 | private void addPolygon(JLMapView map) { 173 | 174 | JLLatLng[][][] vertices = new JLLatLng[2][][]; 175 | 176 | vertices[0] = new JLLatLng[2][]; 177 | vertices[1] = new JLLatLng[1][]; 178 | //first part 179 | vertices[0][0] = new JLLatLng[]{ 180 | new JLLatLng(37, -109.05), 181 | new JLLatLng(41, -109.03), 182 | new JLLatLng(41, -102.05), 183 | new JLLatLng(37, -102.04) 184 | }; 185 | //hole inside the first part 186 | vertices[0][1] = new JLLatLng[]{ 187 | new JLLatLng(37.29, -108.58), 188 | new JLLatLng(40.71, -108.58), 189 | new JLLatLng(40.71, -102.50), 190 | new JLLatLng(37.29, -102.50) 191 | }; 192 | //second part 193 | vertices[1][0] = new JLLatLng[]{ 194 | new JLLatLng(41, -111.03), 195 | new JLLatLng(45, -111.04), 196 | new JLLatLng(45, -104.05), 197 | new JLLatLng(41, -104.05) 198 | }; 199 | map.getVectorLayer().addPolygon(vertices).setOnActionListener(new OnJLObjectActionListener() { 200 | @Override 201 | public void click(JLPolygon jlPolygon, Action action) { 202 | log.info("object click listener for jlPolygon:" + jlPolygon); 203 | } 204 | 205 | @Override 206 | public void move(JLPolygon jlPolygon, Action action) { 207 | log.info("object move listener for jlPolygon:" + jlPolygon); 208 | } 209 | }); 210 | } 211 | } -------------------------------------------------------------------------------- /src/test/java/io/github/makbn/jlmap/model/JLBoundsTest.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.DisplayName; 5 | import org.junit.jupiter.api.Tag; 6 | import org.junit.jupiter.api.Test; 7 | import org.junit.jupiter.api.extension.ExtensionContext; 8 | import org.junit.jupiter.params.ParameterizedTest; 9 | import org.junit.jupiter.params.provider.Arguments; 10 | import org.junit.jupiter.params.provider.ArgumentsProvider; 11 | import org.junit.jupiter.params.provider.ArgumentsSource; 12 | 13 | import java.util.stream.Stream; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; 16 | 17 | @Tag("JLBounds") 18 | class JLBoundsTest implements ModelTest { 19 | 20 | public static class TestArgumentProvider implements ArgumentsProvider { 21 | 22 | private JLLatLng latLng(Double lat, Double lng) { 23 | return JLLatLng.builder() 24 | .lat(lat) 25 | .lng(lng) 26 | .build(); 27 | } 28 | 29 | @Override 30 | public Stream provideArguments(ExtensionContext extensionContext) { 31 | if (extensionContext.getTags().contains("test_get_center")) { 32 | return Stream.of( 33 | Arguments.of(latLng(51.03, -124.48), latLng(58.23, -110.76), latLng(54.63, -117.62)), 34 | Arguments.of(latLng(50.92, -114.12), latLng(51.16, -113.9), latLng(51.04, -114.01)) 35 | ); 36 | } else if (extensionContext.getTags().contains("test_get_direction")) { 37 | return Stream.of( 38 | Arguments.of(JLBounds.builder().southWest(latLng(51.03, -124.48)).northEast(latLng(58.23, -110.76)).build(), -124.48, 58.23, -110.76, 51.03), 39 | Arguments.of(JLBounds.builder().southWest(latLng(50.92, -114.12)).northEast(latLng(51.16, -113.9)).build(), -114.12, 51.16, -113.9, 50.92) 40 | ); 41 | } 42 | return Stream.empty(); 43 | } 44 | } 45 | 46 | @Test 47 | @DisplayName("Test toString output format") 48 | void testToString() { 49 | JLBounds bounds = JLBounds.builder() 50 | .northEast(JLLatLng.builder().lat(10).lng(10).build()) 51 | .southWest(JLLatLng.builder().lat(40).lng(60).build()) 52 | .build(); 53 | 54 | assertEquals("[[10.000000, 10.000000], [40.000000, 60.000000]]", bounds.toString()); 55 | } 56 | 57 | @Test 58 | @DisplayName("Test toBBoxString output format") 59 | void testBBox() { 60 | JLBounds bounds = JLBounds.builder() 61 | .northEast(JLLatLng.builder().lat(10).lng(20).build()) 62 | .southWest(JLLatLng.builder().lat(40).lng(60).build()) 63 | .build(); 64 | 65 | assertEquals("60.000000,40.000000,20.000000,10.000000", bounds.toBBoxString()); 66 | } 67 | 68 | @Tag("test_get_center") 69 | @ParameterizedTest(name = "SW: {0}, NE: {1} Center: {2}") 70 | @ArgumentsSource(TestArgumentProvider.class) 71 | @DisplayName("Test getCenter method to find the center of a bound") 72 | void testGetCenter(JLLatLng sw, JLLatLng ne, JLLatLng expectedCenter) { 73 | JLLatLng calculatedCenter = JLBounds.builder() 74 | .southWest(sw) 75 | .northEast(ne) 76 | .build() 77 | .getCenter(); 78 | 79 | Assertions.assertTrue(calculatedCenter.distanceTo(expectedCenter) / 1000 < DISTANCE_ERROR_KM); 80 | } 81 | 82 | @Tag("test_get_direction") 83 | @ParameterizedTest(name = "Point: {0}") 84 | @ArgumentsSource(TestArgumentProvider.class) 85 | @DisplayName("Test get directions of a bound") 86 | void testGetDirections(JLBounds bounds, double west, double north, double east, double south) { 87 | Assertions.assertEquals(west, bounds.getWest()); 88 | Assertions.assertEquals(north, bounds.getNorth()); 89 | Assertions.assertEquals(east, bounds.getEast()); 90 | Assertions.assertEquals(south, bounds.getSouth()); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/test/java/io/github/makbn/jlmap/model/JLLatLngTest.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.DisplayName; 5 | import org.junit.jupiter.api.Tag; 6 | import org.junit.jupiter.api.Test; 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | import org.junit.jupiter.params.provider.CsvSource; 9 | 10 | 11 | @Tag("JLLatLng") 12 | class JLLatLngTest implements ModelTest { 13 | 14 | @Test 15 | @DisplayName("Test the equals method for exact similar points") 16 | void testEquals() { 17 | JLLatLng pointA = JLLatLng.builder() 18 | .lat(10) 19 | .lng(20) 20 | .build(); 21 | 22 | JLLatLng pointB = JLLatLng.builder() 23 | .lat(10.000) 24 | .lng(20.00) 25 | .build(); 26 | Assertions.assertEquals(pointA, pointB); 27 | } 28 | 29 | @Test 30 | @DisplayName("Test the equals method for non-similar points") 31 | void testNotEquals() { 32 | JLLatLng pointA = JLLatLng.builder() 33 | .lat(10) 34 | .lng(20) 35 | .build(); 36 | 37 | JLLatLng pointB = JLLatLng.builder() 38 | .lat(20) 39 | .lng(10) 40 | .build(); 41 | 42 | Assertions.assertNotEquals(pointA, pointB); 43 | } 44 | 45 | @ParameterizedTest(name = "Point A(lat: {0} lng:{1}), Point B(lat: {2}, lng: {3}), Distance: {4}") 46 | @DisplayName("Test the equals method with margin for similar points") 47 | @CsvSource({ 48 | "10, 20, 10.0001, 20", 49 | "10, 20.0001, 10, 20", 50 | "10.0001, 20.0001, 10, 20" 51 | }) 52 | void testNotEqualsWithError(double pointALat, double pointALng, double pointBLat, double pointBLng) { 53 | JLLatLng pointA = JLLatLng.builder() 54 | .lat(pointALat) 55 | .lng(pointALng) 56 | .build(); 57 | 58 | JLLatLng pointB = JLLatLng.builder() 59 | .lat(pointBLat) 60 | .lng(pointBLng) 61 | .build(); 62 | // max margin should be in meter, DISTANCE_ERROR is in Km 63 | Assertions.assertTrue(pointA.equals(pointB, DISTANCE_ERROR_M)); 64 | } 65 | 66 | @ParameterizedTest(name = "Point A(lat: {0} lng:{1}), Point B(lat: {2}, lng: {3}), Distance: {4}") 67 | @DisplayName("Test distance calculation in different directions") 68 | @CsvSource({ 69 | "10, 20, 10, 50, 3282", 70 | "50, 10, 20, 10, 3334", 71 | "50, 80, 30, 10, 6113" 72 | }) 73 | void testDistanceCalculation_lng(double pointALat, double pointALng, double pointBLat, double pointBLng, int distance) { 74 | JLLatLng pointA = JLLatLng.builder() 75 | .lat(pointALat) 76 | .lng(pointALng) 77 | .build(); 78 | 79 | JLLatLng pointB = JLLatLng.builder() 80 | .lat(pointBLat) 81 | .lng(pointBLng) 82 | .build(); 83 | 84 | Assertions.assertTrue(Math.abs(distance - Math.round(pointA.distanceTo(pointB) / 1000)) < DISTANCE_ERROR_KM); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/test/java/io/github/makbn/jlmap/model/JLMarkerTest.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import io.github.makbn.jlmap.listener.OnJLObjectActionListener; 4 | import org.junit.jupiter.api.*; 5 | 6 | import static org.junit.jupiter.api.Assertions.*; 7 | 8 | @Tag("JLMarker") 9 | class JLMarkerTest implements ModelTest{ 10 | private static final String MOVE_END_ACTION = OnJLObjectActionListener.Action.MOVE_END.getJsEventName(); 11 | 12 | private JLMarker jlMarker; 13 | 14 | @BeforeEach 15 | void setUp() { 16 | jlMarker = new JLMarker(0, "", null); 17 | } 18 | 19 | @Test 20 | @DisplayName("Test update by moveend action") 21 | void testUpdateWithMoveEndEventAndNonNullLatLng() { 22 | JLLatLng expectedLatLng = new JLLatLng(1.0, 2.0); 23 | jlMarker.update(MOVE_END_ACTION, expectedLatLng); 24 | assertEquals(expectedLatLng, jlMarker.getLatLng()); 25 | } 26 | 27 | @Test 28 | @DisplayName("Test update by moveend action with null point") 29 | void testUpdateWithMoveEndEventAndNullLatLng() { 30 | jlMarker.setLatLng(JLLatLng.builder().build()); 31 | jlMarker.update(MOVE_END_ACTION, null); 32 | assertNotNull(jlMarker.getLatLng()); 33 | } 34 | 35 | @Test 36 | @DisplayName("Test update by wrong action") 37 | void testUpdateWithNonMoveEndEvent() { 38 | JLLatLng originalLatLng = new JLLatLng(3.0, 4.0); 39 | jlMarker.setLatLng(originalLatLng); 40 | 41 | jlMarker.update("click", new Object()); 42 | assertEquals(originalLatLng, jlMarker.getLatLng()); 43 | } 44 | 45 | @Test 46 | @DisplayName("Test setId method") 47 | void testSetAndGetId() { 48 | int expectedId = 123; 49 | jlMarker.setId(expectedId); 50 | assertEquals(expectedId, jlMarker.getId()); 51 | } 52 | 53 | @Test 54 | @DisplayName("Test setText method") 55 | void testSetAndGetText() { 56 | String expectedText = "Marker Text"; 57 | jlMarker.setText(expectedText); 58 | assertEquals(expectedText, jlMarker.getText()); 59 | } 60 | 61 | @Test 62 | @DisplayName("Test setLatLng method") 63 | void testSetAndGetLatLng() { 64 | JLLatLng expectedLatLng = new JLLatLng(5.0, 6.0); 65 | jlMarker.setLatLng(expectedLatLng); 66 | assertEquals(expectedLatLng, jlMarker.getLatLng()); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/test/java/io/github/makbn/jlmap/model/ModelTest.java: -------------------------------------------------------------------------------- 1 | package io.github.makbn.jlmap.model; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Tag; 5 | 6 | 7 | @Tag("model") 8 | @DisplayName("Tests related to the model package") 9 | public interface ModelTest { 10 | float DISTANCE_ERROR_KM = 0.01F; 11 | float DISTANCE_ERROR_M = 20F; 12 | } 13 | --------------------------------------------------------------------------------