|
| 1 | +# Angular Google Maps component |
| 2 | + |
| 3 | +This component provides a Google Maps Angular component that implements the |
| 4 | +[Google Maps JavaScript API](https://developers.google.com/maps/documentation/javascript/tutorial). |
| 5 | +File any bugs against the [angular/components repo](https://github.com/angular/components/issues). |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +To install, run `npm install @angular/google-maps`. |
| 10 | + |
| 11 | +## Loading the API |
| 12 | + |
| 13 | +- First follow [these steps](https://developers.google.com/maps/gmp-get-started) to get an API key that can be used to load Google Maps. |
| 14 | +- Load the [Google Maps JavaScript API](https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API). |
| 15 | +- The Google Maps JavaScript API must be loaded before the `GoogleMap` component. |
| 16 | + |
| 17 | +## GoogleMap |
| 18 | + |
| 19 | +The `GoogleMap` component wraps the [`google.maps.Map` class](https://developers.google.com/maps/documentation/javascript/reference/map) from the Google Maps JavaScript API. You can configure the map via the component's inputs. The `options` input accepts a full [`google.maps.MapOptions` object](https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions), and the component additionally offers convenience inputs for setting the `center` and `zoom` of the map without needing an entire `google.maps.MapOptions` object. The `height` and `width` inputs accept a string to set the size of the Google map. [Events](https://developers.google.com/maps/documentation/javascript/reference/map#Map.bounds_changed) can be bound using the outputs of the `GoogleMap` component, although events have the same name as native mouse events (e.g. `mouseenter`) have been prefixed with "map" as to not collide with the native mouse events. Other members on the `google.maps.Map` object are available on the `GoogleMap` component and can be accessed using the [`ViewChild` decorator](https://angular.io/api/core/ViewChild). |
| 20 | + |
| 21 | +See the [example](#example) below or the [source](./google-map/google-map.ts) to read the API. |
| 22 | + |
| 23 | +## MapMarker |
| 24 | + |
| 25 | +The `MapMarker` component wraps the [`google.maps.Marker` class](https://developers.google.com/maps/documentation/javascript/reference/marker#Marker) from the Google Maps JavaScript API. The `MapMarker` component displays a marker on the map when it is a content child of a `GoogleMap` component. Like `GoogleMap`, this component offers an `options` input as well as convenience inputs for `position`, `title`, `label`, and `clickable`, and supports all `google.maps.Marker` events as outputs. |
| 26 | + |
| 27 | +See the [example](#example) below or the [source](./map-marker/map-marker.ts) to read the API. |
| 28 | + |
| 29 | +## MapInfoWindow |
| 30 | + |
| 31 | +The `MapInfoWindow` component wraps the [`google.maps.InfoWindow` class](https://developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow) from the Google Maps JavaScript API. The `MapInfoWindow` has a `options` input as well as a convenience `position` input. Content for the `MapInfoWindow` is the inner HTML of the component, and will keep the structure and css styling of any content that is put there when it is displayed as an info window on the map. |
| 32 | + |
| 33 | +To display the `MapInfoWindow`, it must be a child of a `GoogleMap` component, and it must have its `open` method called, so a reference to `MapInfoWindow` will need to be loaded using the [`ViewChild` decorator](https://angular.io/api/core/ViewChild). The `open` method accepts an `MapMarker` as an optional input, if you want to anchor the `MapInfoWindow` to a `MapMarker`. |
| 34 | + |
| 35 | +See the [example](#example) below or the [source](./map-info-window/map-info-window.ts) to read the API. |
| 36 | + |
| 37 | +## Example |
| 38 | + |
| 39 | +```typescript |
| 40 | +// example-module.ts |
| 41 | + |
| 42 | +import {CommonModule} from '@angular/common'; |
| 43 | +import {NgModule} from '@angular/core'; |
| 44 | +import {GoogleMapsModule} from '@angular/google-maps'; |
| 45 | + |
| 46 | +import {GoogleMapDemo} from './google-map-demo'; |
| 47 | + |
| 48 | +@NgModule({ |
| 49 | + imports: [ |
| 50 | + CommonModule, |
| 51 | + GoogleMapsModule, |
| 52 | + ], |
| 53 | + declarations: [GoogleMapDemo], |
| 54 | +}) |
| 55 | +export class GoogleMapDemoModule { |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +// example-app.ts |
| 60 | +import {Component, ViewChild} from '@angular/core'; |
| 61 | +import {MapInfoWindow, MapMarker} from '@angular/google-maps'; |
| 62 | + |
| 63 | +/** Demo Component for @angular/google-maps/map */ |
| 64 | +@Component({ |
| 65 | + selector: 'google-map-demo', |
| 66 | + templateUrl: 'google-map-demo.html', |
| 67 | +}) |
| 68 | +export class GoogleMapDemo { |
| 69 | + @ViewChild(MapInfoWindow, {static: false}) infoWindow: MapInfoWindow; |
| 70 | + |
| 71 | + center = {lat: 24, lng: 12}; |
| 72 | + markerOptions = {draggable: false}; |
| 73 | + markerPositions: google.maps.LatLngLiteral[] = []; |
| 74 | + zoom = 4; |
| 75 | + display?: google.maps.LatLngLiteral; |
| 76 | + |
| 77 | + addMarker(event: google.maps.MouseEvent) { |
| 78 | + this.markerPositions.push(event.latLng.toJSON()); |
| 79 | + } |
| 80 | + |
| 81 | + move(event: google.maps.MouseEvent) { |
| 82 | + this.display = event.latLng.toJSON(); |
| 83 | + } |
| 84 | + |
| 85 | + openInfoWindow(marker: MapMarker) { |
| 86 | + this.infoWindow.open(marker); |
| 87 | + } |
| 88 | + |
| 89 | + removeLastMarker() { |
| 90 | + this.markerPositions.pop(); |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +```html |
| 96 | +<!-- index.html --> |
| 97 | +<!doctype html> |
| 98 | +<head> |
| 99 | + ... |
| 100 | + <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"> |
| 101 | + </script> |
| 102 | +</head> |
| 103 | + |
| 104 | +<!-- example-app.html --> |
| 105 | +<google-map height="400px" |
| 106 | + width="750px" |
| 107 | + [center]="center" |
| 108 | + [zoom]="zoom" |
| 109 | + (mapClick)="addMarker($event)" |
| 110 | + (mapMousemove)="move($event)" |
| 111 | + (mapRightclick)="removeLastMarker()"> |
| 112 | + <map-marker #marker |
| 113 | + *ngFor="let markerPosition of markerPositions" |
| 114 | + [position]="markerPosition" |
| 115 | + [options]="markerOptions" |
| 116 | + (mapClick)="openInfoWindow(marker)"></map-marker> |
| 117 | + <map-info-window>Info Window content</map-info-window> |
| 118 | +</google-map> |
| 119 | + |
| 120 | +<div>Latitude: {{display?.lat}}</div> |
| 121 | +<div>Longitude: {{display?.lng}}</div> |
| 122 | +``` |
0 commit comments