地图上位置计算函数(通过经纬度)
mysql函数
BEGIN RETURN ROUND( 6378.138 * 2 * ASIN( SQRT( POW( SIN( ( lat1 * PI() / 180 - lat2 * PI() / 180 ) / 2 ), 2 ) + COS(lat1 * PI() / 180) * COS(lat2 * PI() / 180) * POW( SIN( ( lng1 * PI() / 180 - lng2 * PI() / 180 ) / 2 ), 2 ) ) ) ,2 ); END
2.mybatis应用
mybatis使用,计算出来的距离单位是千米 userLongitude,userLatitude分别是用户的经纬度 sh.longitude ,sh.latitude是从数据库查询出来商圈的经纬度,因为经纬度在shop中所以需要discounts关联store通过store关联shop获取 sh.longitude ,sh.latitude <select id="getAllAppletDiscountsClientList" parameterType="java.util.Map" resultType="java.util.Map" > select d.id as id, d.name as name, store_id as storeId,DATE_FORMAT(begin_time ,'%Y-%m-%d') as beginTime, DATE_FORMAT(end_time ,'%Y-%m-%d') as endTime, d.type as type, number, demo_number, d.img as img , d.detail as detail ,s.name as storeName,pay_type as payType,pay_money as payMoney , ud.discounts_id as userDiscountsId, getDistance( sh.longitude ,sh.latitude,#{userLongitude,jdbcType=DECIMAL},#{userLatitude,jdbcType=DECIMAL}) as shopDistance from discounts as d inner join store as s on d.store_id =s.id inner join shop as sh on sh.id=s.shop_id left join users_discounts as ud on ud.discounts_id=d.id and ud.users_id= #{userId,jdbcType=CHAR} WHERE d.deleted=0 and now() <= d.end_time <if test="name!= null"> and d.name like concat("%",#{name,jdbcType=VARCHAR},"%") </if> <if test="beginTime!= null"> and d.begin_time >= #{beginTime,jdbcType=TIMESTAMP} </if> <if test="beginTime1!= null"> and d.begin_time <= #{beginTime1,jdbcType=TIMESTAMP} </if> <if test="endTime != null"> and d.end_time >= #{endTime,jdbcType=TIMESTAMP} </if> <if test="endTime1 != null"> and d.end_time <= #{endTime1,jdbcType=TIMESTAMP} </if> <if test="type!= null"> and d.type= #{type,jdbcType=INTEGER} </if> <if test="payType!= null"> and d.pay_type= #{payType,jdbcType=INTEGER} </if> <if test="storeId!= null"> and d.store_id = #{storeId,jdbcType=CHAR} </if> order by shopDistance asc <bind name="start" value="( pageNo - 1 ) * pageSize" /> limit #{start},#{pageSize,jdbcType=INTEGER} </select>

浙公网安备 33010602011771号