Qt. ubuntu x64, can't get coordinates
eds709394 opened this issue · 8 comments
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
MMDB_s mmdb;
MMDB_lookup_result_s result;
int gai_error;
int mmdb_error;
const char ip_address[] = "116.28.219.199"; // 要查询的 IP 地址
// 初始化 MMDB 结构
// GeoLite2-City.mmdb
// GeoIP2-City.mmdb
//if (MMDB_open("./GeoLite2-City.mmdb", MMDB_MODE_MMAP, &mmdb) == MMDB_SUCCESS)
if (MMDB_open("./GeoIP2-City.mmdb", MMDB_MODE_MMAP, &mmdb) == MMDB_SUCCESS)
{
//MMDB_open("/GeoIP2-City.mmdb", MMDB_MODE_MMAP, &mmdb);
// 查询 IP 地址的经纬度信息
result = MMDB_lookup_string(&mmdb, ip_address, &gai_error, &mmdb_error);
if (gai_error == 0 && mmdb_error == MMDB_SUCCESS)
{
MMDB_entry_data_s entry_data;
if (result.found_entry)
{
if (MMDB_SUCCESS == MMDB_get_value(&result.entry, &entry_data, "location", "latitude", NULL, NULL)) {/* MMDB_get_value 成功 */
if (entry_data.has_data) {/* 找到了想要的数据 */
if (entry_data.type == MMDB_DATA_TYPE_DOUBLE) {
printf("%lf", entry_data.double_value);
printf("\n");
}
else {
printf("data_type = %d\n", entry_data.type);
}
}
else {
fprintf(stderr, "MMDB_get_value not found\n");
}
}
}
char lat[16], lon[16];
// 从查询结果中获取经度和纬度信息
if (MMDB_get_value(&result.entry, &entry_data, "location/latitude", NULL) == MMDB_SUCCESS) {
snprintf(lat, sizeof(lat), "%.6f", entry_data.double_value);
}
if (MMDB_get_value(&result.entry, &entry_data, "location/longitude", NULL) == MMDB_SUCCESS) {
snprintf(lon, sizeof(lon), "%.6f", entry_data.double_value);
}
// 获取经纬度信息
double latitude = 0.0;
double longitude = 0.0;
MMDB_get_value(&result.entry, &entry_data, "location", "latitude");
latitude = entry_data.double_value;
MMDB_get_value(&result.entry, &entry_data, "location", "longitude");
longitude = entry_data.double_value;
// 打印经纬度信息
printf("Latitude: %f\n", latitude);
printf("Longitude: %f\n", longitude);
// 打印经度和纬度信息
printf("IP地址:%s\n", ip_address);
printf("经度:%s\n", lat);
printf("纬度:%s\n", lon);
} else {
printf("无法查询IP地址的经纬度信息\n");
}
// 关闭 MMDB
MMDB_close(&mmdb);
}
}
I am not entirely sure what the question is, but I suspect the issue is that you need to replace:
MMDB_get_value(&result.entry, &entry_data, "location", "latitude");
latitude = entry_data.double_value;
MMDB_get_value(&result.entry, &entry_data, "location", "longitude");
longitude = entry_data.double_value;
with:
MMDB_get_value(&result.entry, &entry_data, "location", "latitude", NULL);
latitude = entry_data.double_value;
MMDB_get_value(&result.entry, &entry_data, "location", "longitude", NULL);
longitude = entry_data.double_value;
Can be compiled. But can't get the right value.
Did you try the fix I proposed above? If so, what output are you getting and what output are you expecting?
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
-124968807495796401245153016236126869777128867833808623288451205173435487563224681818285869834730626066856392915726039229981606467101545437276624551019756826905477527753855192786503238782004751359288074763272142991080518245834880389993883020296192.000000
Latitude: 0.000000
Longitude: 0.000000
IP地址:116.28.219.199
经度:讁���
纬度:��D���
Win7 x64, That's my expecting.
34.773200
Latitude: 34.773200
Longitude: 113.722000
IP鍦板潃锛16.28.219.199
缁忓害锛
绾害锛殺��X
This project in windows is ok, but when I transplant to ubuntu...
I'd suggest reaching out to support@maxmind.com for any additional help. The code seems to work fine for me on Ubuntu with the changes I outlined.