JS method for obtaining the current geographic location. js current geographic location
The examples in this article share with you the specific code for JS to obtain the current geographic location method for your reference. The specific content is as follows:
1. Mobile Phone Positioning
Var getLocation = function (successFunc, errorFunc) {// callback function for successful location retrieval by successFunc, callback for failed location retrieval by errorFunc // first set the default city var defCity = {id: '2016 ', name: 'beijing', date: curDateTime () // method for obtaining the current time}; // default city $. cookie ('vpiao _ MOBILE_DEFAULTCITY ', JSON. stringify (defCity), {expires: 1, path: '/'}); if (navigator. geolocation) {navigator. geolocation. getCurrentPosition (function (position) {var lat = position. coords. lat Direction; var lon = position. coords. longpolling; // var map = new BMap. map ("container"); // create Map instance var point = new BMap. point (lon, lat); // create Point coordinate var gc = new BMap. geocoder (); gc. getLocation (point, function (rs) {var addComp = rs. addressComponents; var curCity = {id: '', name: addComp. province, date: curDateTime ()}; // the current target 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 cannot be obtained temporarily. "); Break; case 3: alert (" Location Information Retrieval times out. "); Break; default: alert (" Unknown error. "); Break;} var curCity = {id: '000000', 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 obtaining geographic location information. "); If (errorFunc! = Undefined) errorFunc ("your browser does not support obtaining geographic location information. ") ;}}; Var showPosition = function (position) {var lat = position. coords. latitude; var lon = position. coords. longpolling; // var map = new BMap. map ("container"); // create Map instance var point = new BMap. point (lon, lat); // create Point coordinate var gc = new BMap. geocoder (); gc. getLocation (point, function (rs) {var addComp = rs. addressComponents; var curCity = {id: '', name: addComp. province, date: curDateTime ()}; // The current 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 cannot be obtained temporarily. "); Break; case 3: alert (" Location Information Retrieval times out. "); Break; default: alert (" Unknown error. "); Break;} var curCity = {id: '000000', name: 'beijing', date: curDateTime ()}; // default city $. cookie ('vpiao _ MOBILE_DEFAULTCITY ', JSON. stringify (curCity), {expires: 1, path :'/'});};
The premise to introduce Baidu API: <script src = "http://api.map.baidu.com/api? V = 1.4 "type =" text/javascript "> </script>
2. Obtain the Client IP Address
<script>var url = 'http://chaxun.1616.net/s.php?type=ip&output=json&callback=?&_=' + Math.random(); $.getJSON(url, function(data) { alert(data.Ip); });</script>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.