Moving the marker does not update the address

When the dragend event is triggered, the Getaddress function will be called in the following code:

    Getaddress(LastLat, LastLng , marker,source){
        this.http.get('https://maps.googleapis.com/maps/api/geocode/json?latlng='+LastLat+ ','+LastLng )
                .map(res => res.json())
                .subscribe(data => {
              console.log(this.data); //json output
                this.data = data.results[0].formatted_address;
              console.log(this.data); //Right address
                this.Origin=" ";
                this.Origin=this.data;
              }
});

In the HTML:

<ion-item> <ion-label >Origin</ion-label> <ion-input type="text" value={{Origin}}></ion-input> </ion-item>

When the marker's dragend event occurs, the correct address will be displayed in the console log. However, the input item only displays the initial location of the marker. Clicking on the input item will then update it to the new address or location indicated by the marker. Changing the input item to a label did not solve this issue.

Update 1 dragend event :

lastLatLng(marker,source){
      google.maps.event.addListener(marker, 'dragend', () =>{ 
      this.LastLat= marker.position.lat();
      this.LastLng= marker.position.lng();
      this.Getaddress(this.LastLat,this.LastLng, marker,source);    
    });
    }

Answer №1

To assign the value to the variable, you must use bind. Your current code is setting the value attribute.

Here is a suggested approach:

<ion-item> <ion-label >Origin</ion-label> <ion-input type="text" [value]="Origin"></ion-input> </ion-item>

Refer to the Angular template syntax property binding for more information.

UPDATE:

Implement a callback function as shown below:

setOriginAddress(data:any){
   this.data = data.results[0].formatted_address;
              console.log(this.data); //Correct address
                this.Origin=" ";
                this.Origin=this.data;
}

//Also create a similar callback for dragevent without directly setting this.Origin.

lastLatLng(marker,source){
      google.maps.event.addListener(marker, 'dragend', () =>{ 
      this.LastLat= marker.position.lat();
      this.LastLng= marker.position.lng();
      this.Getaddress(this.LastLat,this.LastLng, marker,source,this.setOriginAddress.bind(this));    
    });
    }

GetAddress

Getaddress(LastLat, LastLng , marker,source,callbackFn){
        this.http.get('https://maps.googleapis.com/maps/api/geocode/json?latlng='+LastLat+ ','+LastLng )
                .map(res => res.json())
                .subscribe(callbackFn);

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

Utilizing history in React with Typescript: A step-by-step guide

I am currently working on a function that navigates to My Page upon clicking a button. However, I encountered an error when trying to implement it in Typescript instead of JavaScript. I am seeking assistance to resolve this issue. //Topbar.tsx function Top ...

What could be causing the lack of re-rendering in children components using redux-form?

When the parent component sends data, the children components do not re-render automatically. Re-rendering only occurs when a key is pressed on an input element. SMART User values from the state are sent by the smart component. If we add console.log(this. ...

The issue with Angular routing lies in the component content not displaying as expected

To showcase my project, I've created a demo on StackBlitz. I successfully implemented routing in my Angular application. You can find the code on StackBlitz. The sample consists of two components: AppComponent LoginComponent By default, when the ...

Subclasses do not have the ability to invoke methods and properties

My intention is to utilize different low-level implementations in various environments while abstracting the specific details from higher-level modules. The expectation is to have a standard way of using the class on the outer layer. The buildEnv variabl ...

What steps can I take to ensure TypeScript compiler approves of variance in calling generic handlers, such as those used in expressJS middleware?

disclaimer: I am a bit uncertain about variance in general... Here is the scenario I am facing: // index.ts import express from 'express'; import {Request, Response} from 'express'; const app = express(); app.use(handler); interface ...

Incorporating the id attribute into the FormControl element or its parent in Angular 7

I'm attempting to assign an id attribute to the first invalid form control upon form submission using Angular reactive forms. Here is my current component code: onSubmit() { if (this.form.invalid) { this.scrollToError(); } else { ...

IDE flags an error with TypeScript type declarations

Here is how my type definition looks: export type AuthType = boolean | { roles: string[]; assistant?: string[] } | (() => void); Now, I need to check the type of the auth variable and assign a value or execute a function in this line of code: req.all ...

ElevationScroll expects the 'children' prop to be a single child component of type 'ReactElement<any, string>'

I am currently working on integrating the Elevate AppBar from Material UI into my application using the following code: interface Props { children: React.ReactElement; } export const ElevationScroll = ({children}: Props) => { const trigger = u ...

A method to simultaneously retrieve all emitted values from multiple EventEmitters in Angular 7

I am facing a scenario where I have a parent component that consists of multiple child components. Each child component may differ from the others, creating a diverse structure within the parent component. Here's an example: ...

Is there a method to refresh the Monaco editor or clear existing models within it?

Currently, I am utilizing ngx-monaco-editor to create a code editor within a modal window. In order to support multiple tabs, I am looking to create a map for the models to keep track of them along with their URIs. Furthermore, it is important that the mod ...

Could you please share the standard naming convention used for interfaces and classes in TypeScript?

Here's what I have: interface IUser { email: string password: string } class User { email: string password: string constructor(email: string, password: string) { this.email = email this.password = password } isEmailValid(): boo ...

How is it that void can be assigned undefined?

According to the documentation on typescript, it states that "the one exception being that undefined is also assignable to void". Source Strict null checking mode specifies that null and undefined values are not within the domain of every type and can o ...

Taking ASP.NET Core 2.0 with Angular to CloudFoundry

Currently, I am facing an issue while working on an app in CloudFoundry (CF). Whenever I push my code into CF, I encounter an error indicating that NodeJs is not installed. [APP/PROC/WEB/0] ERR [1] Ensure that Node.js is installed and can be found in one ...

Can Node.js version 11 support node-sass in Angular projects?

I'm encountering a compilation error with my code. Could this be due to the node version? Does node-sass work with all versions of node? If not, how can I resolve this issue? Compilation error: Unable to locate module 'node-sass' ...

Passing a custom data type from a parent component to a child component in React

I'm currently working on developing a unique abstract table component that utilizes the MatTable component. This abstract table will serve as a child element, and my goal is to pass a custom interface (which functions like a type) from the parent to t ...

The component 'ProtectRoute' cannot be utilized within JSX

While using typescript with nextjs, I encountered an issue as illustrated in the image. When I try to use a component as a JSX element, typescript displays the message: ProtectRoute' cannot be used as a JSX component. import { PropsWithChildren } from ...

Undoing alterations to the user interface in Angular 2 after discontinuing bi-directional data binding

UPDATE: Including code for further clarification. client.component.ts import { Component } from "@angular/core"; import { ClientService } from "../services/client.service"; import { Client } from "../types/client"; @Component({ selector: "rpcs-client", ...

Tips for Verifying a User's Identity using Username and Password

After examining this Angular 2 solution: state: string = ''; error: any; constructor(public af: AngularFire, private router: Router) { this.af.auth.subscribe(auth => { if (auth) { this.router.navigateByUrl('/mem ...

Achieving VS Code/typescript autocomplete functionality without the need to import the library

When a module (for example, moment.js, knockout, or big.js) is added with a <script> tag like this: <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"> </script> that defines a global property (for instance ...

The const keyword is not automatically inferred as a const when using conditional types for generic type parameters

Within Typescript, the const modifier can be applied to function type parameters. This ensures that the inferred type matches the literal received with as const. function identity<const T>(a: T){ return a } For example, when using identity({ a: 4 ...