查找附近的商家 算法

思路:利用IOS所在位置坐标为圆心,以某个半径为圆的外切正方形的四个顶点为参照物,去和数据库中的商家坐标进行比较,从而找出符合条件的商家。

 

实质上,最本质的就是寻找四个顶点的过程。

 

Java实现:

 

 

private static double degrees(double d) {
		return d * (180 / Math.PI);
	}

	/**
	 * 获取四个顶点的list
	 * @param lgt
	 * @param lat
	 * @param distance
	 * @return
	 */
	public static List<LatlgtPoint> getPointsList(double lgt, double lat,
			double distance) {

		List<LatlgtPoint> pointsList = new ArrayList<LatlgtPoint>();

		double dlng = 2 * Math.asin(Math.sin(distance / (2 * EARTH_RADIUS))
				/ Math.cos(rad(lat)));
		dlng = degrees(dlng);// 一定转换成角度数 原PHP文章这个地方说的不清楚根本不正确 后来lz又查了很多资料终于搞定了

		double dlat = distance / EARTH_RADIUS;
		dlat = degrees(dlat);// 一定转换成角度数

		// 左上角的顶点
		LatlgtPoint leftUpPoint = new LatlgtPoint();
		leftUpPoint.setLat(lat + dlat);
		leftUpPoint.setLgt(lgt - dlng);
		pointsList.add(leftUpPoint);

		// 左下角的顶点
		LatlgtPoint leftDownPoint = new LatlgtPoint();
		leftDownPoint.setLat(lat - dlat);
		leftDownPoint.setLgt(lgt - dlng);
		pointsList.add(leftDownPoint);

		// 右上角的顶点
		LatlgtPoint rightUpPoint = new LatlgtPoint();
		rightUpPoint.setLat(lat + dlat);
		rightUpPoint.setLgt(lgt + dlng);
		pointsList.add(rightUpPoint);

		// 右下角的顶点
		LatlgtPoint rightDownPoint = new LatlgtPoint();
		rightDownPoint.setLat(lat - dlat);
		rightDownPoint.setLgt(lgt + dlng);
		pointsList.add(rightDownPoint);

		return pointsList;

	}

private static final double EARTH_RADIUS = 6378137;

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

/**
	 * 传入经纬度计算距离,单位为km,保留2位小数
	 * 
	 * @param lng1
	 * @param lat1
	 * @param lng2
	 * @param lat2
	 * @return
	 */
	public static String calDistance(float lng1, float lat1, float lng2,
			float lat2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS / 1000;
		return String.format("%.2f", s);
	}

 

 

Point类:

 

package com.chebaobao.api.common.test;

public class LatlgtPoint {
	private double lat;//纬度
	private double lgt;//经度
	public double getLat() {
		return lat;
	}
	public void setLat(double lat) {
		this.lat = lat;
	}
	public double getLgt() {
		return lgt;
	}
	public void setLgt(double lgt) {
		this.lgt = lgt;
	}
	@Override
	public String toString() {
		return "LatlgtPoint [lat=" + lat + ", lgt=" + lgt + "]";
	}

}

 

refurl:http://digdeeply.org/archives/06152067.html

 

http://www.cnblogs.com/cake/p/3240325.html

 

http://dev.mysql.com/doc/refman/5.1/zh/spatial-extensions-in-mysql.html#creating-a-spatially-enabled-mysql-database   引申出来的mysql高版本的空间索引, 以及mongodb,sqlserver2008都有。

 

http://www.oschina.net/question/41761_132578 mongodb

 

http://bbs.csdn.net/topics/390346060?page=1#post-395973698 csdn网上人的解答也不错,比如写一个计算距离的函数,比如有专门的坐标字段

 

下面是通过GPS坐标计算直线距离的:

 

http://gooderlee.iteye.com/blog/1178163

 

http://blog.csdn.net/e_wsq/article/details/6151160

 

http://www.cnblogs.com/ycsfwhh/archive/2010/12/20/1911232.html

Tagged: ,

Comments are closed.