Java calls the Gaode map API to obtain the latitude and longitude according to the detailed address

Step 1: Register a Gaode developer account and create an application

  1. Visit Gaode open platform https://lbs.amap.com/

  2. After logging in, create an application in the console and obtain the generated application key. This key will be used to access the Gaode map API.

     

Step 2: Use Java to send an HTTP request to obtain the latitude and longitude

HttpURLConnectionYou can use tools such as or in Java HttpClientto send HTTP requests to the Gaode map API, and pass parameters to obtain latitude and longitude information. Here is a HttpURLConnectionsample code used:

package com.lps.utils;

import com.alibaba.fastjson.JSONObject;

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


public class AMapAPI {
    private static final String API_URL = "https://restapi.amap.com/v3/geocode/geo";
    private static final String KEY = "你的key 横扫饥饿做回自己";

    public static void main(String[] args) {

        String address = "润州区牌湾派出所";
        String city = "镇江市";
        String url = API_URL + "?key=" + KEY + "&address=" + address + "&city=" + city;
        try {
            URL apiUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                JSONObject json = JSONObject.parseObject(response.toString());
                String status = json.getString("status");
                if (status.equals("1")) {
                    JSONObject geocode = json.getJSONArray("geocodes").getJSONObject(0);
                    String location = geocode.getString("location");
                    System.out.println(address+"经纬度:" + location);
                } else {
                    System.out.println("查询失败:" + json.getString("info"));
                }
            } else {
                System.out.println("HTTP请求失败:" + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Step 3: Parse the JSON data returned by the API

Use JSONObjectthe class to parse the JSON data returned by the API, and extract the latitude and longitude information from it. YOUR_APP_KEYMake sure you replace it with your actual application key in the code .

 

The above code will help you call the Gaode map API to obtain the latitude and longitude information of the detailed address for use in your application. Remember to handle exceptions that may arise, such as network connectivity issues or unparsable JSON data.

Guess you like

Origin blog.csdn.net/lps12345666/article/details/132437401