Mysql calculates the real-time location of the address latitude and longitude distance

foreword

When working on a project recently, I encountered a need like this,

Click to use the coupon in the card package, you need to display the coupon to use the nearby store,

train of thought

Database Address Table Design

  1. Common Area Street Address Listtz_sys_area
Field Name type Remark
area_id bigint area ID
area_name varchar(32) area name
parent_id bigint ID of the parent region to which it belongs
level int level
type char Area type 0 country 1 province directly under the central government 2 cities 3 districts and counties
area_name varchar(32) area name
parent_id bigint ID of the parent region to which it belongs
level int level

Background can also be modified

四级区域地址数据The source is the json file I found on the Internet and then poured into the database according to the format. If you need it, you can follow my official account猿小叔

  1. Store Address Listtz_address

Realization of requirements

  1. Here to calculate the distance you need to use经纬度

You need to use the Gaode map api interface 地理/逆地理编码to get the latitude and longitude of the address and save it

 /**
     * 地理/逆地理编码
     * https://lbs.amap.com/api/webservice/guide/api/georegeo
     *
     * @return
     */
    public String addressToLongitude(String address) {
    
    
        String longitude = "";
        String urlString = "?key={key}&address={address}&output=JSON";
        String response = restTemplate.getForObject(ApiAction.API_GEOREGO_TEST + urlString, String.class, apiKey, address);
        if (StrUtil.isEmpty(response)) {
    
    
            return null;
        }
        JSONObject jsonObject = JSON.parseObject(response);
        String code = jsonObject.getString("infocode");
        if (code.equals("10000")) {
    
    
            JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
            JSONObject jsonObject1 = (JSONObject) jsonArray.get(0);
            longitude = jsonObject1.get("location").toString();
        } else {
    
    
            return null;
        }
        return longitude;
    }

use

 private Address setlngAndLat(Address address) {
    
    
        String addr = address.getProvince() + address.getCity() + address.getArea() + address.getAddr();
        String longitude = gaoDeService.addressToLongitude(addr);
        if (StrUtil.isBlank(longitude)) {
    
    
            throw new BusinessException("地址经纬度识别识别");
        }
        String lat = longitude.split(",")[1];
        String lng = longitude.split(",")[0];

        address.setLat(lat);
        address.setLng(lng);
        return address;
    }
  1. MySQL calculates the address distance from the current location based on latitude and longitude
 SELECT
               (
                       6371 * acos(
                                   cos(radians(#{lat}))
                                   * cos(radians(lat))
                                   * cos(radians(lng) - radians(#{lng}))
                               + sin(radians(#{lat}))
                                       * sin(radians(lat))
                       )
                   ) AS distance
        FROM tz_user_addr where addr_id=#{storeAddrId}

Guess you like

Origin blog.csdn.net/u011738045/article/details/124436677