注冊成為百度用戶,成為地圖開放平臺開發(fā)者,獲取服務(wù)密鑰。通過百度地圖API接口獲取經(jīng)緯度,進而通過API接口進行全球逆地理編碼服務(wù),簡單說就是通過經(jīng)緯度再次定位到具體位置。
代碼為:
<!DOCTYPE? html>
<html>
<head>
<title>IP定位具體位置</title>
</head>
<body>
<form? action="" method="POST">
請輸入公網(wǎng)IP:<input?? type="text"?? name="ip">
<input? type="submit"? name="submit" >
</form>
</body>
</html>
<?php
header("Content-Type:text/html;charset=utf-8;");
$ip=@$_POST['ip'];
//正則匹配IP
if (preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/',$ip)){
$url_ip="http://api.map.baidu.com/location/ip?ip=$ip&ak=您的ak&coor=bd09ll";
//注冊后得到的服務(wù)密鑰ak
$str=file_get_contents($url_ip);
$arr=json_decode($str,true);
if ($arr["status"]==0){
$x=$arr['content']['point']['x'];
$y=$arr['content']['point']['y'];
$url="http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=$y,$x&output=json&pois=1&ak=您的ak";
//注意y代表緯度,x代表精度
$str=file_get_contents($url);
//var_dump($str);
//以下自由發(fā)揮
preg_match("/{.+}/", $str, $result);
$arr=json_decode($result[0],true);
$num=count($arr["result"]["pois"]);
for ($i=0; $i < $num; $i++) {
echo '<font?? color="#ff0000">'.$ip."-----".$arr["result"]["pois"][$i]["addr"]."<br/>";
}
}else{
echo "<h2>not find IP</h2>";
}
}else{
echo "<h2>not? really? IP</h2>";
}
?>
注意替換ak
附加一圖:

參考資料:
http://lbsyun.baidu.com/index.php?title=webapi/ip-api
http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad