国外区域、天气api获取
This commit is contained in:
@ -310,9 +310,9 @@ public class StationServiceImpl implements StationService {
|
|||||||
* @param station
|
* @param station
|
||||||
*/
|
*/
|
||||||
private void updateStationInfo(Station station) {
|
private void updateStationInfo(Station station) {
|
||||||
MyAddress address = AddressUntils.getAddress(String.valueOf(station.getLatitude()),String.valueOf(station.getLongitude()));
|
MyAddress address = AbroadAddressUtils.getAddress(String.valueOf(station.getLatitude()),String.valueOf(station.getLongitude()));
|
||||||
if (null != address) {
|
if (null != address) {
|
||||||
String adCode = address.getAdcode();
|
String adCode = address.getCityCode();
|
||||||
station.setAdCode(adCode);
|
station.setAdCode(adCode);
|
||||||
station.setDistrict(address.getDistrict());
|
station.setDistrict(address.getDistrict());
|
||||||
station.setCity(address.getCity());
|
station.setCity(address.getCity());
|
||||||
@ -320,7 +320,7 @@ public class StationServiceImpl implements StationService {
|
|||||||
station.setNation(address.getNation());
|
station.setNation(address.getNation());
|
||||||
String key = RedisKeyConstant.WEATHER_PROVINCE_CITY + adCode;
|
String key = RedisKeyConstant.WEATHER_PROVINCE_CITY + adCode;
|
||||||
if (!redisService.hasKey(key)) {
|
if (!redisService.hasKey(key)) {
|
||||||
WeatherRespVo weatherRespVo = WeatherUntils.getWeatherRespVo(station.getAdCode());
|
WeatherRespVo weatherRespVo = AbroadWeatherUtils.getWeatherRespVo(String.valueOf(station.getLatitude()),String.valueOf(station.getLongitude()));
|
||||||
if (weatherRespVo != null) {
|
if (weatherRespVo != null) {
|
||||||
weatherRespVo.setUpdateTime(System.currentTimeMillis());
|
weatherRespVo.setUpdateTime(System.currentTimeMillis());
|
||||||
redisService.set(key, weatherRespVo);
|
redisService.set(key, weatherRespVo);
|
||||||
|
|||||||
@ -46,6 +46,11 @@
|
|||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
<artifactId>fastjson</artifactId>
|
<artifactId>fastjson</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>20231013</version>
|
||||||
|
</dependency>
|
||||||
<!--jackson-->
|
<!--jackson-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
|||||||
@ -25,6 +25,9 @@ public class WeatherRespVo {
|
|||||||
@ApiModelProperty(value = "天气现象")
|
@ApiModelProperty(value = "天气现象")
|
||||||
String skyCon;
|
String skyCon;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "天气现象编码")
|
||||||
|
int code;
|
||||||
|
|
||||||
@ApiModelProperty(value = "风向风速")
|
@ApiModelProperty(value = "风向风速")
|
||||||
String speedAndDirection;
|
String speedAndDirection;
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,128 @@
|
|||||||
|
package com.ho.common.tools.util;
|
||||||
|
|
||||||
|
import com.ho.common.tools.entity.MyAddress;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author kerwin
|
||||||
|
*
|
||||||
|
* 海外地址反向解析
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class AbroadAddressUtils {
|
||||||
|
|
||||||
|
static String apiKey = "AIzaSyAM4JOtE9yt48f60ATwByB7uWzTIrYSSUg";
|
||||||
|
|
||||||
|
public static MyAddress getAddress(String lat, String lng){
|
||||||
|
// 构建API请求URL
|
||||||
|
String urlString = "https://maps.googleapis.com/maps/api/geocode/json?" +
|
||||||
|
"latlng=" + lat + "," + lng +
|
||||||
|
"&key=" + apiKey;
|
||||||
|
// 发送HTTP请求
|
||||||
|
URL url = null;
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
try {
|
||||||
|
url = new URL(urlString);
|
||||||
|
// 创建代理(如果不需要代理,可以移除这部分)
|
||||||
|
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 4781));
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
|
||||||
|
conn.setRequestMethod("GET");
|
||||||
|
conn.setConnectTimeout(10000); // 10秒连接超时
|
||||||
|
conn.setReadTimeout(10000); // 10秒读取超时
|
||||||
|
// 检查响应状态
|
||||||
|
int responseCode = conn.getResponseCode();
|
||||||
|
if (responseCode != 200) {
|
||||||
|
throw new RuntimeException("HTTP请求失败: " + responseCode);
|
||||||
|
}
|
||||||
|
// 读取响应内容
|
||||||
|
try (BufferedReader reader = new BufferedReader(
|
||||||
|
new InputStreamReader(conn.getInputStream()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (ProtocolException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 解析JSON响应
|
||||||
|
return parseGeocodeResponse(response.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MyAddress parseGeocodeResponse(String jsonResponse) {
|
||||||
|
JSONObject json = new JSONObject(jsonResponse);
|
||||||
|
String status = json.getString("status");
|
||||||
|
|
||||||
|
if (!"OK".equals(status)) {
|
||||||
|
throw new RuntimeException("地理编码API错误: " + status);
|
||||||
|
}
|
||||||
|
MyAddress address = new MyAddress();
|
||||||
|
|
||||||
|
// 获取第一个结果(最精确的匹配)
|
||||||
|
JSONObject firstResult = json.getJSONArray("results").getJSONObject(0);
|
||||||
|
// 解析地址组件
|
||||||
|
JSONArray components = firstResult.getJSONArray("address_components");
|
||||||
|
for (int i = 0; i < components.length(); i++) {
|
||||||
|
JSONObject component = components.getJSONObject(i);
|
||||||
|
JSONArray types = component.getJSONArray("types");
|
||||||
|
String longName = component.getString("long_name");
|
||||||
|
// 国家信息
|
||||||
|
if (types.toString().contains("country")) {
|
||||||
|
address.setNation(longName);
|
||||||
|
}
|
||||||
|
// 省份/州信息
|
||||||
|
if (types.toString().contains("administrative_area_level_1")) {
|
||||||
|
address.setProvince(longName);
|
||||||
|
}
|
||||||
|
// 城市信息
|
||||||
|
if (types.toString().contains("locality")) {
|
||||||
|
address.setCity(longName);
|
||||||
|
}
|
||||||
|
// 区县信息
|
||||||
|
if (types.toString().contains("sublocality")) {
|
||||||
|
address.setDistrict(longName);
|
||||||
|
}
|
||||||
|
// 邮政编码
|
||||||
|
if (types.toString().contains("postal_code")) {
|
||||||
|
address.setCityCode(longName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 示例坐标 - 纽约
|
||||||
|
String latitude = "48.877132";
|
||||||
|
String longitude = "2.356407";
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 获取地址信息
|
||||||
|
MyAddress address = getAddress(latitude, longitude);
|
||||||
|
|
||||||
|
// 打印结果
|
||||||
|
System.out.println("============ 反向地理编码结果 ============");
|
||||||
|
System.out.println("国家: " + address.getNation() );
|
||||||
|
System.out.println("省份/州: " + address.getProvince());
|
||||||
|
System.out.println("城市: " + address.getCity());
|
||||||
|
System.out.println("地区/区县: " + address.getDistrict());
|
||||||
|
System.out.println("邮政编码: " + address.getCityCode());
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("发生错误: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,91 @@
|
|||||||
|
package com.ho.common.tools.util;
|
||||||
|
|
||||||
|
import com.ho.common.tools.entity.WeatherRespVo;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author kerwin
|
||||||
|
*
|
||||||
|
* 获取海外天气
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class AbroadWeatherUtils {
|
||||||
|
|
||||||
|
static String apiKey = "6358b3081a5849f09ac90138252107";
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String latitude = "40.714224";
|
||||||
|
String longitude = "-73.961452";
|
||||||
|
|
||||||
|
try {
|
||||||
|
System.out.println(getWeatherRespVo(latitude, longitude));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static WeatherRespVo getWeatherRespVo(String lat, String lng) {
|
||||||
|
String urlString = "http://api.weatherapi.com/v1/forecast.json?key="+apiKey+"&q=" + lat + "," + lng +"&days=1";
|
||||||
|
URL url = null;
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
try {
|
||||||
|
url = new URL(urlString);
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setRequestMethod("GET");
|
||||||
|
try (BufferedReader reader = new BufferedReader(
|
||||||
|
new InputStreamReader(conn.getInputStream()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parseWeatherData(response.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static WeatherRespVo parseWeatherData(String jsonResponse) {
|
||||||
|
WeatherRespVo weatherRespVo =new WeatherRespVo();
|
||||||
|
JSONObject root = new JSONObject(jsonResponse);
|
||||||
|
|
||||||
|
// 解析地区信息
|
||||||
|
JSONObject location = root.getJSONObject("location");
|
||||||
|
String area = location.getString("name");
|
||||||
|
weatherRespVo.setCityzh(area);
|
||||||
|
|
||||||
|
// 获取天气预报数据
|
||||||
|
JSONObject forecast = root.getJSONObject("forecast");
|
||||||
|
JSONArray forecastday = forecast.getJSONArray("forecastday");
|
||||||
|
JSONObject day = forecastday.getJSONObject(0).getJSONObject("day");
|
||||||
|
|
||||||
|
// 解析温度数据
|
||||||
|
double maxTemp = day.getDouble("maxtemp_c");
|
||||||
|
weatherRespVo.setMaxTemperature(new BigDecimal(String.valueOf(maxTemp)));
|
||||||
|
double minTemp = day.getDouble("mintemp_c");
|
||||||
|
weatherRespVo.setMinTemperature(new BigDecimal(String.valueOf(minTemp)));
|
||||||
|
String condition = day.getJSONObject("condition").getString("text");
|
||||||
|
weatherRespVo.setSkyCon(condition);
|
||||||
|
int code = day.getJSONObject("condition").getInt("code");
|
||||||
|
weatherRespVo.setCode(code);
|
||||||
|
double maxWindKph = day.getDouble("maxwind_kph");
|
||||||
|
// 获取风向(从最近的小时数据中获取)
|
||||||
|
JSONArray hours = forecastday.getJSONObject(0).getJSONArray("hour");
|
||||||
|
String windDirection = hours.getJSONObject(0).getString("wind_dir");
|
||||||
|
// 组合风向风速信息
|
||||||
|
String windInfo = windDirection + " " + maxWindKph + " km/h";
|
||||||
|
weatherRespVo.setSpeedAndDirection(windInfo);
|
||||||
|
weatherRespVo.setUpdateTime(System.currentTimeMillis());
|
||||||
|
return weatherRespVo;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -12,6 +12,7 @@ import com.ho.common.tools.entity.MyAddress;
|
|||||||
import com.ho.common.tools.entity.WeatherRespVo;
|
import com.ho.common.tools.entity.WeatherRespVo;
|
||||||
import com.ho.common.tools.exception.DataResult;
|
import com.ho.common.tools.exception.DataResult;
|
||||||
import com.ho.common.tools.service.RedisService;
|
import com.ho.common.tools.service.RedisService;
|
||||||
|
import com.ho.common.tools.util.AbroadWeatherUtils;
|
||||||
import com.ho.common.tools.util.AddressUntils;
|
import com.ho.common.tools.util.AddressUntils;
|
||||||
import com.ho.common.tools.util.IPUtils;
|
import com.ho.common.tools.util.IPUtils;
|
||||||
import com.ho.common.tools.util.WeatherUntils;
|
import com.ho.common.tools.util.WeatherUntils;
|
||||||
@ -241,7 +242,7 @@ public class WeatherController {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
String key = RedisKeyConstant.WEATHER_PROVINCE_CITY + adCode;
|
String key = RedisKeyConstant.WEATHER_PROVINCE_CITY + adCode;
|
||||||
WeatherRespVo weatherRespVo = WeatherUntils.getWeatherRespVo(station.getAdCode());
|
WeatherRespVo weatherRespVo = AbroadWeatherUtils.getWeatherRespVo(String.valueOf(station.getLatitude()),String.valueOf(station.getLongitude()));
|
||||||
weatherRespVo.setUpdateTime(updateTime);
|
weatherRespVo.setUpdateTime(updateTime);
|
||||||
redisService.set(key, weatherRespVo);
|
redisService.set(key, weatherRespVo);
|
||||||
map.put(adCode, adCode);
|
map.put(adCode, adCode);
|
||||||
|
|||||||
Reference in New Issue
Block a user