高德地图web服务api反坐标查询/逆地理编码

官方API:https://lbs.amap.com/api/webservice/gettingstarted

1、pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.3.RELEASE
		 
	
	com.api
	gaodeapi
	0.0.1-SNAPSHOT
	gaodeapi
	Demo project for Spring Boot

    
        UTF-8
        UTF-8
        1.8
    

	
		
			org.springframework.boot
			spring-boot-starter
		
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.0
        

		
			org.postgresql
			postgresql
			runtime
		
        
            org.postgresql
            postgresql
            42.1.2
        

        
        
            com.alibaba
            druid
            1.1.0
        

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

        
        
            com.squareup.okhttp3
            okhttp
            3.10.0
        
        
        
            com.squareup.okio
            okio
            1.13.0
        
        
        
            com.alibaba
            fastjson
            1.2.47
        
        
            junit
            junit
        
        
            org.springframework
            spring-test
            5.1.4.RELEASE
        
        
            org.springframework.boot
            spring-boot-test
        


    

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



2、代码

package com.tyxx.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tyxx.GaodeapiApplication;
import com.tyxx.model.GdGPS;
import com.tyxx.service.GdGPSService;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.List;

/**
 * Created by cqy on 2019/3/18.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {GaodeapiApplication.class})// 指定启动类
public class Test {

    @Autowired
    private GdGPSService gdGPSService;		//数据查询和更新

    @org.junit.Test
    public void testSelect() {
        List lt = gdGPSService.selAll();
        for (int i = 0; i < lt.size(); i++) {
            GdGPS gdGPS = lt.get(i);
            System.out.println("id:" + gdGPS.getId());
            String str = Test.gdapi(gdGPS.getJingdu(), gdGPS.getWeidu());
            System.out.println("位置:" + str);
            gdGPS.setAddress(str);
            Integer row = gdGPSService.updatePri(gdGPS);
            System.out.println(row + "-------------");
        }
    }

    public static String gdapi(String jingdu, String weidu) {
        String url = "https://restapi.amap.com/v3/geocode/regeo?output=json&location=" +
                jingdu + "," + weidu + "&key=换成自己申请的key&radius=1000&extensions=all";
        OkHttpClient okHttpClient = new OkHttpClient();

        Request request = new Request.Builder().url(url).get().build();

        Call call = okHttpClient.newCall(request);
        String results = "";
        try {
            Response response = call.execute();
            String result = response.body().string();
            // System.out.println(result);
            JSONObject json = JSONArray.parseObject(result);
            // System.out.println(json);
            // System.out.println(json.get("regeocode"));
            JSONObject obj = JSONObject.parseObject(json.get("regeocode")
                    .toString());
            System.out.println(obj.get("formatted_address"));
            results = obj.get("formatted_address").toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return results;
    }

}

你可能感兴趣的:(java)