java 经纬度解析地址地理位置信息

/***
  * 根据经伟度得到详细的地址信息
  * @param x
  * @param y
  * @throws IOException
  * @throws UnsupportedEncodingException
  */
 public String getaddressforxybyGooglehttpconnection(String x,String y) throws IOException
 {
  if(x.length() >9){x = x.substring(0,7);}
  if(y.length()>8){y = y.substring(0,6);}
  URL url = null;
  String mapurl="http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=" +Float.parseFloat(y)/100000+ "," + Float.parseFloat(x)/100000 ;
  
  String address="无法从地理信息服务器上获得此位置的地理信息";//取地图的地址
  StringBuffer strBuffer = new StringBuffer();
  HttpURLConnection connection = null;    
  DataInputStream in = null;
  try {
   url = new URL(mapurl);          
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET"); 
    connection.setUseCaches(false);   
    connection.setDefaultUseCaches(false);                    
    connection.setDoInput(true);  
    connection.setDoOutput(true);       
      connection.setConnectTimeout(1000);
      connection.setReadTimeout(1000);    
      in = new DataInputStream(connection.getInputStream());               
      int   all=   in.available();  
      int   code   =   connection.getResponseCode();   
      if(code   !=   connection.HTTP_OK)   
            {                           
                return address ;
            }   
            else   
            {   
             byte[]   b=   new   byte[all];   
            in.read(b);     
              String strAddress=   new   String(b,"UTF-8"); //GBK  2011-04-28
               String[] m_sAddress = strAddress.split(",");
                if (m_sAddress.length == 3)
                {
                 address = m_sAddress[2];
                }
                else
                {
                 address = "无法获取地址";
                }
                //address   =   new   String(b,"UTF-8"); //GBK  2011-04-28
   }                                                           
      connection.disconnect();
      in.close();         
     } catch (Exception e)
     {       
      address="无法从地理信息服务器上获得此位置的地理信息";//取地图的地址
     } 
     finally
     { 
      connection.disconnect();
      in.close();
     }
  address = address.replace('"', ' ');
  address = address.replace("<?xml version= 1.0  encoding= GBK ?><R><code>0</code><msg>", "");
  address = address.replace("</msg></R>", "");
  return address;
 }

您可以使用Java中提供的Google Maps API或OpenStreetMap API来免费将地理位置转换为经纬度。这些API允许您通过HTTP请求向服务发送地址或地点名称,并返回与该地点相关的经度和纬度坐标。以下是一个使用Google Maps API的示例代码: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject; public class Geocoding { public static String getLatLng(String address) throws Exception { String url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address.replace(" ", "+"); URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JSONObject json = new JSONObject(response.toString()); JSONObject location = json.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location"); double lat = location.getDouble("lat"); double lng = location.getDouble("lng"); return lat + "," + lng; } public static void main(String[] args) throws Exception { String address = "1600 Amphitheatre Parkway, Mountain View, CA"; String latLng = getLatLng(address); System.out.println(latLng); } } ``` 在上述代码中,我们首先构造了Google Maps API的URL,然后使用Java的HttpURLConnection类发送GET请求并获取响应。接下来,我们将响应字符串解析为JSON格式,并提取出经度和纬度坐标。最后,我们将这些坐标合并为一个字符串并返回。在此示例中,我们将地址设置为1600 Amphitheatre Parkway,Mountain View,CA,并获取其经度和纬度坐标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值