Displaying a pop-up window on top of a Google Map in Angular 5

Currently, I am engrossed in a small project that involves the utilization of Google Maps JS API (latest version). The front end is constructed on Angular 5 with Typescript. My goal is to display a modal window over the map as soon as the user clicks anywhere on it. Although I have successfully triggered the opening of the modal, I am encountering issues with animations not working properly, DatePicker failing to render, and the inability to close the modal by clicking on the button. Interestingly, the modal seems to partially work after resizing the browser window; for instance, if I click on the 'close' button and then resize the browser window, the modal closes correctly. It's worth noting that I am using an angular-material modal. Surprisingly, after the map loads, it alters the top menu styles which are completely unrelated.

Efforts to programmatically call the resize event for the map were futile.

The code for map.component.ts:

Including necessary imports...

@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
    Including constructor and other variables...

    initMap() {
        Setting up google maps and adding a click listener...
    }

    private addMarker(location) {
        Adding markers and showing the meeting modal...
    }

    private showMeetingModal() {
        Displaying the modal...
    }

    ngOnInit() {
        Initializing the map...
    }
}

The code for map.component.html:

<div #mapDiv id="map"></div>

I believe the code for the modal is not relevant considering it works flawlessly in other parts of the application. Thank you for your attention!

UPDATE: Here is a link to a stackblitz example!
https://stackblitz.com/edit/angular-ulwfo2

Answer №1

After some investigation, I was able to uncover a solution. The issue arises because the events triggered by Google Map occur outside of Angular's realm, leading to unexpected behavior.
To resolve this, I simply had to encapsulate the modal opening within the NgZone.run method.
Therefore, when subscribing to a click event on the map, my code now appears as follows:

this.map.addListener('click', (event) => {
    this.zone.run(() => {
      this.addMarker(event.latLng);
    });
  });

Additionally, I needed to inject NgZone into the constructor of the map's component.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting a property of an object to match the value of its sibling in TypeScript

Is there a way to dynamically type an object field based on the value of its sibling? Playground link In this scenario, I have a layout type that resolves into a cell type. Cells can have a layout which should be based on the type of the field. export in ...

Issue when retrieving child elements in Next.js server-side component

"use client"; -- Imports and interfaces const SubscriptionDataFetcher: React.FC<SubscriptionDataFetcherProps> = ({ children }) => { const [data, setData] = useState<SubscriptionData>({}); -- Functions return <> ...

Guide on incorporating text input areas into specific positions within a string

Looking for a way to replace specific words in a string with input fields to enter actual values? For example... Dear Mr. [Father_name], your son/daughter [name] did not attend class today. This is what I want it to look like... Dear Mr. Shankar, your ...

API rest data retrieval service in Angular

I'm currently working on my first Angular App and I need to fetch data from another local API Rest. My project is inspired by the Angular tutorial tour-of-heroes. I have created a component service to make API calls and display the results in another ...

The export enumeration in Typescript-Angular is not defined

I've encountered a strange issue in my Angular project. I have some components and enums set up, and everything was working fine with one component using the enums. But when I tried to implement the same enums in other components, they are returning " ...

Is it considered a bad practice to reference node_modules directly in the src path?

I'm attempting to access a woff file from a npm package called 'extras', but it seems like the URL prefixes the string with './'. When I remove the url() and just try the string/parameter, it still doesn't work. Should I navi ...

Updating shared variables is a challenge when passing functions as props to multiple functional components

In my work on implementing a navbar feature in the next.js framework, I have developed two functions for managing the mobile interface of the website. These functions handle the opening and closing of the navbar, utilizing a hamburger menu for opening and ...

What is the best method for exporting and importing types in React and Next.js apps?

Is there a way to export and import types from API responses in TypeScript? I have a type called Post that I want to re-use in my project. // pages/users.tsx type Post = { id: number; name: string; username: string; email: string; address: { ...

ngc encountering issues when compiling project with untyped third-party libraries

Utilizing a third-party library without defined types, the project is being developed using Angular CLI (version 1.0.0-beta.29) and the library is declared in typings.d.ts. In this instance: declare module ‘xml2js-es6-promise’; The project compiles an ...

The absence of a declaration file for a module results in it implicitly having an 'any' type, particularly when trying to import a portion of a CommonJS module

Presently, the project utilizes a CommonJS module to store configuration values in a single file: module.exports = { classA: `classA`, classB: `classB` classC: `classC` } This makes it easy to reuse these values for creating JS selectors by followi ...

Leverage es6 classes along with mongoose in typescript to utilize loadClass functionality

I've been struggling with a problem and despite my exhaustive search on Google, I still haven't found a solution. My issue revolves around incorporating es6 classes with mongoose using the schema.loadClass(class) method. Unfortunately, when worki ...

``Error Message: TypeORM - could not establish database connection

I encountered an issue while running my project built with Typescript, Typeorm, and Express. The error message received when running the dev script was: connectionNotFoundError: Connection "default" was not found The content of my ormconfig.json ...

Hey there world! I seem to be stuck at the Loading screen while trying to use Angular

A discrepancy in the browsers log indicates node_modules/angular2/platform/browser.d.ts(78,90): error TS2314: Generic type 'Promise' is missing 2 type arguments. ...

Dealing with Promise Wrapper Already Declared in TypeScript Return TypeMismatch

Here is a snippet of TypeScript code I am working with: type Chain = 'eth' export type ApiMethods = { getToken: { path: 'tokens/' (args: { chain: Chain tokenAddress: EthereumAddress }): string } getRank: ...

Executing a function in the constructor of an Angular4 component

I am currently facing an issue where I am attempting to invoke a modal function within the constructor in Angular 4. However, it seems that the function is not being called properly as it gets highlighted. Upon loading the page, no errors are logged and th ...

Issue with accessing custom method in subclass in Typescript

Recently delving into TypeScript, I decided to subclass Error and add a new method called getCode() in my MyError class. export class MyError extends Error { public code: number; constructor(code: number, message?: string) { super(mes ...

The class function in the exported typescript logs that "this" is not defined

I am currently facing an issue with my TypeScript class where I am setting class values in a constructor and referencing them using "this" in a class method. While the .ts file compiles without any warnings, when I import the compiled .js file into another ...

Using TypeScript, define an object and assign variables based on specific conditions

I'm trying to define an object and then set its values based on specific conditions. However, I'm encountering the error message "Variable 'answer' is used before being assigned" with this code snippet. Can you help me understand why? l ...

What is the process for transforming a JSON object into a TypeScript interface?

After receiving a JSON output from an api: { "id": 13, "name": "horst", "cars": [{ "brand": "VW", "maxSpeed": 120, "isWastingGazoline": true ...

Getting meta values in Angular2 is a common task that can be achieved through

Is there a way to retrieve the meta value from the HTML head in Angular2? For instance, let's say I have the following HTML structure of my project and I want to extract the meta tag with the name "userId". <!doctype html> <html> <he ...