{"id":16687400,"url":"https://github.com/rodrigokamada/angular-leaflet","last_synced_at":"2025-04-10T00:20:59.876Z","repository":{"id":37459958,"uuid":"367414575","full_name":"rodrigokamada/angular-leaflet","owner":"rodrigokamada","description":"Application example built with Angular 15 and adding the map Leaflet component using the leaflet library.","archived":false,"fork":false,"pushed_at":"2023-04-17T23:30:20.000Z","size":6250,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-05T14:38:09.656Z","etag":null,"topics":["angular","article","beginners","dev-community","gh-actions","gh-pages","hacktoberfest","leaflet","map","stackblitz","tutorial"],"latest_commit_sha":null,"homepage":"https://rodrigokamada.github.io/angular-leaflet/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rodrigokamada.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"rodrigokamada"}},"created_at":"2021-05-14T16:04:06.000Z","updated_at":"2025-01-19T13:52:11.000Z","dependencies_parsed_at":"2023-01-17T14:45:26.411Z","dependency_job_id":null,"html_url":"https://github.com/rodrigokamada/angular-leaflet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrigokamada%2Fangular-leaflet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrigokamada%2Fangular-leaflet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrigokamada%2Fangular-leaflet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodrigokamada%2Fangular-leaflet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rodrigokamada","download_url":"https://codeload.github.com/rodrigokamada/angular-leaflet/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248132034,"owners_count":21052966,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","article","beginners","dev-community","gh-actions","gh-pages","hacktoberfest","leaflet","map","stackblitz","tutorial"],"created_at":"2024-10-12T15:09:02.288Z","updated_at":"2025-04-10T00:20:59.856Z","avatar_url":"https://github.com/rodrigokamada.png","language":"TypeScript","readme":"# Angular Leaflet\n\n\nApplication example built with [Angular](https://angular.io/) 15 and adding the map Leaflet component using the [leaflet](https://www.npmjs.com/package/leaflet) library.\n\nThis tutorial was posted on my [blog](https://rodrigo.kamada.com.br/blog/adicionando-o-componente-de-mapa-leaflet-em-uma-aplicacao-angular) in portuguese and on the [DEV Community](https://dev.to/rodrigokamada/adding-the-map-leaflet-component-to-an-angular-application-d13) in english.\n\n\n\n[![Website](https://shields.braskam.com/v1/shields?name=website\u0026format=rectangle\u0026size=small\u0026radius=5)](https://rodrigo.kamada.com.br)\n[![LinkedIn](https://shields.braskam.com/v1/shields?name=linkedin\u0026format=rectangle\u0026size=small\u0026radius=5)](https://www.linkedin.com/in/rodrigokamada)\n[![Twitter](https://shields.braskam.com/v1/shields?name=twitter\u0026format=rectangle\u0026size=small\u0026radius=5\u0026socialAccount=rodrigokamada)](https://twitter.com/rodrigokamada)\n[![Instagram](https://shields.braskam.com/v1/shields?name=instagram\u0026format=rectangle\u0026size=small\u0026radius=5)](https://www.instagram.com/rodrigokamada/)\n\n\n\n## Prerequisites\n\n\nBefore you start, you need to install and configure the tools:\n\n* [git](https://git-scm.com/)\n* [Node.js and npm](https://nodejs.org/)\n* [Angular CLI](https://angular.io/cli)\n* IDE (e.g. [Visual Studio Code](https://code.visualstudio.com/) or [WebStorm](https://www.jetbrains.com/webstorm/))\n\n\n\n## Getting started\n\n\n### Create and configure the account on the Mapbox\n\n\n**1.** Let's create the account. Access the site [https://www.mapbox.com/](https://www.mapbox.com/) and click on the button *Sign up*.\n\n![Mapbox - Home page](https://res.cloudinary.com/rodrigokamada/image/upload/v1637580196/Blog/angular-leaflet/mapbox-step1.png)\n\n**2.** Fill in the fields *Username*, *Email*, *Password*, *First name*, *Last name*, click on the checkbox *I agree to the Mapbox Terms of Service and Privacy Policy.* and click on the button *Get started*.\n\n![Mapbox - Sign up](https://res.cloudinary.com/rodrigokamada/image/upload/v1637580197/Blog/angular-leaflet/mapbox-step2.png)\n\n**3.** Check the registered email.\n\n![Mapbox - Check email](https://res.cloudinary.com/rodrigokamada/image/upload/v1637580197/Blog/angular-leaflet/mapbox-step3.png)\n\n**4.** Click on the link in the email sent.\n\n![Mapbox - Confirm email](https://res.cloudinary.com/rodrigokamada/image/upload/v1637580197/Blog/angular-leaflet/mapbox-step4.png)\n\n**5.** Copy the token displayed in the *Dashboard* menu and, in my case, the token was displayed `pk.eyJ1IjoiYnJhc2thbSIsImEiOiJja3NqcXBzbWoyZ3ZvMm5ybzA4N2dzaDR6In0.RUAYJFnNgOnnZAw` because this token will be configured in the Angular application.\n\n![Mapbox - Dashboard](https://res.cloudinary.com/rodrigokamada/image/upload/v1637580197/Blog/angular-leaflet/mapbox-step5.png)\n\n**6.** Ready! Account created and token generated.\n\n\n### Create the Angular application\n\n\n**1.** Let's create the application with the Angular base structure using the `@angular/cli` with the route file and the SCSS style format.\n\n```powershell\nng new angular-leaflet\n? Would you like to add Angular routing? Yes\n? Which stylesheet format would you like to use? SCSS   [ https://sass-lang.com/documentation/syntax#scss                ]\nCREATE angular-leaflet/README.md (1073 bytes)\nCREATE angular-leaflet/.editorconfig (274 bytes)\nCREATE angular-leaflet/.gitignore (604 bytes)\nCREATE angular-leaflet/angular.json (3339 bytes)\nCREATE angular-leaflet/package.json (1090 bytes)\nCREATE angular-leaflet/tsconfig.json (783 bytes)\nCREATE angular-leaflet/.browserslistrc (703 bytes)\nCREATE angular-leaflet/karma.conf.js (1445 bytes)\nCREATE angular-leaflet/tsconfig.app.json (287 bytes)\nCREATE angular-leaflet/tsconfig.spec.json (333 bytes)\nCREATE angular-leaflet/src/favicon.ico (948 bytes)\nCREATE angular-leaflet/src/index.html (313 bytes)\nCREATE angular-leaflet/src/main.ts (372 bytes)\nCREATE angular-leaflet/src/polyfills.ts (2820 bytes)\nCREATE angular-leaflet/src/styles.scss (80 bytes)\nCREATE angular-leaflet/src/test.ts (788 bytes)\nCREATE angular-leaflet/src/assets/.gitkeep (0 bytes)\nCREATE angular-leaflet/src/environments/environment.prod.ts (51 bytes)\nCREATE angular-leaflet/src/environments/environment.ts (658 bytes)\nCREATE angular-leaflet/src/app/app-routing.module.ts (245 bytes)\nCREATE angular-leaflet/src/app/app.module.ts (393 bytes)\nCREATE angular-leaflet/src/app/app.component.scss (0 bytes)\nCREATE angular-leaflet/src/app/app.component.html (24617 bytes)\nCREATE angular-leaflet/src/app/app.component.spec.ts (1139 bytes)\nCREATE angular-leaflet/src/app/app.component.ts (233 bytes)\n✔ Packages installed successfully.\n    Successfully initialized git.\n```\n\n**2.** Install and configure the Bootstrap CSS framework. Do steps 2 and 3 of the post *[Adding the Bootstrap CSS framework to an Angular application](https://github.com/rodrigokamada/angular-bootstrap)*.\n\n**3.** Configure the Mapbox token in the `src/environments/environment.ts` and `src/environments/environment.prod.ts` files as below.\n\n```typescript\nmapbox: {\n  accessToken: 'pk.eyJ1IjoiYnJhc2thbSIsImEiOiJja3NqcXBzbWoyZ3ZvMm5ybzA4N2dzaDR6In0.RUAYJFnNgOnn80wXkrV9ZA',\n},\n```\n\n**4.** Create the `src/assets/images` folder and copy the `marker-icon.png` and `marker-shadow.png` files.\n\n![Marker Icon](https://res.cloudinary.com/rodrigokamada/image/upload/v1637581626/Blog/angular-leaflet/marker-icon.png)\n![Marker Shadow](https://res.cloudinary.com/rodrigokamada/image/upload/v1637581626/Blog/angular-leaflet/marker-shadow.png)\n\n**5.** Install the `leaflet` and `@types/leaflet` libraries.\n\n```powershell\nnpm install leaflet @types/leaflet\n```\n\n**6.** Configure the `leaflet` library. Change the `angular.json` file and add the `leaflet.css` file as below.\n\n```json\n\"styles\": [\n  \"node_modules/bootstrap/scss/bootstrap.scss\",\n  \"node_modules/bootstrap-icons/font/bootstrap-icons.css\",\n  \"node_modules/leaflet/dist/leaflet.css\",\n  \"src/styles.scss\"\n],\n```\n\n**7.** Remove the contents of the `AppComponent` class from the `src/app/app.component.ts` file. Import the `leaflet` service and create the `getCurrentPosition`, `loadMap` methods as below.\n\n```typescript\nimport { AfterViewInit, Component } from '@angular/core';\nimport { Observable, Subscriber } from 'rxjs';\nimport * as L from 'leaflet';\n\nimport { environment } from '../environments/environment';\n\n@Component({\n  selector: 'app-root',\n  templateUrl: './app.component.html',\n  styleUrls: ['./app.component.scss'],\n})\nexport class AppComponent implements AfterViewInit {\n\n  map: any;\n\n  constructor() {\n  }\n\n  public ngAfterViewInit(): void {\n    this.loadMap();\n  }\n\n  private getCurrentPosition(): any {\n    return new Observable((observer: Subscriber\u003cany\u003e) =\u003e {\n      if (navigator.geolocation) {\n        navigator.geolocation.getCurrentPosition((position: any) =\u003e {\n          observer.next({\n            latitude: position.coords.latitude,\n            longitude: position.coords.longitude,\n          });\n          observer.complete();\n        });\n      } else {\n        observer.error();\n      }\n    });\n  }\n\n  private loadMap(): void {\n    this.map = L.map('map').setView([0, 0], 1);\n    L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {\n      attribution: 'Map data \u0026copy; \u003ca href=\"https://www.openstreetmap.org/copyright\"\u003eOpenStreetMap\u003c/a\u003e contributors, Imagery © \u003ca href=\"https://www.mapbox.com/\"\u003eMapbox\u003c/a\u003e',\n      maxZoom: 18,\n      id: 'mapbox/streets-v11',\n      tileSize: 512,\n      zoomOffset: -1,\n      accessToken: environment.mapbox.accessToken,\n    }).addTo(this.map);\n\n    this.getCurrentPosition()\n    .subscribe((position: any) =\u003e {\n      this.map.flyTo([position.latitude, position.longitude], 13);\n\n      const icon = L.icon({\n        iconUrl: 'assets/images/marker-icon.png',\n        shadowUrl: 'assets/images/marker-shadow.png',\n        popupAnchor: [13, 0],\n      });\n\n      const marker = L.marker([position.latitude, position.longitude], { icon }).bindPopup('Angular Leaflet');\n      marker.addTo(this.map);\n    });\n  }\n\n}\n```\n\n**8.** Remove the contents of the `src/app/app.component.html` file. Add the map `div` tag as below.\n\n```html\n\u003cdiv class=\"container-fluid py-3\"\u003e\n  \u003ch1\u003eAngular Leaflet\u003c/h1\u003e\n\n  \u003cdiv id=\"map\"\u003e\u003c/div\u003e\n\u003c/div\u003e\n```\n\n**9.** Add the style in the `src/app/app.component.scss` file as below.\n\n```css\n#map {\n  height: 400px;\n  width: 100%;\n  max-width: 600px;\n}\n```\n\n**10.** Run the application with the command below.\n\n```powershell\nnpm start\n\n\u003e angular-leaflet@1.0.0 start\n\u003e ng serve\n\n✔ Browser application bundle generation complete.\n\nInitial Chunk Files | Names         |      Size\nvendor.js           | vendor        |   2.81 MB\nstyles.css          | styles        | 280.54 kB\npolyfills.js        | polyfills     | 128.51 kB\nscripts.js          | scripts       |  76.67 kB\nmain.js             | main          |  12.03 kB\nruntime.js          | runtime       |   6.63 kB\n\n                    | Initial Total |   3.30 MB\n\nBuild at: 2021-08-20T10:40:47.188Z - Hash: 030dfe6c9ea7ff5d80c2 - Time: 12256ms\n\n** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **\n\n\n✔ Compiled successfully.\n```\n\n**11.** Ready! Access the URL `http://localhost:4200/` and check if the application is working. See the application working on [GitHub Pages](https://rodrigokamada.github.io/angular-leaflet/) and [Stackblitz](https://stackblitz.com/edit/angular15-leaflet).\n\n![Angular Leaflet](https://res.cloudinary.com/rodrigokamada/image/upload/v1637580179/Blog/angular-leaflet/angular-leaflet.png)\n\n\n\n## Cloning the application\n\n**1.** Clone the repository.\n\n```powershell\ngit clone git@github.com:rodrigokamada/angular-leaflet.git\n```\n\n**2.** Install the dependencies.\n\n```powershell\nnpm ci\n```\n\n**3.** Run the application.\n\n```powershell\nnpm start\n```\n","funding_links":["https://github.com/sponsors/rodrigokamada"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodrigokamada%2Fangular-leaflet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frodrigokamada%2Fangular-leaflet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodrigokamada%2Fangular-leaflet/lists"}