var iplocation_url = 'https://www.localizaip.com.br/api/iplocation.php'; var ip_data = new Array(); //GLOBAL //definir se usuario nao definiu if (typeof LIP_done == 'undefined') LIP_done = false; //make sure callback only once if (typeof LIP_timeout == 'undefined') LIP_timeout = 10000; //10 segundos if (LIP_timeout<2000) LIP_timeout=2000; //minimo=2000 if (typeof LocalizaIP_done == 'undefined') LocalizaIP_done=LocalizaIP_success; //nome antigo function LocalizarIP(){ if ((typeof LIP_LowPrecision != 'undefined') && (LIP_LowPrecision)) use_LIP_DB(); else if (navigator.geolocation) { setTimeout('HTML5geoIPError(1);', LIP_timeout); navigator.geolocation.getCurrentPosition(function(position) { HTML5geoIPFound(position.coords.latitude, position.coords.longitude, position.coords.accuracy); }, function(err) { HTML5geoIPError(err) }, {enableHighAccuracy:false, timeout:LIP_timeout-1000, maximumAge:60*60*1000} ); } else { use_LIP_DB(); } } function HTML5geoIPFound(mylat, mylng, accuracy) { mylat = (Math.round(mylat*1000000)/1000000); //arredondar 6 casas mylng = (Math.round(mylng*1000000)/1000000); //arredondar 6 casas accuracy = Math.round(accuracy); ip_data['latitude'] = mylat; ip_data['longitude'] = mylng; /* var xhttp = new XMLHttpRequest(); xhttp.open("GET","https://maps.googleapis.com/maps/api/geocode/json?latlng="+mylat+","+mylng+"&sensor=false&key=AIzaSyDCQDTQw518noANcH8o-m8qzQ8UdYQxnhk",true); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { var city, state, countryCode, countryName; textResult = xhttp.responseText; eval("data=("+textResult+")"); //console.log(data); for (i = 0; i < data['results'].length; i++) { result = data['results'][i]; for (j = 0; j < result['address_components'].length; j++) { address_comp = result['address_components'][j]; if (address_comp['types'] == "locality,political") city = address_comp['long_name']; if (address_comp['types'] == "administrative_area_level_1,political") state = address_comp['short_name']; if (address_comp['types'] == "country,political") { countryCode = address_comp['short_name']; countryName = address_comp['long_name']; } } //console.log('country: '+countryCode+' - Estado: '+state+' - City: '+city); } ip_data['errorCode'] = 0; ip_data['errorMsg'] = ''; ip_data['city'] = city; ip_data['state'] = state; ip_data['country'] = countryName; ip_data['countryCode'] = countryCode; ip_data['src']='HTML5';*/ var xh0 = new XMLHttpRequest(); //pegar ISP e ReverseDNS xh0.open("GET",iplocation_url,true); xh0.onreadystatechange = function() { // if (xhttp.readyState == 4 && xhttp.status == 200) { textResult = xh0.responseText; if (!textResult) return; //vazio - ocorriam varias chamadas a este processo, algumas com os dados ainda vazios ip_data_tmp = new Array(); eval("ip_data_tmp=("+textResult+")"); ip_data['ip'] = ip_data_tmp['ip']; ip_data['provider'] = ip_data_tmp['provider']; ip_data['reverseIP'] = ip_data_tmp['reverseIP']; if ((!LIP_done) && (typeof LocalizaIP_done == 'function')) { LIP_done = true; LocalizaIP_done(ip_data); } // } } xh0.send(); //registrar no DB /* var xh1 = new XMLHttpRequest(); xh1.open("GET",'https://www.localizaip.com.br/databank/ipinfo.php?ip=this&city='+city+'&state='+state+'&country='+countryCode+'&lat='+mylat+'&lng='+mylng+'&accu='+accuracy,true); xh1.send(); //registra no BD } } xhttp.send();*/ } //use internal LocalizaIP DB function use_LIP_DB(){ if (LIP_done) return; //ja foi por outro caminho var xh = new XMLHttpRequest(); xh.open("GET",iplocation_url,true); xh.onreadystatechange = function() { if (xh.readyState == 4 && xh.status == 200) { textResult = xh.responseText; eval("ip_data=("+textResult+")"); if ((!LIP_done) && (typeof LocalizaIP_done == 'function')) { //como eh assincrono, preciso checar LIP_done de novo LIP_done = true; LocalizaIP_done(ip_data); } } } xh.send(); } function HTML5geoIPError(err){ //alert('Erro localizando: '+err.code); use_LIP_DB(); } LocalizarIP();