您的位置:首页 > 编程语言 > Java开发

java实现根据经纬度获取具体地址

2018-02-12 11:08 423 查看
--java类

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;
/**

 * @author 

 * @date 2018年2月12日 上午9:25:18

 * @explain 根据经纬度获取地址

 */

public class MyGetLocation {

 public static void main(String[] args){

 /**

  * 获取位置

  * @param log 大: 纬度

  * @param lat 小:经度

  * @return

  */

 public static String getAdd(String log,String lat) {

  /**

   * 阿里云根据经纬度获取地区名接口:

   * http://gc.ditu.aliyun.com/regeocoding?l=39.938133,116.395739&type=001
   * 阿里云根据地区名获取经纬度接口:

   * http://gc.ditu.aliyun.com/geocoding?a=苏州市
   */
  /** type(100代表道路,010代表POI,001代表门址,111前面都是)   */

  String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+log+"&type=010";

  String res = "";

  try {

   URL url = new URL(urlString);

   HttpURLConnection conn = (HttpURLConnection) url.openConnection();

   conn.setDoInput(true);

   conn.setRequestMethod("POST");

   BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));

   String line;

   while((line = in.readLine())!= null) {

    res += line+"\n";

   }

   in.close();

  

  } catch (Exception e) {

   // TODO: handle exception

   System.out.println("error in wapaction,and e is "+e.getMessage());

  }

  System.out.println(res);

  return res;

 }

 public static JSONObject getAddrObj() {

  String add = getAdd("121.4015340316","31.1721005574");

  JSONObject jsonObject = JSONObject.fromObject(add);

  JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));

  JSONObject addrObj = JSONObject.fromObject(jsonArray.get(0));

  return addrObj;

 }

}

--pom.xml
--管理依赖  cmd运行 mvn compile 编译项目
<!-- Json -->

  <dependency>

   <groupId>org.json</groupId>

   <artifactId>json</artifactId>

  </dependency>

  

  <!--json-lib-->

  <dependency>

   <groupId>net.sf.json-lib</groupId>

   <artifactId>json-lib</artifactId>

   <version>2.4</version>

   <classifier>jdk15</classifier>

  </dependency>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java