zoukankan      html  css  js  c++  java
  • 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("北京")); } }

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

  • 相关阅读:
    8 盒子模型
    7 display属性
    如何提高运维价值体系
    Python学习之OS模块初识
    7、MongoDB学习之游标
    Python 学习之文件对象的属性和方法简介
    Python集合set()操作详解
    Python中的字典介绍
    Python序列之元组
    Python序列之列表
  • 原文地址:https://www.cnblogs.com/news1997/p/10906414.html
Copyright © 2011-2022 走看看