在Grafana中使用worldmap绘制热力图时发现需要相应的经纬度信息,但是我们日常可能只有地名的信息,像在FineBI中绘制热力图的话只需要对应的城市名以及对应的值就可以绘制出城市热力图。那么如果想要在Grafana中实现输入的数据源为地名并将其展示就需要转换为经纬度。
通过查询资料可以使用Google Geocoding API实现将地名转换为经纬度,但是这样看起来过于麻烦,而且还需要申请key。但是查询到了相应的方法这里仅供参考。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class GeocodingExample {
public static void main(String[] args) {
String apiKey = "YOUR_API_KEY";
String address = "地名"; // 要转换为经纬度的地名
try {
String encodedAddress = URLEncoder.encode(address, "UTF-8");
String apiUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=" + encodedAddress + "&key=" + apiKey;
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// 解析API响应
// 这里使用的是简单的字符串操作,实际上你可以使用JSON库来解析响应
String jsonResponse = response.toString();
double latitude = parseLatitudeFromResponse(jsonResponse);
double longitude = parseLongitudeFromResponse(jsonResponse);
System.out.println("经度:" + longitude);
System.out.println("纬度:" + latitude);
} catch (IOException e) {
e.printStackTrace();
}
}
private static double parseLatitudeFromResponse(String jsonResponse) {
// 解析经纬度
// 这里需要根据你使用的JSON库和API响应的结构进行相应的解析操作
return 0.0; // 替换为实际的解析逻辑
}
private static double parseLongitudeFromResponse(String jsonResponse) {
// 解析经纬度
// 这里需要根据你使用的JSON库和API响应的结构进行相应的解析操作
return 0.0; // 替换为实际的解析逻辑
}
}
好,如果使用api转换经纬度显然过于麻烦,那么换一种思路将经纬度信息直接存入到数据库中,然后对比数据源中的地名以获取经纬度。这里使用的数据库是ClickHouse。
创建表
CREATE TABLE geo_dict (
word String,
longitude Float64,
latitude Float64
) ENGINE = MergeTree()
ORDER BY word;
插入经纬度数据
INSERT INTO geo_dict (word, longitude, latitude)
VALUES
('山东', 117.000923, 36.675807),
('河北', 115.48333, 38.03333),
('吉林', 125.35000, 43.88333),
('黑龙江', 127.63333, 47.75000),
('辽宁', 123.38333, 41.80000),
('内蒙古', 111.670801, 41.818311),
('新疆', 87.68333, 43.76667),
('甘肃', 103.73333, 36.03333),
('宁夏', 106.26667, 37.46667),
('山西', 112.53333, 37.86667),
('陕西', 108.95000, 34.26667),
('河南', 113.65000, 34.76667),
('安徽', 117.283042, 31.86119),
('江苏', 119.78333, 32.05000),
('浙江', 120.20000, 30.26667),
('福建', 118.30000, 26.08333),
('广东', 113.23333, 23.16667),
('江西', 115.90000, 28.68333),
('海南', 110.35000, 20.01667),
('广西', 108.320004, 22.82402),
('贵州', 106.71667, 26.56667),
('湖南', 113.00000, 28.21667),
('湖北', 114.298572, 30.584355),
('四川', 104.06667, 30.66667),
('云南', 102.73333, 25.05000),
('西藏', 91.00000, 30.60000),
('青海', 96.75000, 36.56667),
('天津', 117.20000, 39.13333),
('上海', 121.55333, 31.20000),
('重庆', 106.45000, 29.56667),
('北京', 116.41667, 39.91667),
('台湾', 121.30, 25.03),
('香港', 114.10000, 22.20000),
('澳门', 113.50000, 22.20000);
在Grafana中输入对应的sql语句
SELECT
geo_dict.longitude,
geo_dict.latitude,
Max(weibo_location.count)
FROM weibo_location
LEFT JOIN geo_dict ON weibo_location.word = geo_dict.word
GROUP BY geo_dict.longitude, geo_dict.latitude
注:如遇地图展示不全,可能因为Grafana中使用的是谷歌地图因此需要代理,其实在Grafana中实现绘制热力图的方式还可以使用echarts插件。
如有问题或建议欢迎留言