Troubleshooting problems with integrating Google Maps into Angular 4

I am currently attempting to integrate a Google Map into an Angular project. I am injecting the lat-lng information into the iframe URL, but unfortunately, the map is not being displayed and no errors are showing up in the console.

Below is the template source code:

<iframe [src]="sanitizeUrl('https://www.google.com/maps/embed/v1/place?q='+address?.latitude+','+address?.longitude+'&key=my_key')" width="393" height="165" frameborder="0"></iframe>

And here is the Angular component code:

import { Component } from '@angular/core';
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';

@Component({
  selector: 'app-leftblock',
  templateUrl: './leftblock.component.html',
  styleUrls: ['./leftblock.component.scss']
})
export class AppLeftblockComponent {

  address: any;

  constructor(
    private sanitizer: DomSanitizer

    this.address = {
        'latitude': '19.0759837'
        'latitude': '72.87765590000004'
    };

  ) { }

  sanitizeUrl(url) {
    console.log(url);
    this.sanitizer.bypassSecurityTrustUrl(url);
  }

}

Answer №1

Initial attempt with the following steps.

Include this.address in the constructor as well.

constructor(
    private sanitizer: DomSanitizer
  ) { 
      this.address = [
        'lat' => '40.7128'
        'lng' => '74.0060'
    ];
}


cleanseUrl() {
    const url = 'https://www.google.com/maps/embed/v1/place?q='+this.address.lat+','+this.address.lng+'&key=my_key';
    console.log(url);
    this.sanitizer.bypassSecurityTrustUrl(url);
}

Answer №2

 <iframe [src]="sanitizeUrl('https://www.google.com/maps/embed/v1/place?q='+address.latitude+','+address.longitude+'&key=my_key')" width="393" height="165" frameborder="0"></iframe>

When setting up your frame, ensure that you are passing in the correct values for both longitude and latitude. In this case, it seems you have mistakenly listed 'latitude' twice in the address array.

To resolve this issue, adjust your address object to include both latitude and longitude as follows:

  address = {'latitude': '19.0759837', 'longitude': '72.87765590000004'};

Additionally,

If preferred, you can simplify the process by utilizing the Angular package ngui-map instead of using an iframe. This package offers easy integration and functionality within your project.

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

Switch out button with AngularJS post request

One of the features in my project is a "favorite/unfavorite" button that switches its function when clicked by the user. <button id="favoriteButton" class="btn btn-default btn-sm" type="button" ng-click="@Model.GetFavoriteClick()"> <span aria ...

Animate numbers in response to scroll with JQuery

I am currently working on a project where I need to create a counter that triggers at a specific scroll value. Essentially, I want users to see animated numbers incrementing from 0 to a pre-defined number once they reach a certain point while scrolling. H ...

Is it better to convert fields extracted from a JSON string to Date objects or moment.js instances when using Angular and moment.js together?

When working on editing a user profile, the API call returns the following data structure: export class User { username: string; email: string; creationTime: Date; birthDate: Date; } For validating and manipulating the birthDate val ...

How to best handle dispatching two async thunk actions in Redux Toolkit when using TypeScript?

A recent challenge arose when attempting to utilize two different versions of an API. The approach involved checking for a 404 error with version v2, and if found, falling back to version v1. The plan was to create separate async thunk actions for each ver ...

Retrieving information from deeply nested JSON structures within React components

I am developing a web application that focuses on searching and displaying movie information. One of my challenges is accessing nested objects like "principals" from the same endpoint that contains the main object "title". Upon fetching the JSON response: ...

Integrating Json data from a service in Angular without the use of a data model: A step-by

I received a Nested JSON from my service through a GET call { "id": "7979d0c78074638bbdf739ffdf285c7e1c74a691", "seatbid": [{ "bid": [{ "id": "C1X1486125445685", "impid": "12345", "price": 0, ...

Retain the user's input in the text box even after the form has been submitted

Currently, I am tackling the challenge of creating a register form with an error handler to manage any mistakes made by users during registration. Once the form is submitted, potential errors are displayed to the user. To enhance user experience, I am ex ...

React components do not re-render when the context value changes

The issue with React not re-rendering when the context value changes persists I am using tailwindcss, React(v18.2.0), and vite(3.2.4). I have already attempted i want that clicking on TodoItem should change the completed value in the todo using React con ...

Dynamic assignment of NgModel in AngularJSIs it possible to dynamically

In my controller, there is an array named fields with a specific structure. Here is an example of the data within the array: [ { "name": "Demarcación", "type": "multiple", "scope": "restricted", "icon": "location-arrow ...

When I hover over the content, the image displayed in the tooltip is causing a flickering effect

Dealing with Angular in this situation, my goal is to have an image or video displayed inside a tooltip when hovering over specific content. However, there seems to be a flickering effect before the image renders, making the transition less smooth compared ...

Obtain the UTC timestamp from the current local time on the same day

Imagine knowing the current time (hh:mm:ss) without the date, but you're certain it's today: 12:34:56 Additionally, you know that the time is in the CST timezone (UTC +0800). Your goal is to obtain a timestamp based on this time. If we consi ...

Issue with AngularJs in locating links based on rel attribute is unresolved

When I attempt the following: $document.find('link') The output shows: JQLite(9) [link, link, link, link, link, link, link, link, link] However, if I try: $document.find('link[rel="canonical"]') or $document.find('link[rel= ...

Access JSON file from local PC within a Nativescript application

I am struggling to display the contents of my JSON file in Nativescript using console commands. My goal is to showcase these contents and leverage the values in the file to perform additional functions. Here is the Javascript function that I have tweaked ...

The event.currentTarget's attempt to toggle the class of the child element is not functioning as

I am having trouble toggling a class of a child element of the target element. I can't seem to get it to work and I'm unsure of what steps to take next. HTML: <div class="post-footer-icons-container-el" data-btntype="comment&qu ...

I am experiencing an issue where the position of the value in the returned response array keeps changing after an Ajax request is made. How can

I am currently using a script that sends an array of numbers through Ajax to a web server. These numbers are then used to query a database, and the corresponding values from the table are sent back. I then display these values in respective divs within my ...

Creating a dashed circle in Three.js

I attempted to create a dashed circle following the pattern for dashed line, but unfortunately, the result was not as expected. Here is the code snippet I used: var dashMaterial = new THREE.LineDashedMaterial( { color: 0xee6666, dashSize: 0.5, gapSize: 0. ...

Revamping a function

In my React project, I have split a function into two different components. This function highlights a value if it matches the one being searched for. const generateHighlightedText = useMemo(() => { const regex = new RegExp(`(${highlightValue} ...

Encountering a pair of errors while working with Node.js and Express

(Apologies for the vague title) I have been developing a project using Firebase and Express, but I am encountering some issues. src/index.js import { initializeApp } from "firebase/app"; import { doc, getFirestore } from "firebase/firesto ...

How can you extract and save all links on a webpage based on their class name?

Hello everyone, this is my first question on Stack Overflow. I am trying to extract all links on a page by their class name, store them in an array, and export the results as a JSON format file named results.json. My background is in PHP, C++, and I am c ...

Typescript - Stripping multiple characters from the start and end of a string/Retrieving attributes of a JSON list element

My challenge involves a string like the following : "{"element":"634634"}" My goal is to eliminate {"element":" which remains constant, as well as the final character "}. The only variable component is 634634. How can I achieve this? Alternatively, can ...