这是仅使用两行JavaScript代码即可获得用户地理位置的方法。 第一行加载地理位置JavaScript文件,第二行警告用户位置(在准备就绪的文档中,该文档可以在一行上)。
编码
注意事项:
- 提供的地理位置基于您的ISP的IP地址/位置。
- 您依赖geoplugin.com提供的API服务。
直接在Firebug中运行
jQuery(document).ready(function($) {
jQuery.getScript('http://www.geoplugin.net/javascript.gp', function()
{
var country = geoplugin_countryName();
var zone = geoplugin_region();
var district = geoplugin_city();
console.log("Your location is: " + country + ", " + zone + ", " + district);
});
});
地理位置属性的完整列表
function geoplugin_city() { return 'Dobroyd Point';}
function geoplugin_region() { return 'New South Wales';}
function geoplugin_regionCode() { return '02';}
function geoplugin_regionName() { return 'New South Wales';}
function geoplugin_areaCode() { return '0';}
function geoplugin_dmaCode() { return '0';}
function geoplugin_countryCode() { return 'AU';}
function geoplugin_countryName() { return 'Australia';}
function geoplugin_continentCode() { return 'OC';}
function geoplugin_latitude() { return '-33.873600';}
function geoplugin_longitude() { return '151.144699';}
function geoplugin_currencyCode() { return 'AUD';}
function geoplugin_currencySymbol() { return '$';}
function geoplugin_currencyConverter(amt, symbol) {
if (!amt) { return false; }
var converted = amt * 0.9587170632;
if (converted <0) { return false; }
if (symbol === false) { return Math.round(converted * 100)/100; }
else { return '$'+(Math.round(converted * 100)/100);}
return false;
}
你好世界示例
整合成表格: http : //www.jquery4u.com/api-calls/geolocation-jquery-api-geoplugin/
有关该插件的更多信息: http : //www.geoplugin.com/webservices/javascript
From: https://www.sitepoint.com/geo-location-2-lines-javascript/