百度 谷歌 GPS 坐标互转 java版

</pre><pre name="code" class="html">

public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

//百度转谷歌

public static double[] bd2gcj(double[] locs) {
        double[] res = new double[2];
        double x = locs[1] - 0.0065, y = locs[0] - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        res[1] = z * Math.cos(theta);
        res[0] = z * Math.sin(theta);

        return res;
    }

//谷歌转百度
    public static double[] gcj2bd(double[] locs) {
        double[] res = new double[2];
        double x = locs[1], y = locs[0];
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        res[1] = z * Math.cos(theta)+ 0.0065;
        res[0] = z * Math.sin(theta)+ 0.006;
        return res;
    }

public static double pi = 3.14159265358979324;
public static double a = 6378245.0;
public static double ee = 0.00669342162296594323;

//GPS转谷歌
    // World Geodetic System ==> Mars Geodetic System
    public static double[] transform(double wgLat, double wgLon )
    {
        double[] locs = new double [2];
        if (outOfChina(wgLat, wgLon))
        {
            locs[1] = wgLat ;
            locs[0] = wgLon ;
            return locs;
        }
        double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
        double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
        double radLat = wgLat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        locs[1] = wgLat + dLat;
        locs[0] = wgLon + dLon;
        return locs;
    }

    public static boolean outOfChina(double lat, double lon)
    {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }

    public static double transformLat(double x, double y)
    {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    public static double transformLon(double x, double y)
    {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
        return ret;
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java可以使用地理编码服务将地址换为经纬度坐标(GPS坐标)。地理编码服务是一种将地址换为经纬度坐标的服务,通常由地图服务提供商提供。 有许多可用于Java的地理编码服务,例如Google Maps API和Bing Maps API。要使用这些服务,您需要注册并获取API密钥。然后,您可以使用Java代码调用API并传递地址信息,API将返回地址的经纬度坐标。 例如,使用Google Maps API进行地址GPS的示例代码如下所示: ``` import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject; public class AddressToGPS { // Replace YOUR_API_KEY with your Google Maps API key private static final String API_KEY = "YOUR_API_KEY"; public static void main(String[] args) { String address = "1600 Amphitheatre Parkway, Mountain View, CA"; String latLong = getLatLong(address); System.out.println(latLong); } public static String getLatLong(String address) { try { URL url = new URL( "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=" + API_KEY); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output = "", full = ""; while ((output = br.readLine()) != null) { full += output; } JSONObject jsonObject = new JSONObject(full); JSONObject jsonObjectLocation = (JSONObject) jsonObject.get("results")[0].get("geometry").get("location"); ### 回答2: 在Java中,地址GPS坐标可以通过多种方式来实现。以下是一种常见的方法: 1. 使用地理编码服务:Java提供了许多地理编码服务的API,比如Google Maps Geocoding API、百度地图的地理编码API等。可以通过调用这些API来将地址换为GPS坐标。 示例代码如下: ```java import org.json.JSONArray; import org.json.JSONObject; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; public class AddressToGPSConverter { public static void main(String[] args) throws Exception { String address = "北京市朝阳区酒仙桥路10号"; // 待换的地址 String apiKey = "YOUR_API_KEY"; // 替换为实际的API Key String encodedAddress = URLEncoder.encode(address, "UTF-8"); String apiUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=" + encodedAddress + "&key=" + apiKey; URL url = new URL(apiUrl); URLConnection connection = url.openConnection(); 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 jsonObject = new JSONObject(response.toString()); JSONArray results = jsonObject.getJSONArray("results"); JSONObject location = results.getJSONObject(0).getJSONObject("geometry").getJSONObject("location"); double latitude = location.getDouble("lat"); double longitude = location.getDouble("lng"); System.out.println("Latitude: " + latitude); System.out.println("Longitude: " + longitude); } } ``` 上述代码使用了Google Maps Geocoding API来将地址换为GPS坐标。需要替换`YOUR_API_KEY`为实际的API Key,以获得访问权限。然后,可以将待换的地址赋值给`address`变量,并运行代码,即可获得对应的经纬度信息。 另外,还可以使用其他第三方地理编码服务的API,在Java中进行类似的操作。具体使用哪个API取决于需求和个人偏好。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值