java实现附近站点的功能!!!
最近写了个功能是现实附近站点的功能
我就上代码了
首先我们需要先看看数据库有没有经度维度的代码
然后我们看controller层 我这个是加上了分页插件的
/**
* @param jsonObject
* @return
*/
@PostMapping("/getsitelists")
public List<TbStation> jiancezhanliebiao (@RequestBody JSONObject jsonObject) {
Integer pageNum = jsonObject.getInteger("pageNum");
Integer pageSize = jsonObject.getInteger("pageSize");
//省
String province = jsonObject.getString("province");
//市
String city = jsonObject.getString("city");
//区
String district = jsonObject.getString("district");
//name
String name = jsonObject.getString("name");
Integer type = jsonObject.getInteger("type");
//经度
String longitude = jsonObject.getString("longitude");
//纬度
String latitude = jsonObject.getString("latitude");
List<TbStation> getsitelist = siteService.jiancezhanliebiao(province,city,district,name,type,longitude,latitude,pageNum,pageSize);
return getsitelist;
}
然后我们就一路传参数到service层
算了还是先看看servicer接口层吧
package com.xihewh.carinspection.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.pagehelper.PageInfo;
import com.xihewh.carinspection.entity.TbStation;
import com.xihewh.carinspection.entity.TbStationSetmeal;
import com.xihewh.carinspection.model.StationModel;
import com.xihewh.carinspection.utils.Result;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
/**
* sail
* 2020-08-18
* @author Administrator
*/
public interface SiteService extends IService<TbStation> {
/**
* @param province
* @param city
* @param district
* @param name
* @param type
* @param longitude
* @param latitude
* @param pageNum
* @param pageSize
* @return
*/
List<TbStation> jiancezhanliebiao(String province, String city, String district, String name, Integer type, String longitude, String latitude, Integer pageNum, Integer pageSize);
}
然后我们看service层
package com.xihewh.carinspection.service.impl;
import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xihewh.carinspection.entity.TbStation;
import com.xihewh.carinspection.entity.TbStationSetmeal;
import com.xihewh.carinspection.mapper.SiteMapper;
import com.xihewh.carinspection.mapper.StationSetmealMapper;
import com.xihewh.carinspection.model.LocalModel;
import com.xihewh.carinspection.model.StationModel;
import com.xihewh.carinspection.service.SiteService;
import com.xihewh.carinspection.utils.Result;
import com.xihewh.carinspection.utils.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* sail
* 2020-08-18
* 站点的业务层
* @author Administrator
*/
@Service
public class SiteServiceImpl extends ServiceImpl<SiteMapper, TbStation> implements SiteService {
@Autowired
private SiteMapper siteMapper;
@Autowired
private StationSetmealMapper tbStationSetmealMappert;
/**
* @param province
* @param city
* @param district
* @param name
* @param type
* @param longitude
* @param latitude
* @param pageNum
* @param pageSize
* @return
*/
@Override
public List<TbStation> jiancezhanliebiao(String province, String city, String district, String name, Integer type, String longitude, String latitude, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum,pageSize);
List<TbStation> getsitelist = siteMapper.getsitelist(name, province, city, district,type);
ArrayList<TbStation> objects = new ArrayList<>();
objects.addAll((Collection<? extends TbStation>) getsitelist);
List<TbStation> collect = objects.stream().distinct().collect(Collectors.toList());
for (int i = 0; i < collect.size(); i++) {
TbStation tbStations = collect.get(i);
//纬度
double latitudedouble = Double.parseDouble(latitude);
//经度
double longitudedouble = Double.parseDouble(longitude);
double distance = LocalModel.distance(latitudedouble, longitudedouble, Double.parseDouble(tbStations.getLatitude()), Double.parseDouble(tbStations.getLongitude()));
BigDecimal bigDecimal = new BigDecimal(distance);
double f1 = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
tbStations.setDistance(f1);
}
return getsitelist;
}
}
用到的工具类
package com.xihewh.carinspection.model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @Author namesu
*/
public class LocalModel {
private static final double EARTH_RADIUS = 6378137;
private Location location;
class Location{
public static final double MINLAT = -90;
public static final double MAXLAT = 90;
public static final double MINLNG = -180;
public static final double MAXLNG = 180;
private double lat;
private double lng;
public Location(double lat, double lng) {
this.lat = lat;
this.lng = lng;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
}
private int hashLength = 8;
private int latLength = 20;
private int lngLength = 20;
private double minLat;
private double minLng;
private static final char[] CHARS = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
public LocalModel(double lat, double lng) {
location = new Location(lat, lng);
setMinLatLng();
}
public int gethashLength() {
return hashLength;
}
private void setMinLatLng() {
minLat = Location.MAXLAT - Location.MINLAT;
for (int i = 0; i < latLength; i++) {
minLat /= 2.0;
}
minLng = Location.MAXLNG - Location.MINLNG;
for (int i = 0; i < lngLength; i++) {
minLng /= 2.0;
}
}
public List<String> getBase32Other() {
double leftLat = location.getLat() - minLat;
double rightLat = location.getLat() + minLat;
double upLng = location.getLng() - minLng;
double downLng = location.getLng() + minLng;
List<String> base32For9 = new ArrayList<String>();
//左侧从上到下 3个
String leftUp = getBase32(leftLat, upLng);
if (!(leftUp == null || "".equals(leftUp))) {
base32For9.add(leftUp);
}
String leftMid = getBase32(leftLat, location.getLng());
if (!(leftMid == null || "".equals(leftMid))) {
base32For9.add(leftMid);
}
String leftDown = getBase32(leftLat, downLng);
if (!(leftDown == null || "".equals(leftDown))) {
base32For9.add(leftDown);
}
//中间从上到下 3个
String midUp = getBase32(location.getLat(), upLng);
if (!(midUp == null || "".equals(midUp))) {
base32For9.add(midUp);
}
String midMid = getBase32(location.getLat(), location.getLng());
if (!(midMid == null || "".equals(midMid))) {
base32For9.add(midMid);
}
String midDown = getBase32(location.getLat(), downLng);
if (!(midDown == null || "".equals(midDown))) {
base32For9.add(midDown);
}
//右侧从上到下 3个
String rightUp = getBase32(rightLat, upLng);
if (!(rightUp == null || "".equals(rightUp))) {
base32For9.add(rightUp);
}
String rightMid = getBase32(rightLat, location.getLng());
if (!(rightMid == null || "".equals(rightMid))) {
base32For9.add(rightMid);
}
String rightDown = getBase32(rightLat, downLng);
if (!(rightDown == null || "".equals(rightDown))) {
base32For9.add(rightDown);
}
return base32For9;
}
public boolean sethashLength(int length) {
if (length < 1) {
return false;
}
hashLength = length;
latLength = (length * 5) / 2;
if (length % 2 == 0) {
lngLength = latLength;
} else {
lngLength = latLength + 1;
}
setMinLatLng();
return true;
}
public String getBase32() {
return getBase32(location.getLat(), location.getLng());
}
private String getBase32(double lat, double lng) {
boolean[] bools = getBinary(lat, lng);
if (bools == null) {
return null;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bools.length; i = i + 5) {
boolean[] base32 = new boolean[5];
for (int j = 0; j < 5; j++) {
base32[j] = bools[i + j];
}
char cha = getBase32Char(base32);
if (' ' == cha) {
return null;
}
sb.append(cha);
}
return sb.toString();
}
private char getBase32Char(boolean[] base32) {
if (base32 == null || base32.length != 5) {
return ' ';
}
int num = 0;
for (boolean bool : base32) {
num <<= 1;
if (bool) {
num += 1;
}
}
return CHARS[num % CHARS.length];
}
private boolean[] getBinary(double lat, double lng) {
boolean[] latArray = getHashArray(lat, Location.MINLAT, Location.MAXLAT, latLength);
boolean[] lngArray = getHashArray(lng, Location.MINLNG, Location.MAXLNG, lngLength);
return merge(latArray, lngArray);
}
private boolean[] merge(boolean[] latArray, boolean[] lngArray) {
if (latArray == null || lngArray == null) {
return null;
}
boolean[] result = new boolean[lngArray.length + latArray.length];
Arrays.fill(result, false);
for (int i = 0; i < lngArray.length; i++) {
result[2 * i] = lngArray[i];
}
for (int i = 0; i < latArray.length; i++) {
result[2 * i + 1] = latArray[i];
}
return result;
}
private boolean[] getHashArray(double value, double min, double max, int length) {
if (value < min || value > max) {
return null;
}
if (length < 1) {
return null;
}
boolean[] result = new boolean[length];
for (int i = 0; i < length; i++) {
double mid = (min + max) / 2.0;
if (value > mid) {
result[i] = true;
min = mid;
} else {
result[i] = false;
max = mid;
}
}
return result;
}
public static double distance(double lat1, double lng1, double lat2, double lng2) {
double x1 = Math.cos(lat1) * Math.cos(lng1);
double y1 = Math.cos(lat1) * Math.sin(lng1);
double z1 = Math.sin(lat1);
double x2 = Math.cos(lat2) * Math.cos(lng2);
double y2 = Math.cos(lat2) * Math.sin(lng2);
double z2 = Math.sin(lat2);
double lineDistance =
Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
double realDistance = EARTH_RADIUS * Math.PI * 2 * Math.asin(0.5 * lineDistance) / 180;
return realDistance;
}
}
其实核心的代码是这里
if (name == null || province == null || city == null || district == null) {
//获取纬度
if (oConvertUtils.isEmpty(latitude)) {
System.out.println("==========纬度不能为空==========");
return null;
}
//获取经度
if (oConvertUtils.isEmpty(longitude)) {
System.out.println("==========经度不能为空==========");
return null;
}
//纬度
double latitudedouble = Double.parseDouble(latitude);
//经度
double longitudedouble = Double.parseDouble(longitude);
LocalModel localModel = new LocalModel(latitudedouble, longitudedouble);
localModel.sethashLength(5);
List<String> base32Other = localModel.getBase32Other();
ArrayList<TbStation> stations = new ArrayList<>();
for (int i = 0; i < base32Other.size(); i++) {
QueryWrapper<TbStation> stationQueryWrapper = new QueryWrapper<>();
stationQueryWrapper.lambda().likeRight(TbStation::getCode, base32Other.get(i));
PageHelper.startPage(pageNum,pageSize);
List<TbStation> tbStationList = siteMapper.selectList(stationQueryWrapper);
stations.addAll(tbStationList);
}
List<TbStation> collect = stations.stream().distinct().collect(Collectors.toList());
for (int i = 0; i < collect.size(); i++) {
TbStation tbStations = collect.get(i);
double distance = LocalModel.distance(latitudedouble, longitudedouble, Double.parseDouble(tbStations.getLatitude()), Double.parseDouble(tbStations.getLongitude()));
if (distance > 2000) {
collect.remove(i);
}
}
return (List<TbStation>) collect;
}
剩下就换成自己需要的逻辑就行了 先把代码看懂
没有效果请各位打我一顿!!!!