Returns the corresponding geographic location based on the latitude and longitude of the map coordinates

 

 1. Before returning to the corresponding geographic location based on the latitude and longitude of the map coordinates, we should understand the map coordinate system:

1. Geodetic coordinate system WGS-84

A regional coordinate system used to express the location of points on the earth. It uses a reference ellipsoid that closely approximates the natural shape of the earth as a datum for describing and inferring the positions and relationships of ground points. A geodetic coordinate system must clearly define the orientation of its three axes and the location of its center. Usually people use the short axis of the spheroid to be parallel to the mean rotation axis of the earth at a certain moment and the corresponding true starting meridian plane to determine the direction of the coordinate axis. If the center of the reference ellipsoid coincides with the mean center of mass of the earth, the geocentric geodetic coordinate system is defined and established. It is the basis for various positioning, measurement and control orbits in aerospace and long-range weapons and space science. If the surface of the ellipsoid matches the local geoid of one or several countries best, then a local geodetic coordinate system of a country or region is established. The position of the midpoint of the geodetic coordinate system is represented by its geodetic coordinate, which is defined by the normal of the ellipsoid. Among them, the intersection angle between the ellipsoid normal passing through a certain point and the ellipsoid equatorial plane is the geodetic latitude; the dihedral angle including the normal and the geodetic meridian plane and the starting geodetic meridian plane is the geodetic longitude of the point; along the normal to The distance of the ellipsoid is the geodetic height of the point. The geodetic latitude, geodetic longitude and geodetic height are represented by capital letters B, L and H respectively.

Domestic Internet companies will not use GPS coordinates because this is not in line with national policy. So everyone will use the GCJ-02 coordinate system.

 

2. Mars coordinate system GCJ-02

The national security plug-in, also called encryption plug-in or biasing or SM module, is actually an artificial biasing process for the real coordinate system. According to the algorithm of a few lines of code, the real coordinates are encrypted into false coordinates, and this biasing The offset is not linear, so the offset will vary from place to place. The encrypted coordinates are also often referred to as the Mars coordinate system.

Map providers currently using the Mars coordinate system:

  • Tencent Soso Map
  • Sohu Sogou map
  • Alibaba Cloud Map
  • AutoNavi MapABC map
  • Lingtu 51ditu map

All electronic maps and all navigation devices need to join the national security plug-in. In the first step, the map company surveys and maps the map. After the survey and mapping is completed, it is sent to the National Bureau of Surveying and Mapping, and the electronic map of the real coordinates is encrypted into "Mars coordinates". Only such a map can be published and released, and then the GPS company can be used. deal with. The second step, all GPS companies, as long as they need car navigation, need to use electronic maps for navigation, they all need to add a national security algorithm to the software to encrypt and convert the real coordinate signals read from the COM port into the state required by the country. Confidential coordinates, in this way, the GPS navigator and the navigation electronic map can be completely matched, and the GPS can work normally.

Domestic Internet companies must use the GCJ-02 coordinate system.

 

 

3. Baidu coordinate system BD-09

Baidu coordinates encrypts the Mars coordinate system once to form the Baidu coordinate system.

Map providers currently using Baidu's coordinate system:

  • Baidu Baidu map

A small number of companies will perform another encryption on the basis of GCJ-02, but such coordinates are not universal and are only suitable for specific company maps.

 

4. Picture it

Figure bar is the result of multiplying Baidu coordinates by 10000.

The map quotient currently using the map bar coordinate system:

  • Map Bar MapBar Map

A small number of companies will perform another encryption on the basis of GCJ-02, but such coordinates are not universal and are only suitable for specific company maps.

2. The map coordinate conversion tool class converts the coordinates according to the actual situation in the project. Because the coordinate systems used by each map are different, the latitude and longitude of the corresponding coordinate system must be used to return the correct geographic location.

Coordinate system conversion tool class:

PositionUtil.java
package com.ydtx.utils.gps;

import java.util.HashMap;
import java.util.Map;

import com.ydtx.moudle.controller.carMonitor.PointDouble;
import com.ydtx.utils.BaiduMapUtil;

/**
 * Comparison and conversion of various map API coordinate systems;
 * WGS84 coordinate system: that is, the earth coordinate system, which is an international coordinate system. The device generally contains a GPS chip or Beidou chip, and the latitude and longitude obtained by the chip is the WGS84 geographic coordinate system.
 * Google Maps uses the WGS84 geographic coordinate system (except China);
 * GCJ02坐标系:即火星坐标系,是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。 
 * 谷歌中国地图和搜搜中国地图采用的是GCJ02地理坐标系; BD09坐标系:即百度坐标系,GCJ02坐标系经加密后的坐标系; 
 * 搜狗坐标系、图吧坐标系等,估计也是在GCJ02基础上加密而成的。 chenhua 
 */  
public class PositionUtil {  
      
    public static final String BAIDU_LBS_TYPE = "bd09ll";  
      
    public static double pi = 3.1415926535897932384626;  
    public static double a = 6378245.0;  
    public static double ee = 0.00669342162296594323;  
  
    /** 
     * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System 
     *  
     * @param lat 
     * @param lon 
     * @return 
     */  
    public static Gps gps84_To_Gcj02(double lat, double lon) {  
        if (outOfChina(lat, lon)) {  
            return null;  
        }  
        double dLat = transformLat(lon - 105.0, lat - 35.0);  
        double dLon = transformLon(lon - 105.0, lat - 35.0);  
        double radLat = lat / 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);  
        double mgLat = lat + dLat;  
        double mgLon = lon + dLon;  
        return new Gps(mgLat, mgLon);  
    }  
  
    /** 
     * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return 
     * */  
    public static Gps gcj_To_Gps84(double lat, double lon) {  
        Gps gps = transform(lat, lon);  
        double lontitude = lon * 2 - gps.getWgLon();  
        double latitude = lat * 2 - gps.getWgLat();  
        return new Gps(latitude, lontitude);  
    }  
  
    /** 
     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标 
     *  
     * @param gg_lat 
     * @param gg_lon 
     */  
    public static Gps gcj02_To_Bd09(double gg_lat, double gg_lon) {  
        double x = gg_lon, y = gg_lat;  
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);  
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);  
        double bd_lon = z * Math.cos(theta) + 0.0065;  
        double bd_lat = z * Math.sin(theta) + 0.006;  
        return new Gps(bd_lat, bd_lon);  
    }  
  
    /** 
     * * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param 
     * bd_lat * @param bd_lon * @return 
     */  
    public static Gps bd09_To_Gcj02(double bd_lat, double bd_lon) {  
        double x = bd_lon - 0.0065, y = bd_lat - 0.006;  
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);  
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);  
        double gg_lon = z * Math.cos(theta);  
        double gg_lat = z * Math.sin(theta);  
        return new Gps(gg_lat, gg_lon);  
    }  
  
    /** 
     * (BD-09)-->84 
     * @param bd_lat 
     * @param bd_lon 
     * @return 
     */  
    public static Gps bd09_To_Gps84(double bd_lat, double bd_lon) {  
  
        Gps gcj02 = PositionUtil.bd09_To_Gcj02(bd_lat, bd_lon);  
        Gps map84 = PositionUtil.gcj_To_Gps84(gcj02.getWgLat(),  
                gcj02.getWgLon());  
        return map84;  
  
    }  
  
    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 Gps transform(double lat, double lon) {  
        if (outOfChina(lat, lon)) {  
            return new Gps(lat, lon);  
        }  
        double dLat = transformLat(lon - 105.0, lat - 35.0);  
        double dLon = transformLon(lon - 105.0, lat - 35.0);  
        double radLat = lat / 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);  
        double mgLat = lat + dLat;  
        double mgLon = lon + dLon;  
        return new Gps(mgLat, mgLon);  
    }  
  
    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;  
    }  
  
    public static Map GPSToBaiduMap(String log, String lat){
      Map map = new HashMap();
      if(log!=null&&log!=""&&lat!=null&&lat!=""){
         Gps gps=PositionUtil.gps84_To_Gcj02(Double.parseDouble(lat), Double.parseDouble(log));
         lat=String.valueOf(gps.getWgLat());
         log=String.valueOf(gps.getWgLon());
      }
      map.put("lng", log);
      map.put("lat", lat);
      return map;
   }
    public static void main(String[] args) {

        // 北斗芯片获取的经纬度为WGS84地理坐标 31.426896,119.496145
        Gps gps = new Gps(31.426896, 119.496145);
        System.out.println("gps :" + gps);
        Gps gcj = gps84_To_Gcj02(gps.getWgLat(), gps.getWgLon());
        System.out.println("gcj :" + gcj);
        Gps star = gcj_To_Gps84(gcj.getWgLat(), gcj.getWgLon());
        System.out.println("star:" + star);
        Gps bd = gcj02_To_Bd09(gcj.getWgLat(), gcj.getWgLon());
        System.out.println("bd  :" + bd);
        Gps gcj2 = bd09_To_Gcj02(bd.getWgLat(), bd.getWgLon());
        System.out.println("gcj :" + gcj2);
    }
}  

根据gprs坐标经纬度返回对应的地址:

我们项目中采用的是阿里云地图进行地址返回,使用的源数据是大地坐标系统 WGS-84需要转换成火星坐标系GCJ-02,转换之后就可以通过阿里云地图接口进行地址返回

 MapAddressForGprsUtil.java

package com.ydtx.utils;

import java.net.URL;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.ydtx.utils.gps.PositionUtil;

import net.sf.json.JSONObject;

public class MapAddressForGprsUtil {
   public static void main(String[] args) {  
        String add = getAdd("22.5810633333333","113.848505");  
        System.out.println(add);
    }  
    /**
     * @author lizhiyuan
     * 定义通过经纬度返回地理位置信息
     * @param lat
* @param log
* @return
*/
public static String getAdd( String lat,String log){  
        //lat 小  log  大  
        //参数解释: 经度,纬度, type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)  
String urlString =null;  
        String res = "";     
        try {   
           //平台和终端用的百度地图需要将经纬度转换成百度地图的经纬度
Map map=PositionUtil.GPSToBaiduMap(log, lat);
           if(map!=null){
              urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+map.get("lat")+","+map.get("lng")+"&type=111";
              map=null;
                URL url = new URL(urlString);    
                java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();    
                conn.setDoOutput(true);    
                conn.setRequestMethod("POST");    
                java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));    
                String line;    
               while ((line = in.readLine()) != null) {    
                   res += line+"\n";    
             }    
                in.close();    
           }
        } catch (Exception e) {    
            //System.out.println("error in wapaction,and e is " + e.getMessage());    
}   
        JSONObject jsonObject = JSONObject.fromObject(res);  
        JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("addrList"));  
        JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0));  
        String area = j_2.getString("admName"); 
        String address=null;
        if(area!=null&&!area.equals("")){
           String arrs[] = area.split(",");    
            String arr= j_2.getString("name");
            
            if(arrs.length==3){
               address=arrs[0]+arrs[1]+arrs[2]+arr;
            }else{
               address=arrs[0]+arrs[1]+arr;
            }
            
            JSONObject j_3 = JSONObject.fromObject(jsonArray.get(1));  
            String name = j_3.getString("name"); 
            if(name!=null&&!name.equals("")){
                String addr= j_3.getString("addr");
                address+=name+addr;
            }
        }else{
           JSONObject j_3 = JSONObject.fromObject(jsonArray.get(1));  
            String name = j_3.getString("admName"); 
            if(name!=null&&!name.equals("")){
                String arrs[] = name.split(",");    
                String name1= j_3.getString("name");
                String arr= j_3.getString("addr");
                address=arrs[0]+arrs[1]+arrs[2]+arr+name1;
            }
        }
        
        return address;    
    }  
}

 测试结果为:

log4j:WARN No such property [maxFileSize] in org.apache.log4j.DailyRollingFileAppender.

广东省深圳市宝安区宝源路西乡体育中心银田路18

 

最后总结吧:

1.一定要了解自己源数据是什么坐标

2.因为百度地图需要申请ak而且返回接口次数有限制,参考了网上的一些博客,采用的阿里云地图接口

   但是在项目中发现部分地区:比如新疆 东北 等等一些偏远地区可能阿里云地图没有采集数据所以无法返回

   地理位置信息

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326273842&siteId=291194637