Use JS to get the current location method summary _javascript Tips

Source: Internet
Author: User

This year's project development, the initial contact with the mobile Web development, but also learning HTML5 edge development, mainly using jquery mobile technology, found that this is not suitable for Internet products, most of the style needs to be rewritten, only part of the function. Mobile phone side of the Web development process for the first time contacted the positioning function, through the major search engines found that the mobile phone is located through the location of the browser, and the first time in the PC browser pop-up prompts "whether to open positioning function", Boss saw this hint, but feel the user's experience is not good, Bad then I change a way to achieve not good, this is not much of the matter, and the boss's face changed greatly, said: should not have such an experience. We do not do this in order to be in a hurry and to release the new features as soon as possible.

1. Mobile Web positioning Method:

Copy Code code as follows:

var getLocation = function (Successfunc, errorfunc) {//successfunc Gets the positioned success callback function, Errorfunc gets the location failure callback
First set the default city
var defcity = {
ID: ' 000001 ',
Name: ' Beijing ',
Date:curdatetime ()//Get Current Time method
};
Default City
$.cookie (' vpiao_mobile_defaultcity ', json.stringify (defcity), {expires:1, path: '/'});
if (navigator.geolocation) {
Navigator.geolocation.getCurrentPosition (function (position) {
var lat = position.coords.latitude;
var lon = Position.coords.longitude;
var map = new Bmap.map ("container"); Create a Map instance
var point = new Bmap.point (lon, LAT); Create point coordinates
var gc = new Bmap.geocoder ();
Gc.getlocation (Point, Function (RS) {
var addcomp = rs.addresscomponents;
var curcity = {
ID: ',
Name:addComp.province,
Date:curdatetime ()
};
Current Location City
$.cookie (' vpiao_mobile_currentcity ', json.stringify (curcity), {expires:7, path: '/'});
Alert (addcomp.province + "," + addcomp.city + "," + Addcomp.district + "," + Addcomp.street);
if (successfunc!= undefined)
Successfunc (Addcomp);
});
},
Function (Error) {
Switch (error.code) {
Case 1:
Alert ("Location service is denied.") ");
Break
Case 2:
Alert ("Location information is temporarily not available.) ");
Break
Case 3:
Alert ("Gets the location information timed out.") ");
Break
Default
Alert ("Unknown error.") ");
Break
}
var curcity = {
ID: ' 000001 ',
Name: ' Beijing ',
Date:curdatetime ()
};
Default City
$.cookie (' vpiao_mobile_defaultcity ', json.stringify (curcity), {expires:1, path: '/'});
if (errorfunc!= undefined)
Errorfunc (Error);
}, {timeout:5000, enablehighaccuracy:true});
} else {
Alert ("Your browser does not support access to geographic information.) ");
if (errorfunc!= undefined)
Errorfunc ("Your browser does not support access to geographic information.) ");
}
};
var showposition = function (position) {
var lat = position.coords.latitude;
var lon = Position.coords.longitude;
var map = new Bmap.map ("container"); Create a Map instance
var point = new Bmap.point (lon, LAT); Create point coordinates
var gc = new Bmap.geocoder ();
Gc.getlocation (Point, Function (RS) {
var addcomp = rs.addresscomponents;
var curcity = {
ID: ',
Name:addComp.province,
Date:curdatetime ()
};
Current Location City
$.cookie (' vpiao_mobile_currentcity ', json.stringify (curcity), {expires:7, path: '/'});
Alert (addcomp.province + "," + addcomp.city + "," + Addcomp.district + "," + Addcomp.street);
});
};
var showpositionerror = function (Error) {
Switch (error.code) {
Case 1:
Alert ("Location service is denied.") ");
Break
Case 2:
Alert ("Location information is temporarily not available.) ");
Break
Case 3:
Alert ("Gets the location information timed out.") ");
Break
Default
Alert ("Unknown error.") ");
Break
}
var curcity = {
ID: ' 000001 ',
Name: ' Beijing ',
Date:curdatetime ()
};
Default City
$.cookie (' vpiao_mobile_defaultcity ', json.stringify (curcity), {expires:1, path: '/'});
};

Premise to introduce Baidu Api:<script src= "http://api.map.baidu.com/api?v=1.4" type= "Text/javascript" ></script>

2.PC End-to-end through IP implementation method:

Using the interface provided by Tencent, this is no longer available.

Copy Code code as follows:

<script type= "Text/javascript" src= "http://fw.qq.com/ipaddress" ></script>
<script type= "Text/javascript" >
document.write (Ipdata[0]); Show IP Address
document.write (ipdata[2]); Show Province
document.write (Ipdata[3]); Show City
</script>

Using Sina interface: HTTP://INT.DPOOL.SINA.COM.CN/IPLOOKUP/IPLOOKUP.PHP?FORMAT=JS

Multi-Region test method: http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=127.0.0.1

Copy Code code as follows:

<script type= "Text/javascript" >
$.getscript (' Http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js ', function (_result) {
if (Remote_ip_info.ret = = ' 1 ') {
Alert (' Country: ' + remote_ip_info.country + ' <BR> Province: ' + remote_ip_info.province + ' <BR> City: ' + remote_ip_info.city + ') <BR> area: ' + remote_ip_info.district + ' <br>isp: ' + remote_ip_info.isp + ' <BR> type: ' + Remote_ip_info.type + ' <BR> Other: ' + Remote_ip_info.desc);
} else {
Alert (' No matching IP address information found! ');
}
});
</script>

NetEase Youdao IP Address interface (this remains to be tested)

Http://www.youdao.com/smartresult-xml/search.s?type=ip&q=IP Address

Taobao request interface (GET)

Copy Code code as follows:

<script>
var ip = "124.127.108.133";
var url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + IP;
$.getjson (URL, function (JSON) {
var myprovince2 = Json.data.area;
var mycity2 = json.data.region;
Alert ("Your city is:" + Myprovince2 + mycity2);
});
</script>

Pacific IP Address Library API interface

Http://whois.pconline.com.cn/?ip=[ip Address string]

In addition: There are Google, Sohu, etc. to provide the corresponding interface, we can try their own.

Sohu IP Address Query interface (default GBK): Http://pv.sohu.com/cityjson

Sohu IP Address Query interface (can be set code): Http://pv.sohu.com/cityjson?ie=utf-8

Sohu additional IP Address query interface: HTTP://TXT.GO.SOHU.COM/IP/SOIP

3. Get Client IP method

Copy Code code as follows:

<script>
var url = ' http://chaxun.1616.net/s.php?type=ip&output=json&callback=?&_= ' + math.random ();
$.getjson (URL, function (data) {
Alert (data. IP);
});
</script>
  

Write this today, there are a lot of work to be done, and so have time to try other interfaces. You are welcome to shoot bricks, to provide a better way.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.