前言:因項目需求以及客戶提出的要求要将百度地圖更換為凱立德地圖,是以就牽扯到經緯度轉換的問題,凱立德官方文檔對這方面提供的資訊少,沒有找到合适的解決方案,是以在幾經周折總算是找到一個比較委托的解決辦法。
地理坐标:437438300,134912100 (第一個參數代表經度,第二個代表次元)
K碼:8q91f4apv
經緯度:121.42437,37.415793
(參數僅供參考)
說明:
凱立德地圖 api 擷取地理坐标以及K碼都有相應的api,大家可以盡情調取,這裡隻根據K碼轉換為經緯度
是以請看轉換的demo(java代碼)
============================================================
由凱立德 k碼 轉換為經緯度
public class KCode {
private double longitude;
private double latitude;
private byte[] map34 = new byte[123];
public KCode() {
map34['0'] = 0;
map34['1'] = 1;
map34['2'] = 2;
map34['3'] = 3;
map34['4'] = 4;
map34['5'] = 5;
map34['6'] = 6;
map34['7'] = 7;
map34['8'] = 8;
map34['9'] = 9;
map34['a'] = 10;
map34['b'] = 11;
map34['c'] = 12;
map34['d'] = 13;
map34['e'] = 14;
map34['f'] = 15;
map34['g'] = 16;
map34['h'] = 17;
map34['i'] = 18;
map34['j'] = 19;
map34['k'] = 20;
map34['m'] = 21;
map34['n'] = 22;
map34['p'] = 23;
map34['q'] = 24;
map34['r'] = 25;
map34['s'] = 26;
map34['t'] = 27;
map34['u'] = 28;
map34['v'] = 29;
map34['w'] = 30;
map34['x'] = 31;
map34['y'] = 32;
map34['z'] = 33;
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
//
private int pow34(int i)
{
int sum = 1;
while(i > 0)
{
sum *= 34;
--i;
}
return sum;
}
//
private int Convert34to10(String str34)
{
int sum = 0;
for(int i = 0; i < str34.length(); ++i)
{
if( (str34.charAt(i) != 'l' && str34.charAt(i) != 'o') &&
((str34.charAt(i) >= '0' && str34.charAt(i) <= '9') ||
(str34.charAt(i) >= 'a' && str34.charAt(i) <= 'z'))
)
{
sum += map34[ str34.charAt(i)] * pow34(i);
}
else
{
System.out.println("invalid chaa");
return -1;
}
}
return sum;
}
//
public int Convert(String kcode)
{
if(kcode.length() != 9)
{
return -1;
}
int lut,lat;
char quad = kcode.charAt(0);
switch(quad)
{
case '5':
lut = Convert34to10(kcode.substring(1, 5));
lat = Convert34to10(kcode.substring(5, 9));
longitude = (double)lut / (3600 * 10) + 105;
latitude = (double)lat / (3600 * 10) + 40;
break;
case '6':
lut = Convert34to10(kcode.substring(1, 5));
lat = Convert34to10(kcode.substring(5, 9));
longitude = (double)lut / (3600 * 10) + 70;
latitude = (double)lat / (3600 * 10) + 40;
break;
case '7':
lut = Convert34to10(kcode.substring(1, 5));
lat = Convert34to10(kcode.substring(5, 9));
longitude = (double)lut / (3600 * 10) + 70;
latitude = (double)lat / (3600 * 10) + 5;
break;
case '8':
lut = Convert34to10(kcode.substring(1, 5));
lat = Convert34to10(kcode.substring(5, 9));
longitude = (double)lut / (3600 * 10) + 105;
latitude = (double)lat / (3600 * 10) + 5;
break;
default:
}
return 0;
}
public static void main(String[] args) {
KCode code = new KCode();
code.Convert("8q91f4apv");
System.out.println(code.getLongitude());
System.out.println(code.getLatitude());
}
}
上述代碼僅供參考。