獲取POI lat lon
大眾點評網
1. if you find a shop, which has the following link
www.dianping.com/shop/3174400/
2. manually attache map to make the link looks like
www.dianping.com/shop/3174400/map
3. do a view source on that page
in the middle of the page, you will find something like
p: “HETHVFZVVJDHUD”
m: $(“ShopGMap”)
shopId: “3174400″
the p: number is the encoded Lat, lon value
following is the script to decode it
<title>jquery test</title>
<script type="text/javascript" src="js/jquery-1.5.1.js"></script>
<script type="text/javascript" src="js/jquery-form-js.js"></script>
<script type="text/javascript">
var settings = { digi: 16, add: 10, plus: 7, cha: 36, center: { lat: 34.957995, lng: 107.050781, isDef: true} };
function decode(poi) {
//alert(poi);
var index = -1;
var count = 0;
var code = "";
var len = poi.length;
var apiKey = poi.charCodeAt(len - 1);
poi = poi.substring(0, len - 1);
len--;
for (var i = 0; i < len; i++) {
var hash = parseInt(poi.charAt(i), settings.cha) - settings.add;
if (hash >= settings.add) {
hash = hash - settings.plus;
}
code += (hash).toString(settings.cha);
if (hash > count) {
index = i;
count = hash;
}
}
var subLL = parseInt(code.substring(0, index), settings.digi);
var addLL = parseInt(code.substring(index + 1), settings.digi);
var lng = (subLL + addLL - parseInt(apiKey)) / 2;
var lat = (addLL - lng) / 100000.0;
lng /= 100000.0;
$('#lat')
.val(lat);
$('#lng').val(lng);
//return { "lat": lat, "lng": lng };
$('#sql').val("update site set thegeom=ST_GeomFromText('POINT("+ lng + " " + lat + ")',4326) where siteid=");
};
</script>
以上是js 方式轉換。
下面是php方式
<?php
#echo $位置.”\n”;
#echo $地圖x軸.”\n”;
#echo $地圖y軸.”\n”;
$db_host = “localhost”;//服務器
$db_user = “root”;//用戶名
$db_psw = “123456″;//密碼
$db_name = “dianping”;//數據庫名
$connection = mysql_connect($db_host,$db_user,$db_psw) or die(“連接服務器失敗”);
mysql_select_db($db_name,$connection) or die(“選擇數據庫失敗”);
mysql_query(“set names’utf-8′”); //utf8
set_time_limit(0);//防止超時。
$querysql = “select * from dianping ” ; //
$queryresult = mysql_query($querysql) or die(“查詢數據失敗”);//執行查詢
while($row = mysql_fetch_array($queryresult)){
$array = decode($row['map']);
$string = $array['0'].”,”.$array['1'];
echo $string.”<br />”;
$id = $row['id'];
$UpdateQuery = “update dianping set map = ‘$string’ where id = ‘$id’”;
$UpdateQueryResult = mysql_query($UpdateQuery) or die(“查詢數據失敗”); //執行查詢
}
/*發現有時候坐標字符串會改變,但是轉換出來的坐標不會變。
$poi = ‘IJJTHFZVITEBVS’;
$poi1 = ‘IJJTGDZVITEBVA’;
$poiss = decode($poi);
$poiss1 = decode($poi1);
print_r($poi).’<br />\n’;
print_r($poiss).’<br />\n’;
print_r($poi1).’<br />\n’;
print_r($poiss1);
*/
function decode($poi) {
$settings = array(
‘digi’=> 16,
‘add’=> 10,
‘plus’=> 7,
‘cha’=> 36,
‘center’=> array(
‘lat’=> 34.957995,
‘lng’=> 107.050781,
‘isDef’=> true
)
);
$i = -1;
$h = 0;
$b = “”;
$j = strlen($poi);
$g = ord($poi{$j-1});
$c = substr($poi, 0, $j-1);
$j–;
for($e=0; $e<$j; $e++) {
$d = base_convert($c{$e}, $settings['cha'], 10) – $settings['add'];
if ($d>=$settings['add']) {
$d = $d- $settings['plus'];
}
$b .= base_convert($d, 10, $settings['cha']);
if ($d>$h) {
$i = $e;
$h = $d;
}
}
$a = base_convert(substr($b, 0, $i), $settings['digi'], 10);
$f = base_convert(substr($b, $i+1), $settings['digi'], 10);
$l = ($a+$f – intval($g))/2;
$k = ($f – $l)/100000;
$l /= 100000;
$lat = $k;
$lng = $l;
return array($lng, $lat);
}