Java根据百度API获得经纬度,然后根据经纬度在获得城市信息(未测试)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
 
/** 
* 获取经纬度
* 
* @author jueyue 返回格式:Map<String,Object> map map.put("status", 
* reader.nextString());//状态 map.put("result", list);//查询结果 
* list<map<String,String>> 
* 密钥:f247cdb592eb43ebac6ccd27f796e2d2 
*/ 
public class GetLatAndLngByBaidu { 
     
    /** 
    * @param addr 
    * 查询的地址 
    * @return 
    * @throws IOException 
    */ 
    public Object[] getCoordinate(String addr) throws IOException { 
        String lng = null;//经度
        String lat = null;//纬度
        String address = null; 
        try { 
            address = java.net.URLEncoder.encode(addr, "UTF-8"); 
        }catch (UnsupportedEncodingException e1) { 
            e1.printStackTrace(); 
        } 
        String key = "f247cdb592eb43ebac6ccd27f796e2d2"; 
        String url = String .format("http://api.map.baidu.com/geocoder?address=%s&output=json&key=%s", address, key); 
        URL myURL = null; 
        URLConnection httpsConn = null; 
        try { 
            myURL = new URL(url); 
        } catch (MalformedURLException e) { 
            e.printStackTrace(); 
        } 
        InputStreamReader insr = null;
        BufferedReader br = null;
        try { 
            httpsConn = (URLConnection) myURL.openConnection();// 不使用代理 
            if (httpsConn != null) { 
                insr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8"); 
                br = new BufferedReader(insr); 
                String data = null; 
                int count = 1;
                while((data= br.readLine())!=null){ 
                    if(count==5){
                        lng = (String)data.subSequence(data.indexOf(":")+1, data.indexOf(","));//经度
                        count++;
                    }else if(count==6){
                        lat = data.substring(data.indexOf(":")+1);//纬度
                        count++;
                    }else{
                        count++;
                    }
                } 
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } finally {
            if(insr!=null){
                insr.close();
            }
            if(br!=null){
                br.close();
            }
        }
        return new Object[]{lng,lat}; 
    } 
 
 
    public static void main(String[] args) throws IOException {
        GetLatAndLngByBaidu getLatAndLngByBaidu = new GetLatAndLngByBaidu();
        Object[] o = getLatAndLngByBaidu.getCoordinate("成都市天府四街");
        System.out.println(o[0]);//经度
        System.out.println(o[1]);//纬度
    }
 
}

输出结果:104.04701

                 30.548397

 

调用以下路径根据经纬度获取地址,返回数据为JSON格式,

http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location=30.548397,104.04701&output=json&pois=1

返回结果:

{"status":0,"result":{"location":{"lng":104.04701001273,"lat":30.548396923974},"formatted_address":"四川省成都市武侯区天府四街","business":"","addressComponent":{"city":"成都市","district":"武侯区","province":"四川省","street":"天府四街","street_number":""},"pois":[{"addr":"四川省成都市武侯区天府四街","cp":"NavInfo","distance":"144","name":"保利·心语花园","poiType":"地产小区","point":{"x":104.04823889466,"y":30.54874673449},"tel":"","uid":"881965b85a8d8a977369d348","zip":""},{"addr":"四川省成都市武侯区天府四街","cp":"NavInfo","distance":"629","name":"逸都国际","poiType":"地产小区","point":{"x":104.05247889667,"y":30.547152788724},"tel":"","uid":"e49f869c4f156be9c6effb6b","zip":""},{"addr":"四川省成都市武侯区剑南大道","cp":"NavInfo","distance":"881","name":"世豪广场","poiType":"地产小区","point":{"x":104.05252381195,"y":30.553318490823},"tel":"","uid":"a2a7b0b167a0e1997adbd206","zip":""},{"addr":"四川省成都市武侯区德赛三街","cp":"NavInfo","distance":"989","name":"顾客停车场","poiType":"交通设施","point":{"x":104.04942465794,"y":30.555798655385},"tel":"","uid":"d135a6e15571911483a00cfe","zip":""}],"cityCode":75}}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要根据经纬度获取城市信息,你需要使用逆地理编码(reverse geocoding)服务。以下是使用 Java 实现的示例代码,基于百度地图的逆地理编码 API: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class GeoCodingService { private static final String BAIDU_MAP_API = "http://api.map.baidu.com/geocoder/v2/?ak=YOUR_AK&location=%s,%s&output=json&pois=0"; public static void main(String[] args) throws IOException { String lat = "39.983424"; String lng = "116.322987"; String address = getAddressByGeoCoding(lat, lng); System.out.println("Address: " + address); } public static String getAddressByGeoCoding(String lat, String lng) throws IOException { String urlStr = String.format(BAIDU_MAP_API, lat, lng); URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); conn.connect(); int responseCode = conn.getResponseCode(); if (responseCode == 200) { InputStream inputStream = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder responseBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { responseBuilder.append(line); } reader.close(); inputStream.close(); String response = responseBuilder.toString(); // 解析 JSON 响应,获取地址信息 // 这里需要使用 JSON 解析库,例如 Jackson、Gson 等 // 这里以 Jackson 为例 ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readTree(response); JsonNode resultNode = rootNode.get("result"); if (resultNode != null) { JsonNode addressComponentNode = resultNode.get("addressComponent"); if (addressComponentNode != null) { String province = addressComponentNode.get("province").asText(); String city = addressComponentNode.get("city").asText(); String district = addressComponentNode.get("district").asText(); String street = addressComponentNode.get("street").asText(); return province + city + district + street; } } } return null; } } ``` 在示例代码中,我们使用了百度地图的逆地理编码服务,需要在 `BAIDU_MAP_API` 常量中填写你的百度地图 API 密钥。`getAddressByGeoCoding` 方法接收经纬度参数 `lat` 和 `lng`,发送 HTTP GET 请求到逆地理编码 API,获取响应后解析 JSON 响应,提取地址信息返回。注意,示例代码仅供参考,你需要根据具体情况进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值