java实现高德与百度之间的坐标转换

时间:2023-01-12 07:39:18
 1 package order.utils;
 2 
 3 import java.math.BigDecimal;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 public class MapUtils {
 8     /*GCJ-02(火星坐标) 和 BD-09 (百度坐标)
 9      *    算法代码如下,其中 bd_encrypt 将 GCJ-02 坐标转换成 BD-09 坐标, bd_decrypt 反之。
10      */
11     static double x_pi = 3.14159265358979324 * 3000.0 / 180.0; 
12     public static void main(String[] args) {
13 //        --高德 119.254356  26.080030  
14 //        --百度 119.260989  26.086051
15         bdEncrypt(26.061317,119.276413);
16 //        119.2608552145561
17 //        26.086114589896383 
18     }
19         //高德转百度
20     public static Map<String,Float> bdEncrypt(double gg_lat, double gg_lon){
21         Map<String,Float> data = new HashMap<String,Float>();
22         double x = gg_lon, y = gg_lat;
23         double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
24         double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
25         double bd_lon = z * Math.cos(theta) + 0.0065;
26         double bd_lat = z * Math.sin(theta) + 0.006;
27         System.out.println(bd_lon+","+bd_lat);
28         System.out.println(new BigDecimal(String.valueOf(bd_lon)).floatValue()+","+new BigDecimal(String.valueOf(bd_lat)).floatValue());
29         data.put("lon", new BigDecimal(String.valueOf(bd_lon)).floatValue());
30         data.put("lat",new BigDecimal(String.valueOf(bd_lat)).floatValue());
31         return data;
32     }
33      
34     public static Map<String,Double> bd_decrypt(double bd_lat, double bd_lon){
35         double x = bd_lon - 0.0065, y = bd_lat - 0.006;
36         double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
37         double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
38         double gg_lon = z * Math.cos(theta);
39         double gg_lat = z * Math.sin(theta);
40         Map<String,Double> data = new HashMap<String,Double>();
41         data.put("lon", gg_lon);
42         data.put("lat", gg_lat);
43         return data;
44     }
45 }