欢迎光临,了解微信小程序开发,就上易用通!

微信小程序开发—使用map地图API获取城市信息实例教程

发布:2018-01-25 11:51浏览: 来源:网络 作者:tianshu

微信小程序开发—使用map地图API获取城市信息实例教程

 


  微信小程序--获取城市信息


由于微信小程序没有方法可以获得当前用户所在城市的信息,所以需要调用方法来获取城市信息,用了两个方法去发送请求并返回城市信息 

  1.

  1. @Controller
  2.  
  3.  
  4.   public class WechatLocationManager {
  5.  
  6.   private Logger logger = LoggerFactory.getLogger(WechatLocationManager.class);
  7.  
  8.   @RequestMapping(value = "/wechat/getcity", method = RequestMethod.POST)
  9.  
  10.   @ResponseBody
  11.  
  12.   public String getCity( @RequestBody Map<String, String> location) {
  13.  
  14.   String local = location.get("location");
  15.  
  16.   System.out.println(local);
  17.  
  18.   String latitude = local.substring(0, local.indexOf(','));
  19.  
  20.   String longitude = local.substring(local.indexOf(',') + 1);
  21.  
  22.   logger.debug("纬度:{}", latitude);
  23.  
  24.   logger.debug("经度:{}", longitude);
  25.  
  26.   String url = "https://api.map.baidu.com/geocoder/v2/?ak=2IBKO6GVxbYZvaR2mf0GWgZE&output=json&pois=0" +
  27.  
  28.   "&location=" + latitude + "," + longitude;
  29.  
  30.   HttpURLConnection connection = null;
  31.  
  32.   BufferedReader reader = null;
  33.  
  34.   try {
  35.  
  36.   URL getUrl = new URL(url);
  37.  
  38.   connection = (HttpURLConnection) getUrl.openConnection();
  39.  
  40.   connection.setConnectTimeout(5000);
  41.  
  42.   connection.setReadTimeout(5000);
  43.  
  44.   connection.connect();
  45.  
  46.   reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
  47.  
  48.   StringBuilder builder = new StringBuilder();
  49.  
  50.   String line;
  51.  
  52.   while ((line = reader.readLine()) != null) {
  53.  
  54.   builder.append(line);
  55.  
  56.   }
  57.  
  58.   if (logger.isDebugEnabled())
  59.  
  60.   logger.debug(builder.toString());
  61.  
  62.   return JSONObject.fromObject(builder.toString());
  63.  
  64.   } catch (Exception e) {
  65.  
  66.   e.printStackTrace();
  67.  
  68.   logger.error(e.getMessage());
  69.  
  70.   } finally {
  71.  
  72.   try {
  73.  
  74.   reader.close();
  75.  
  76.   } catch (IOException e) {
  77.  
  78.   e.printStackTrace();
  79.  
  80.   } finally {
  81.  
  82.   connection.disconnect();
  83.  
  84.   }
  85.  
  86.   }
  87.  
  88.   return obj;
  89.  
  90.   }
  91.  
  92.   }
  93.  
  94.   

2.第二个方法是根据项目中提供的发送请求的方法去发送并接受返回的信息

  1. private static final String LOCATION_URL = "https://api.map.baidu.com/geocoder/v2/";
  2.  
  3.  
  4.   private static final Map<String, String> LOCATION_INPUT = new ConcurrentHashMap<String,String>(){
  5.  
  6.   {
  7.  
  8.   put("ak", "2IBKO6GVxbYZvaR2mf0GWgZE");
  9.  
  10.   put("output", "json");
  11.  
  12.   put("pois","0");
  13.  
  14.   }
  15.  
  16.   };
  17.  
  18.   @RequestMapping(value = "/wechat/city", method = RequestMethod.POST)
  19.  
  20.   @ResponseBody
  21.  
  22.   public String getCity(@RequestBody Map<String, String> location) {
  23.  
  24.   String local = location.get("location");
  25.  
  26.   System.out.println(local);
  27.  
  28.   String latitude = local.substring(0, local.indexOf(','));
  29.  
  30.   String longitude = local.substring(local.indexOf(',') + 1);
  31.  
  32.   logger.debug("纬度:{}", latitude);
  33.  
  34.   logger.debug("经度:{}", longitude);
  35.  
  36.   LOCATION_INPUT.put("location", local);
  37.  
  38.   String obj = HttpClientUtils.doGetWithHeader(null, LOCATION_URL, LOCATION_INPUT, null, "utf-8", null);
  39.  
  40.   return obj;
  41.  
  42.   }

这里记录一下doGet方法去处理请求的

  1. public static String doGet(CloseableHttpClient client, String url, Map<String, String> params, String charset, String userAgent, Map<String, String> heads) {
  2.  
  3.  
  4.   if (StringUtils.isBlank(url)) {
  5.  
  6.   return null;
  7.  
  8.   }
  9.  
  10.   CloseableHttpResponse response = null;
  11.  
  12.   try {
  13.  
  14.   if (params != null && !params.isEmpty()) {
  15.  
  16.   List<NameValuePair> pairs = new ArrayList<>(params.size());
  17.  
  18.   for (Map.Entry<String, String> entry : params.entrySet()) {
  19.  
  20.   String value = entry.getValue();
  21.  
  22.   if (value != null) {
  23.  
  24.   pairs.add(new BasicNameValuePair(entry.getKey(), value));
  25.  
  26.   }
  27.  
  28.   }
  29.  
  30.   url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
  31.  
  32.   }
  33.  
  34.   HttpGet httpGet = new HttpGet(url);
  35.  
  36.   if (StringUtils.isNotBlank(userAgent)) {
  37.  
  38.   httpGet.addHeader(HTTP.USER_AGENT, userAgent);
  39.  
  40.   }
  41.  
  42.   if (heads != null && !heads.isEmpty()) {
  43.  
  44.   for (Map.Entry<String, String> entry : heads.entrySet()) {
  45.  
  46.   String value = entry.getValue();
  47.  
  48.   if (value != null) {
  49.  
  50.   httpGet.addHeader(entry.getKey(),value);
  51.  
  52.   }
  53.  
  54.   }
  55.  
  56.   }
  57.  
  58.   response = client.execute(httpGet);
  59.  
  60.   int statusCode = response.getStatusLine().getStatusCode();
  61.  
  62.   if (statusCode != 200) {
  63.  
  64.   httpGet.abort();
  65.  
  66.   throw new RuntimeException("HttpClient,error status code :" + statusCode);
  67.  
  68.   }
  69.  
  70.   HttpEntity entity = response.getEntity();
  71.  
  72.   String result = null;
  73.  
  74.   if (entity != null) {
  75.  
  76.   result = EntityUtils.toString(entity, charset);
  77.  
  78.   }
  79.  
  80.   EntityUtils.consume(entity);
  81.  
  82.   response.close();
  83.  
  84.   return result;
  85.  
  86.   } catch (Exception e) {
  87.  
  88.   throw new RuntimeException(e);
  89.  
  90.   } finally {
  91.  
  92.   if (null != response) {
  93.  
  94.   try {
  95.  
  96.   response.close();
  97.  
  98.   } catch (Exception ex) {
  99.  
  100.   logger.error("close response has error.");
  101.  
  102.   }
  103.  
  104.   }
  105.  
  106.   }
  107.  
  108.   }





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。