Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web app feature #69

Open
spotvin42 opened this issue Jun 23, 2023 · 0 comments
Open

Web app feature #69

spotvin42 opened this issue Jun 23, 2023 · 0 comments

Comments

@spotvin42
Copy link

spotvin42 commented Jun 23, 2023

Hello, thank you for your work.

It would be great if you could include an option in this plugin for web apps.
I know cordova is for native apps, but there could be an easy implementation with the Google API or even NOMINATIM open API:

Here is how I implemented it, it would be better to have all in one...

  async saveCurrentCity(lat: number, lng: number, uid: string) {
    let loadedCity: string;

    if (Capacitor.getPlatform() !== "web") {
      loadedCity = await this.getCityForNativeApps(lat, lng);
    }
    if (Capacitor.getPlatform() === "web") {
      loadedCity = await this.getCityForWeb(lat, lng);
    }

    await this.updateUserLocation(uid, { updated_at: new Date(), city: loadedCity });

    return loadedCity;
  }

  async getCityForNativeApps(lat: number, lng: number) {
    const options: NativeGeocoderOptions = {
      useLocale: true,
      maxResults: 5,
    };

    try {
      const result: NativeGeocoderResult[] = await this.nativeGeocoder.reverseGeocode(lat, lng, options);
      if (result[0] && result[0].subLocality) {
        return JSON.stringify(result[0].subLocality);
      } else {
        console.log("No subLocality found");
      }
    } catch (error) {
      console.log("NativeGeocoder ERROR", error);
    }
  }

  async getCityForWeb(lat: number, lng: number) {
    try {
      let response = await fetch(`https://nominatim.openstreetmap.org/reverse?format=json&lat=${lat}&lon=${lng}`);
      let data = await response.json();
      return data.address.suburb;
    } catch (error) {
      console.error("getCityForWeb ERROR", error);
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant