0

I have used to get current address using google api and now i want implement callback function in this function using angular 4 how can implement it?

 let currgeocoder = new google.maps.Geocoder();
   currgeocoder.geocode({
      'location': location
   }, function(results:any, status:any) {
      if (status == google.maps.GeocoderStatus.OK) {
        let place = results[0];
       //this.showresult(place.formatted_address);
      } else {
          alert('Geocode was not successful for the following reason: ' + status);
      }
   });

      this.myGroup.setValue({
      searchControl: 'global'
    });
1
  • have look to my answer if you need any thing let me know Commented Dec 15, 2017 at 12:41

1 Answer 1

1

you can create oberservable and push new values on it ,

let subject = new Subject(); 
let ovservable = subject.asObservable()

subject.next("b");

ovservable.subscribe((value) => {
  console.log("Subscription got", value); // Subscription wont get 
                                          // anything at this point
});

so create observable , expose it and when you receive data from call make use of .next() method that will do


in your code

let subject = new Subject(); 
let ovservable = subject.asObservable();
let currgeocoder = new google.maps.Geocoder();
   currgeocoder.geocode({
      'location': location
   }, function(results:any, status:any) {
      if (status == google.maps.GeocoderStatus.OK) {
        let place = results[0];
        subject.next(place);
       //this.showresult(place.formatted_address);
      } else {
          alert('Geocode was not successful for the following reason: ' + status);
      }
   });

      this.myGroup.setValue({
      searchControl: 'global'
    });

    ovservable.subscribe((value) => {
      console.log("Subscription got", value); // Subscription wont get 
                                              // anything at this point
    });
Sign up to request clarification or add additional context in comments.

7 Comments

i want to pass results[0] value in showresult function so how can do that?
what is asObservable()? do i need to import any library?
i am getting this error in cmd: TS2304: Cannot find name 'Subject'.
@dgpoo - please import RXJs library in you angular project
@dgpoo - you cna call you furnction for this ovservable.subscribe((value) => { yourfunction()});
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.