Skip to content

Web app feature #69

@spotvin42

Description

@spotvin42

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);
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions