java实现根据高德地图API接口进行地址位置解析,将地址转化为经纬度

原创文章,转载请注明,欢迎评论和更改。

1,所需额外ar包,import net.sf.json.JSONObject;

2,完整源代码代码

package com.travel.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import net.sf.json.JSONObject;
    
public class AddressLngLatExchange {
    
    public String getLngLat(String address) {
        StringBuffer json = new StringBuffer();
        try {
            URL u = new URL("http://restapi.amap.com/v3/geocode/geo?address="+address+"&output=JSON&key=7f4ffae4074e8b8e4d147190527a4b72");
            URLConnection yc = u.openConnection();
            //读取返回的数据
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8"));
            String inputline = null;
            while((inputline=in.readLine())!=null){
                json.append(inputline);
            }
        in.close();
        } catch (Exception e) {
             e.printStackTrace();
         }
        String jsonStr=json.toString();
        JSONObject jsonObject = JSONObject.fromObject(jsonStr);
        
     //判断输入的位置点是否存在
if(jsonObject.getJSONArray("geocodes").size()>0) return jsonObject.getJSONArray("geocodes").getJSONObject(0).get("location").toString(); else return null; } public static void main(String[] args) { AddressLngLatExchange addressLngLatExchange=new AddressLngLatExchange(); System.out.println(addressLngLatExchange.getLngLat("北京")); } }

原创文章,转载请注明,欢迎评论和更改。

猜你喜欢

转载自www.cnblogs.com/news1997/p/10906414.html
今日推荐