尝试使用 GWeather 和 gtk 访问城市名称时出错

Error while trying to access city name using GWeather and gtk

我想访问城市名称并将其添加到 Gtk.Label。这就是我尝试做的方式。

谁能告诉我我的代码有什么问题?或者建议其他获取城市名称的方法?

public class Epoch.LabelsGrid : Gtk.Grid {
    public Gtk.Label face1_label;
    public Gtk.Label face2_label;
    public Gtk.Label face3_label;
    public Gtk.Label face4_label;
    
    private GWeather.Location location;
    
    construct {
        get_location.begin ();
        
        face1_label = new Gtk.Label ("");
        face1_label.label = location.get_city_name ();
        face1_label.halign = Gtk.Align.CENTER;
        face1_label.hexpand = true;
        face1_label.margin_top = 6;
        face1_label.set_ellipsize (END);
        face1_label.set_max_width_chars (12);
    }
    
    public async void get_location () {
        try {
            var simple = yield new GClue.Simple ("com.github.Suzie97.epoch", GClue.AccuracyLevel.CITY, null);

            simple.notify["location"].connect (() => {
                on_location_updated (simple.location.latitude, simple.location.longitude);
            });

            on_location_updated (simple.location.latitude, simple.location.longitude);
        } catch (Error e) {
            warning ("Failed to connect to GeoClue2 service: %s", e.message);
            return;
        }
    }
    
    public void on_location_updated (double latitude, double longitude) {
        location = GWeather.Location.get_world ();
        location = location.find_nearest_city (latitude, longitude);
    }
}

这是编译时出现的错误:

[1/2] Compiling C object 'com.github.Suzie97.epoch@exe/meson-generated_src_Widgets_Labels.c.o'.
FAILED: com.github.Suzie97.epoch@exe/meson-generated_src_Widgets_Labels.c.o 
cc -Icom.github.Suzie97.epoch@exe -I. -I.. -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/libgeoclue-2.0 -I/usr/include/libgweather-3.0 -I/usr/include/geocode-glib-1.0 -I/usr/include/libsoup-2.4 -I/usr/include/libxml2 -I/usr/include/granite -I/usr/include/gee-0.8 -fdiagnostics-color=always -pipe -D_FILE_OFFSET_BITS=64 -w -g '-DGETTEXT_PACKAGE="com.github.Suzie97.epoch"' -pthread  -MD -MQ 'com.github.Suzie97.epoch@exe/meson-generated_src_Widgets_Labels.c.o' -MF 'com.github.Suzie97.epoch@exe/meson-generated_src_Widgets_Labels.c.o.d' -o 'com.github.Suzie97.epoch@exe/meson-generated_src_Widgets_Labels.c.o' -c 'com.github.Suzie97.epoch@exe/src/Widgets/Labels.c'
In file included from com.github.Suzie97.epoch@exe/src/Widgets/Labels.c:28:0:
/usr/include/libgweather-3.0/libgweather/gweather.h:25:2: error: #error "libgweather should only be used if you understand that it's subject to change, and is not supported as a fixed API/ABI or as part of the platform"
 #error "libgweather should only be used if you understand that it's subject to change, and is not supported as a fixed API/ABI or as part of the platform"
  ^~~~~
ninja: build stopped: subcommand failed.

您需要将 -DGWEATHER_I_KNOW_THIS_IS_UNSTABLE 添加到您的 C 编译参数中。如果您使用介子,GNOME Clocks has a good example.

确保您了解图书馆作者为什么要您这样做。库 API/ABI 将来可能仍会更改,如果发生这种情况,您可能必须相应地更新代码。 (例如,有一个打开的合并请求要删除所有 GTK 小部件,以便库的其余部分可以 link 使用 GTK4 应用程序)。