用途及说明见:
http://blogread.cn/it/article/3947
//GeoHash--用字符串实现附近地点搜索
#include
#include
using namespace std;
class GeoCode{
private:
int precision;
string CalcGeo(double x,double range){
string code(precision,'0');
double start=-range,end=range;
double mid;
for(int i=0;iusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeoHashNamespace
{
#region class
class GeoHash{
private int precision; //将经度(纬度)编码为precision位的二进制数据 precision取值效果见文件末注释
private double Longitude,Latitude;//传入的经纬度
private string GeoLongitude,GeoLatitude;//经纬度各自算出的GeoHash
private string GeoCodeBin,GeoCodeBase64;//二维合为一维
string CalcGeo(double x,double range)
{
StringBuilder code=new StringBuilder(new string ('0',precision));
double start=-range,end=range;
double mid;
for(int i=0;i= 0; p--, q--)
{
tmp2[q] = tmp[p];
}
while (q >= 0)
{
tmp2[q--] = '0';
}
GeoLangitude[j++] = tmp2[0]; GeoLangitude[j++] = tmp2[2];
GeoLatitude[k++] = tmp2[1]; GeoLatitude[k++] = tmp2[3];
}
langitude = RecoverGeo(GeoLangitude.ToString(), 180);
latitude = RecoverGeo(GeoLatitude.ToString(), 90);
}
}
#endregion
class Program
{
static void Main(string[] args)
{
GeoHash obj = new GeoHash();//使用构造函数形参默认值
string result =obj.CalcBase64String();
Console.WriteLine("经纬度编码后的Base64字符串为\t"+result);//DgcECA8AAwUGAQYFBQIGCQ==
double longitude=0,latitude=0;
obj.RecoverFromGeo(result, ref longitude, ref latitude);
Console.WriteLine("\nGeoHash解码后的经度为{0}\t", longitude);//116.390621121973
Console.WriteLine("\nGeoHash解码后的纬度为{0}\t", latitude);//39.9232412176207
Console.Read();
}
}
}
/*
* precision取 32 时能保证经度(纬度)十进制数据中9位有效数字的精度。提醒:有效数字计算方法与小数点位置无关。
* sizeof(double) 为8, 对应二进制64位。机内补码表示中,1位符号位,11位指数位,52位尾数位,即最高支持52位的有效数字位数。
*/