根据地理位置获取天气信息

1.使用百度地图接口,根据ip获取城市名

	public static JSONObject getCityInfo(String ip) {
		//获取百度的应用ak
		String ak = properties.getProperty("baidu_ak");
		//百度获取位置的api
		String url = properties.getProperty("location_url");
		
		if(StringUtils.isBlank(ak) || StringUtils.isBlank(url)){
			return null;
		}
		
		Map<String,String> map = new HashMap<>();
		map.put("ip", ip);
		map.put("ak", ak);
		map.put("coor", "BD09LL");
		
		String aString  = 	HttpUtils.doPost(url, map, "UTF8");
		
		JSONObject json = JSONObject.parseObject(aString);
		//获取位置信息
		String content = json.getString("content");
		
		JSONObject contentJson = JSON.parseObject(content);
		
		return contentJson;
	}


	@Override
	public RequestResult getWeaterInfo(RequestParamas paramas) {
		
		RequestResult result = new RequestResult();
		
		String ip = paramas.getParamate("ip");//获取客户端的ip地址
//				
		
		//根据ip获取城市名称
		String city= WeatherUtils.getCity(ip);
		//根据城市获取天气信息
		String info = WeatherUtils.getWeatherData(city);
		//获得天气信息的对象
		WeatherInfo weatherinfo = WeatherUtils.getWeather(info);
		
		result.addJSONData(weatherinfo);
		result.addState(true);
		result.addStateMsg("获取天气信息成功");
		
		return result;
	}



    @Override
	public RequestResult getCityInfoByIp(RequestParamas paramas) {
		
		RequestResult result = new RequestResult();
		
		Session session = this.getPDReadingSession();
		
		String ip = paramas.getParamate("ip");
		
		JSONObject contentJson = WeatherUtils.getCityInfo(ip);
		//获取位置的详细信息
		String address_detail = contentJson.getString("address_detail");
		
		JSONObject addressJson = JSON.parseObject(address_detail);
		
		String province = addressJson.getString("province");
		
		String city = addressJson.getString("city");
		
		String cityCode = addressJson.getString("city_code");
		
		String address = contentJson.getString("address");
		
		SystemConfig systemConfig = (SystemConfig)                 
                                             session.createCriteria(SystemConfig.class)
											.add(Restrictions.eq("configType",                                            
                                              WELCOMEPAGE))
											.add(Restrictions.eq("configKey", province))
											.uniqueResult();
		//欢迎页
		String welcomePage = systemConfig.getConfigValue();
		
		ProvinceBean bean = new ProvinceBean();
		bean.setAdress(address);
		bean.setCity(city);
		bean.setCityCode(cityCode);
		bean.setProvince(province);
		bean.setWelcomePage(welcomePage);
		
		result.addJSONData(bean);
		result.addState(true);
		result.addStateMsg("获取成功");
		
		return result;
	}


	/**
	 * 根据城市获取天气情况
	 * 
	 * @param cityname
	 * @return
	 */
	public static String getWeatherData(String cityname) {

		StringBuilder sb=new StringBuilder();;
		try {
		String weather_url = "http://wthrcdn.etouch.cn/weather_mini?city="+cityname;

		URL url = new URL(weather_url);
		URLConnection conn = url.openConnection();
		InputStream is = conn.getInputStream();
		GZIPInputStream gzin = new GZIPInputStream(is);
		InputStreamReader isr = new InputStreamReader(gzin, "utf-8"); // 设置读取流的编码格式,自定义编码
		BufferedReader reader = new BufferedReader(isr);
		String line = null;
		while((line=reader.readLine())!=null)
		sb.append(line+" ");
		reader.close();
		} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		} catch (MalformedURLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}
		//System.out.println(sb.toString());
		return sb.toString();
	}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值