-1

Should I convert this google geocode method to an observable?

If so, how would I do it?

FYI - I don't think I need to reject the promise, just return a null, so I made that little change below.

const geoCode = new Geocode();
geoCode.geocode('some address').then(place => {
  console.log(place.geometry.location);
})
.catch(err => {
  console.log(err);
});


export class Geocode {

  geocode(address: string): Promise<any> {
    const geocoder = new google.maps.Geocoder();
    return new Promise((resolve, reject) => {
      geocoder.geocode({
          address: address
        },
        (results, status) => {
          if (status === google.maps.GeocoderStatus.OK) {
            resolve(results[0]);
          } else {
            // reject(new Error(status)); // don't need to reject
            resolve(null);
          }
        }
      );
    });
  }
}
chuckd
  • 13,460
  • 29
  • 152
  • 331
  • 1
    Does this answer your question? [Convert Promise to Observable](https://stackoverflow.com/questions/39319279/convert-promise-to-observable) – Mauricio Gracia Gutierrez Sep 15 '21 at 23:10
  • there's so many answers in your link, it's a little confusing/overwhelming. Not sure if any of the answers from the link help though as the code from the promise isn't shown, while my example has all the code from the promise. – chuckd Sep 15 '21 at 23:15
  • well..read them, try the ones that seem to match your case and then let us know what specific problem you have – Mauricio Gracia Gutierrez Sep 15 '21 at 23:17
  • Maybe a better question would be, how would I wait for google goecoder in Angular 9/10? Promise or Observable? – chuckd Sep 15 '21 at 23:17
  • like this one you mean ? https://stackoverflow.com/questions/56186253/how-to-wait-for-google-geocode-to-return-a-response – Mauricio Gracia Gutierrez Sep 15 '21 at 23:18
  • That is the link I got my code from, but it doesn't really talk about Observables. It only has a Promise as the solution. I'm not that familiar with Promises, only observables as I am new to Angular. I see that Promises are one time and Observables are like "streams". So not sure if either would be better for Google Geocode. – chuckd Sep 15 '21 at 23:22
  • you don't provide us all the details, the things you have already search and expect a precise answer ? how about this other one ? https://stackoverflow.com/questions/27759593/how-do-i-wait-for-a-promise-to-finish-before-returning-the-variable-of-a-functio – Mauricio Gracia Gutierrez Sep 15 '21 at 23:25
  • Well, maybe I don't need anything! Maybe using a promise here is sufficient. I just thought it would be nice to use an Observable. I guess it doesn't really matter. – chuckd Sep 15 '21 at 23:31
  • So you have not tested your code ? – Mauricio Gracia Gutierrez Sep 15 '21 at 23:37
  • No, I have, the code I posted does work. I guess I'm just exploring different options to see if there is something better. Just trying to understand Angular/Observables/Promises better. – chuckd Sep 15 '21 at 23:42
  • I think I have decided on the best option. I'm going to put the Google Geocode call into a service and then subscribe to it. In my app. I have a input field where the client types in an address, presses enter or selects from a dropdown from Google Places autocomplete. If Google autocomplete doesn't fine anything, then I go and look for that address with Google Geocode. Autocomplete is a directive, so I think maybe Geocode should be a service, as I'm going to use it on multiple pages. At first I thought about putting Geocode in a ts file, but that's probably not the best solution. A service is! – chuckd Sep 15 '21 at 23:51

1 Answers1

1

Try this:

  geocode(address: string): Observable<any> {
    const geocoder = new google.maps.Geocoder();
    return new Observable(subscriber => {
      geocoder.geocode({
          address: address
        },
        (results, status) => {
          if (status === google.maps.GeocoderStatus.OK) {
            subscriber.next(results[0]);
          } else {
            subscriber.next(null);
          }
          subscriber.complete();
        }
      );
    });
  }
Yogi
  • 345
  • 1
  • 7