js如何获取用户当前地理位置信息

接口一:可以获取到用户的ip、邮编、省

<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>  
<script type="text/javascript">  
console.log(returnCitySN)
//返回对象Object {cip: "106.5.136.241", cid: "360000", cname: "江西省"}
</script>

接口二:可以获取到用户的省、市

<script type="text/javascript" src="http://ip.ws.126.net/ipquery"></script>
<script type="text/javascript">  
console.log(localAddress["province"]);//省
console.log(localAddress["city"]);//市
</script>

接口三:可以获取到用户的省、市

<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
    $.getScript("http://ip.ws.126.net/ipquery",function(){
            console.log(localAddress);//返回对象Object{city:"南昌市",province:"江西省"}
            province = localAddress["province"];
            province = province.replace(//, '');
            city = localAddress["city"];
            city = city.replace(//, '');
            console.log(province);//江西
            console.log(city);//南昌
        });    
</script>

接口四:可以获取到用户的邮编,省、市、经纬度

<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script>
    var province = '';
    var city = '';
    $.get('https://restapi.amap.com/v3/ip?key=12c0a16fcef033c741fcf169aea63678',function(data){
        console.log(data);//{adcode: "360100",city: "南昌市",info: "OK",infocode: "10000",province: "江西省",rectangle: "115.6786001,28.48182853;116.1596596,28.86719757",status: "1"}
        province = data.province;
        province = province.replace(//, '');
        city = data.city;
        city = city.replace(//, '');
    console.log(province);//江西
    console.log(city);//南昌
    });
</script>

接口五:可以获取到省、市、经纬度

<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script type="text/javascript">
$(function(){
    $.ajax({
    url: 'http://api.map.baidu.com/location/ip?ak=ia6HfFL660Bvh43exmH9LrI6',  
    type: 'POST',  
    dataType: 'jsonp',
    success:function(data) {  
        console.log(data);
        console.log(JSON.stringify(data.content.address_detail.province + "," + data.content.address_detail.city));
    }
    });
})
</script>

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: