谷歌APi逆地址解析经纬度输出地址,并判断是否在另一个国家

pom.xml:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20210307</version>
</dependency>

package com.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONObject;


public class demo {

    public static void main(String[] args) {
        //因测试需要代理
        System.setProperty("https.proxyHost", "Your_Host");
        System.setProperty("https.proxyPort", "Your_Port");
		
        double latitude = lat; // 纬度
        double longitude = long; // 经度
        String apiKey = " Google Map appKey";

        try {
            String address = reverseGeocode(latitude, longitude, apiKey);
            System.out.println("逆地址解析的地址输出: " + address);

            // 检查是否在中国
            boolean isChina = isLocationInMalaysia(address);
            System.out.println("Location: " + (isChina ? true : false));
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }

    private static String reverseGeocode(double latitude, double longitude, String apiKey) throws IOException {
        String apiUrl = "https://maps.googleapis.com/maps/api/geocode/json"; //终端地址
        String urlString = apiUrl + "?latlng=" + latitude + "," + longitude + "&key=" + apiKey;

        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        try {
            // 设置请求方法为GET
            connection.setRequestMethod("GET");

            // 获取响应代码
            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();

                // 解析JSON响应
                JSONObject jsonResponse = new JSONObject(response.toString());
                JSONArray results = jsonResponse.getJSONArray("results");

                if (results.length() > 0) {
                    JSONObject firstResult = results.getJSONObject(0);
                    return firstResult.getString("formatted_address");
                } else {
                    return "No results found";
                }
            } else {
                throw new IOException("HTTP error code: " + responseCode);
            }
        } finally {
            // 关闭连接
            connection.disconnect();
        }
    }

    private static boolean isLocationInMalaysia(String address) {
        // 通过地址信息,判断国家是否为中国
        return address.toLowerCase().contains("china");
    }
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以通过Google API获取给定经纬度地址信息。要实现这一功能,可以使用Java的网络请求库和JSON解析库。 首先,需要向Google Maps Geocoding API发送GET请求,该API可以通过经纬度返回相应的地址信息。在请求中,需要设置查询参数包括`latlng`和`key`。`latlng`用于指定要查询的经纬度,`key`则是你的Google API密钥。通过Google Developer Console(开发者控制台)可以获取到这个API密钥。 可以使用Java中的`URL`和`URLConnection`类来发送HTTP请求。首先,构建一个包含查询参数的URL对象,然后通过`openConnection`方法创建一个`URLConnection`对象,并设置GET请求的请求方法。接下来,通过调用`getInputStream`方法来获取API的响应数据流。 然后,可以使用Java的JSON解析库(如`org.json`)来解析API的响应字符串。将API响应的JSON字符串传递给JSON解析库的相应方法,可以得到一个`JSONObject`对象。该对象包含了各种地址信息,如街道、城市、州、国家等。 最后,可以从`JSONObject`中提取出需要的地址信息,并进行后续处理,比如将地址信息打印出来或者存储到数据库中。 需要注意,访问Google API可能需要网络连接和API密钥,并且可能需要遵守相关的使用条款和限制。 这是一个简单的Java代码示例,演示了如何通过Google API获取给定经纬度地址信息: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import org.json.JSONObject; public class GoogleMapsAPI { public static void main(String[] args) { double latitude = 37.4224764; // 维度 double longitude = -122.0842499; // 经度 String apiKey = "your_api_key"; // 替换成你的Google API密钥 String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&key=" + apiKey; try { // 发送GET请求 URLConnection connection = new URL(url).openConnection(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String response = reader.readLine(); // 解析JSON JSONObject jsonObject = new JSONObject(response); String address = jsonObject.getJSONArray("results").getJSONObject(0).getString("formatted_address"); System.out.println("地址:" + address); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上是一个基本的示例,根据自己的需求可以对代码进行扩展和优化。请注意替换示例代码中的API密钥为你自己的密钥。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值