首先如何得到IP
1,js取得IP地址的方法一
1 | <script src= "http://pv.sohu.com/cityjson?ie=utf-8" ></script> |
2 | <script type= "text/<a href=" http: //www.jbxue.com/jb/js/" target="_blank" class="infotextkey">javascript</a>"> |
3 | document.write(returnCitySN[ "cip" ]+ ',' +returnCitySN[ "cname" ]) |
4 | </script> |
2,js取得IP地址的方法二
1 | <script language= "javascript" type= "text/javascript" src= "http://fw.qq.com/ipaddress" > |
2 | </script> |
3 | <script>document.write( "你的IP是:" +IPData[0]+ ",来自:" +IPData[2]); |
4 | </script> |
3,js取得ip地址的方法三,腾讯IP,转UTF-8:
1 | <script type= "text/javascript" src= "http://fw.qq.com/ipaddress" charset= "gb2312" ></script> |
2 | $(document).ready( function () { |
3 | |
4 | $( "#ip" ).val(IPData[0]); |
5 | $( "#add" ).val(IPData[2]); |
6 | }) |
其次 根据IP如何得到经纬度
<script type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>
<script type="text/javascript">
var country = geoip_country_code();//城市
var lat = geoip_latitude();//经度
var lon = geoip_longitude();//纬度
alert(country);
alert(lat);
alert(lon);
</script>
最后 根据经纬度得到当前具体地址
js方法:
$.getJSON(
"http://api.map.baidu.com/geocoder/v2/?ak=您百度的密钥&callback=renderReverse&location=纬度,经度&output=json&pois=0",
function(myObject){
alert(myObject);//具体地理位置
});
php方法:
$f=fopen(
"http://api.map.baidu.com/geocoder/v2/?ak=您百度的密钥&callback=renderReverse&location=纬度,经度&output=json&pois=0",r);
$response=fread($f,1024);
fclose($f);
echo $response;//具体地理位置
php写法或者:
$formAction = "
http://api.map.baidu.com/geocoder/v2/";
$postVals = array(
"ak"=>您百度的密钥,
"callback"=>renderReverse,
"location"=>纬度,经度,
"output"=>json,
"pois"=>0
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $formAction);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($postVals));
$res = curl_exec($ch);
curl_close($ch);
echo $res;//具体地理位置