Top

Maps


you have to install google map


 npm i @angular/google-maps
Map at a specified location with Marker
<div class="container-fluid">
     <div class="row">
        <div class="col-xl-6">
            <div class="card">
                <div class="card-header card-no-border pb-0">
                    <h5>Map at a specified location with Marker</h5>
                </div>
                <div class="card-body map-z-index">
                    <google-map width="100%" height="500px" [zoom]="zoom">
                    @for( marker of markers ; track maker){
                        <map-marker  [position]="marker.position" [label]="marker.label"></map-marker>
                    }
                    </google-map>
                </div>
            </div>
        </div>
    </div>
</div>
To use google map following type script files
import { Component, OnInit, ViewChild } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { GoogleMap } from '@angular/google-maps';
    import { GoogleMapsModule } from '@angular/google-maps';
    import { HttpClientModule } from '@angular/common/http';                 

@Component({
  selector: 'app-google-map',
  imports: [CommonModule,GoogleMapsModule,HttpClientModule],
  templateUrl: './google-map.html',
  styleUrls: ['./google-map.scss']
})
export class GoogleMap {

  openInfo: any
  public markers: any[];
  public markers1: any[];
  public zoom: number;

  constructor() {
    this.markers = [];
    this.zoom = 2;
  }

  ngOnInit() {
    const bangalore = { lat: 12.97, lng: 77.59 };
    this.markers1.push({
      position: {
        lat: 12.97,
        lng: 77.59
      },
      label: {
        color: "red",
        text: "Arial"
      },
      Option: {
        draggable: true,
        animation: google.maps.Animation.DROP,
        zoomControl: false,
        mapTypeControl: false,
        streetViewControl: false,
        fullscreenControl: false
      }

    })

    const map = new google.maps.Map(
      document.getElementById("map") as HTMLElement,
      {
        zoom: 12,
        center: bangalore,
      }
    );

    this.markers.push({
      position: {
        lat: 35.717,
        lng: 139.731
      },
      label: {
        color: "black",
        text: "Madrid"
      },
      Option: {
        draggable: true,
        animation: google.maps.Animation.DROP,
      }
    });

    this.markers.push({
      position: {
        lat: 48.8615515,
        lng: 2.3112233
      },
      label: {
        color: "black",
        text: "Paris"
      }
    });
  }

  //Street View
  @ViewChild(GoogleMap) map!: GoogleMap;


  ngAfterViewInit() {
    const streetView = this.map.getStreetView();

    streetView.setOptions({
      position: { lat: 38.9938386, lng: -77.2515373 },
      pov: { heading: 70, pitch: -10 },
    });

    streetView.setVisible(true);
    const bounds = this.getBounds(this.markers);
    // const this.map.googleMap.fitBounds(bounds);
  }

  marker1 = { position: { lat: 38.9987208, lng: -77.2538699 } };
  marker2 = { position: { lat: 39.7, lng: -76.0 } };
  marker3 = { position: { lat: 37.9, lng: -76.8 } };

  markers5 = [this.marker1, this.marker2, this.marker3];


  getBounds(markers: any[]) {
    let north;
    let south;
    let east;
    let west;

    for (const marker of markers) {
      // set the coordinates to marker's lat and lng on the first run.
      // if the coordinates exist, get max or min depends on the coordinates.
      north = north !== undefined ? Math.max(north, marker.position.lat) : marker.position.lat;
      south = south !== undefined ? Math.min(south, marker.position.lat) : marker.position.lat;
      east = east !== undefined ? Math.max(east, marker.position.lng) : marker.position.lng;
      west = west !== undefined ? Math.min(west, marker.position.lng) : marker.position.lng;
    };

    const bounds = { north, south, east, west };

    return bounds;
  }

}

you have to install Leaflet map


 npm i @bluehalo/ngx-leaflet

Simple Map

<div class="row">
    <div class="col-xl-6">
      <div class="card">
        <div class="card-header card-no-border pb-0">
          <h3>Simple Map</h3>
      </div>
    <div class="card-body">
      <div style="height: 500px; z-index: 0;" leaflet
        [leafletOptions]="options1"></div>
      </div>
    </div>
  </div>
</div>
             
To use Leaflet Maps following type script files
import { Component } from '@angular/core';
  import * as L from 'leaflet';
  import { HttpClient } from '@angular/common/http';
  import { LeafletModule } from '@bluehalo/ngx-leaflet';
  import { GeoJsonObject } from 'geojson';
  import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-leaflet-map',
  imports: [LeafletModule],
  templateUrl: './leaflet-map.html',
  styleUrls: ['./leaflet-map.scss']
})
export class LeafletMap {

  constructor(private http: HttpClient) { }

  //First map options
  options1 = {
    layers: [
      L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 18, attribution: '...' })
    ],
    zoom: 5,
    center: L.latLng(46.879966, -121.726909)
  };

  layersControl = {
    baseLayers: {
      'Open Street Map': L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 18, attribution: '...' }),
      'Open Cycle Map': L.tileLayer('http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png', { maxZoom: 18, attribution: '...' })
    },
    overlays: {
      'Big Circle': L.circle([46.95, -122], { radius: 5000 }),
      'Big Square': L.polygon([[46.8, -121.55], [46.9, -121.55], [46.9, -121.7], [46.8, -121.7]])
    }
  }

  //Second map
  options2 = {
    layers: [
      L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 50, attribution: '...' })
    ],
    zoom: 5,
    center: L.latLng(46.879966, -121.726909)
  };

  //Third map
  map: L.Map;
  json: GeoJsonObject | GeoJsonObject[] | null | undefined;
  options3 = {
    layers: [
      L.tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
        maxZoom: 18,
        attribution: ""
      })
    ],
    zoom: 7,
    center: L.latLng(47.482019, -1)
  };
  onMapReady(map: L.Map) {
    this.http.get("assets/data/map/polygon.json").subscribe((json: any) => {
      this.json = json;
      L.geoJSON(this.json).addTo(map);
    });
  }

  //Forth map
  map4: L.Map | L.LayerGroup;
  homeCoords = {
    lat: 23.810331,
    lon: 90.412521
  };

  popupText = "Some popup text";

  markerIcon = {
    icon: L.icon({
      iconSize: [25, 41],
      iconAnchor: [10, 41],
      popupAnchor: [2, -40],
      // specify the path here
      iconUrl: "assets/images/marker-icon.png",
      shadowUrl: "assets/images/marker-shadow.png"
    })
  };

  options4 = {
    layers: [
      L.tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
        maxZoom: 18,
        attribution: ""
      })
    ],
    zoom: 5,
    center: L.latLng(this.homeCoords.lat, this.homeCoords.lon)
  };

  initMarkers() {
    const popupInfo = `${
      this.popupText
      }`;
    L.marker([this.homeCoords.lat, this.homeCoords.lon], this.markerIcon)
      .addTo(this.map4)
      .bindPopup(popupInfo);
  }

  onMapReady4(map: L.Map) {
    this.map4 = map;
    this.initMarkers();
  }

  ngOnInit() { }
}