Java Python调用天地地理编码api返回经纬度

import requests

import json

r = requests.get(url='http://api.tianditu.gov.cn/geocoder?ds='
                     '{"keyWord":"重庆市"}&tk=这里是自己申请的tk')

print(r.json())

print(r.json()["location"]["lon"])
package com.isoftstone.web;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;

/**
 * @ClassName: EntCoordSyncJob
 * @Description: TODO(这里用一句话描述这个类的作用)
 *
 */
public class TianApi {


    public static void main(String[] args) throws IOException {

        String place = "北京市";

        String tk = "天地图官网申请的tk";

        String queryStr = "http://api.tianditu.gov.cn/geocoder?ds={'keyWord':'"+place+"'}&tk="+tk;

//        URL编码{,}
        queryStr = queryStr.replace("}","%7D");
        queryStr = queryStr.replace("{","%7B");
//        获取http客户端
        CloseableHttpClient client = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet(queryStr);

//        模拟浏览器
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");

        CloseableHttpResponse response = client.execute(httpGet);

        HttpEntity entity = response.getEntity();

        if(entity != null){
            String result = EntityUtils.toString(entity, "UTF-8");

            System.out.println(result);

            JSONObject jsonObject = JSON.parseObject(result);
//            JSONObject jsonObject1 = JSONObject.parseObject(jsonObject.get("location").toString());
            String location = jsonObject.get("location").toString();
            JSONObject jsonObjectLocation = JSONObject.parseObject(location);
//            获取经度
            String longtitude = jsonObjectLocation.get("lat").toString();
//            获取纬度
            String latitude = jsonObjectLocation.get("lon").toString();

            System.out.println(place+"的经度为:"+longtitude+"\r\n"+place+"的纬度为:"+latitude);

        }
        response.close();
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值