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
var getLocation = function (successFunc, errorFunc) { //successFunc获取定位成功回调函数,errorFunc获取定位失败回调 //首先设置默认城市
var defCity = {
id: '000001',
name: '北京市',
date: curDateTime()
};
//默认城市
$.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"); // 创建Map实例
var point = new BMap.Point(lon, lat); // 创建点坐标
var gc = new BMap.Geocoder();
gc.getLocation(point, function (rs) {
var addComp = rs.addressComponents;
var curCity = {
id: '',
name: addComp.province,
date: curDateTime()
};
//当前定位城市
$.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:
$.toptip("位置服务被拒绝。");
break;
case 2:
$.toptip("暂时获取不到位置信息。");
break;
case 3:
$.toptip("获取位置信息超时。");
break;
default:
$.toptip("未知错误。");
break;
}
var curCity = {
id: '000001',
name: '北京市',
date: curDateTime()
};
//默认城市
$.cookie('VPIAO_MOBILE_DEFAULTCITY', JSON.stringify(curCity), { expires: 1, path: '/' });
if (errorFunc != undefined)
errorFunc(error);
}, { timeout: 5000, enableHighAccuracy: true });
} else {
$.toptip("你的浏览器不支持获取地理位置信息。");
if (errorFunc != undefined)
errorFunc("你的浏览器不支持获取地理位置信息。");
}
};
var showPosition = function (position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
//var map = new BMap.Map("container"); // 创建Map实例
var point = new BMap.Point(lon, lat); // 创建点坐标
var gc = new BMap.Geocoder();
gc.getLocation(point, function (rs) {
var addComp = rs.addressComponents;
var curCity = {
id: '',
name: addComp.province,
date: curDateTime()
};
//当前定位城市
$.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:
$.toptip("位置服务被拒绝。");
break;
case 2:
$.toptip("暂时获取不到位置信息。");
break;
case 3:
$.toptip("获取位置信息超时。");
break;
default:
$.toptip("未知错误。");
break;
}
var curCity = {
id: '000001',
name: '北京市',
date: curDateTime()
};
//默认城市
$.cookie('VPIAO_MOBILE_DEFAULTCITY', JSON.stringify(curCity), { expires: 1, path: '/' });
};
页面调用
var localcurrent = function() {
var currcookie = $.cookie('VPIAO_PC_CURRENTCITY');
if (currcookie && currcookie != "") {
var currCity = JSON.parse(currcookie);
currentcity = currCity.name;
search(1);
} else {
GetLocationFromSina(function (p) {
var currcookie = $.cookie('VPIAO_PC_CURRENTCITY');
if (currcookie && currcookie != "") {
var currCity = JSON.parse(currcookie);
currentcity = currCity.name; } else {
currentcity = p;
}
search(1);
}, function (error) {
$.toptip(error);
var defcookie = $.cookie('VPIAO_PC_DEFAULTCITY');
if (defcookie && defcookie != "") {
var defCity = JSON.parse(defcookie);
currentcity = defCity.name;
search(1);
}
});
}
};
页面调用
ip查询
var GetClientIp = function () {
var ip;
var url = 'http://chaxun.1616.net/s.php?type=ip&output=json&callback=?&_=' + Math.random();
$.getJSON(url, function (data) {
ip = data.Ip;
});
return ip;
}
ip查询