sina的api
var GetLocationFromSina = function (successFunc, errorFunc) { $.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js', function(result) { if (remote_ip_info.ret == '1') { var province = remote_ip_info.province; if (province == "北京" || province == "天津" || province == "上海" || province == "重庆") { province = province + "市"; } else { province = province + "省"; } var curCity = { id: '000000', name: province, date: curDateTime() }; //当前定位城市 $.cookie('VPIAO_PC_CURRENTCITY', JSON.stringify(curCity), { expires: 7, path: '/' }); successFunc(province); } else { var msg = "没有找到匹配的IP地址信息!"; var curCity = { id: '000001', name: '北京市', date: curDateTime() }; //默认城市 $.cookie('VPIAO_PC_DEFAULTCITY', JSON.stringify(curCity), { expires: 1, path: '/' }); if (errorFunc != undefined) errorFunc(msg); } }); };
baidu的api
1 var getLocation = function (successFunc, errorFunc) { //successFunc获取定位成功回调函数,errorFunc获取定位失败回调 2 3 //首先设置默认城市 4 var defCity = { 5 id: '000001', 6 name: '北京市', 7 date: curDateTime() 8 }; 9 //默认城市 10 $.cookie('VPIAO_MOBILE_DEFAULTCITY', JSON.stringify(defCity), { expires: 1, path: '/' }); 11 12 if (navigator.geolocation) { 13 navigator.geolocation.getCurrentPosition(function (position) { 14 var lat = position.coords.latitude; 15 var lon = position.coords.longitude; 16 //var map = new BMap.Map("container"); // 创建Map实例 17 var point = new BMap.Point(lon, lat); // 创建点坐标 18 var gc = new BMap.Geocoder(); 19 gc.getLocation(point, function (rs) { 20 var addComp = rs.addressComponents; 21 var curCity = { 22 id: '', 23 name: addComp.province, 24 date: curDateTime() 25 }; 26 //当前定位城市 27 $.cookie('VPIAO_MOBILE_CURRENTCITY', JSON.stringify(curCity), { expires: 7, path: '/' }); 28 //alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street); 29 if (successFunc != undefined) 30 successFunc(addComp); 31 }); 32 }, 33 function (error) { 34 switch (error.code) { 35 case 1: 36 $.toptip("位置服务被拒绝。"); 37 break; 38 case 2: 39 $.toptip("暂时获取不到位置信息。"); 40 break; 41 case 3: 42 $.toptip("获取位置信息超时。"); 43 break; 44 default: 45 $.toptip("未知错误。"); 46 break; 47 } 48 var curCity = { 49 id: '000001', 50 name: '北京市', 51 date: curDateTime() 52 }; 53 //默认城市 54 $.cookie('VPIAO_MOBILE_DEFAULTCITY', JSON.stringify(curCity), { expires: 1, path: '/' }); 55 if (errorFunc != undefined) 56 errorFunc(error); 57 }, { timeout: 5000, enableHighAccuracy: true }); 58 } else { 59 $.toptip("你的浏览器不支持获取地理位置信息。"); 60 if (errorFunc != undefined) 61 errorFunc("你的浏览器不支持获取地理位置信息。"); 62 } 63 }; 64 var showPosition = function (position) { 65 var lat = position.coords.latitude; 66 var lon = position.coords.longitude; 67 //var map = new BMap.Map("container"); // 创建Map实例 68 var point = new BMap.Point(lon, lat); // 创建点坐标 69 var gc = new BMap.Geocoder(); 70 gc.getLocation(point, function (rs) { 71 var addComp = rs.addressComponents; 72 var curCity = { 73 id: '', 74 name: addComp.province, 75 date: curDateTime() 76 }; 77 //当前定位城市 78 $.cookie('VPIAO_MOBILE_CURRENTCITY', JSON.stringify(curCity), { expires: 7, path: '/' }); 79 //alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street); 80 }); 81 }; 82 var showPositionError = function (error) { 83 switch (error.code) { 84 case 1: 85 $.toptip("位置服务被拒绝。"); 86 break; 87 case 2: 88 $.toptip("暂时获取不到位置信息。"); 89 break; 90 case 3: 91 $.toptip("获取位置信息超时。"); 92 break; 93 default: 94 $.toptip("未知错误。"); 95 break; 96 } 97 var curCity = { 98 id: '000001', 99 name: '北京市', 100 date: curDateTime() 101 }; 102 //默认城市 103 $.cookie('VPIAO_MOBILE_DEFAULTCITY', JSON.stringify(curCity), { expires: 1, path: '/' }); 104 };
页面调用
1 var localcurrent = function() { 2 var currcookie = $.cookie('VPIAO_PC_CURRENTCITY'); 3 if (currcookie && currcookie != "") { 4 var currCity = JSON.parse(currcookie); 5 currentcity = currCity.name; 6 search(1); 7 } else { 8 GetLocationFromSina(function (p) { 9 var currcookie = $.cookie('VPIAO_PC_CURRENTCITY'); 10 if (currcookie && currcookie != "") { 11 var currCity = JSON.parse(currcookie); 12 currentcity = currCity.name; 13 14 } else { 15 currentcity = p; 16 } 17 search(1); 18 }, function (error) { 19 $.toptip(error); 20 var defcookie = $.cookie('VPIAO_PC_DEFAULTCITY'); 21 if (defcookie && defcookie != "") { 22 var defCity = JSON.parse(defcookie); 23 currentcity = defCity.name; 24 search(1); 25 } 26 }); 27 } 28 };
ip查询
1 var GetClientIp = function () { 2 var ip; 3 var url = 'http://chaxun.1616.net/s.php?type=ip&output=json&callback=?&_=' + Math.random(); 4 $.getJSON(url, function (data) { 5 ip = data.Ip; 6 }); 7 return ip; 8 }