English 中文(简体)
带javascript的地处
原标题:Geolocations with javascript

I m working on a script to get the Geolocations (Lat, lon) that I can use to center my instance of google maps. For now i work with 2 possible technologies. One is the google.loader.ClientLocation object. I haven t tested this one yet because it returns null for me. I think because I m not living on a regular location (Willemstad, Curacao using a wireless internet connection. So my modem is wireless.).

因此,我采用了<代码>navigator.geoposition的备份计划。 这在 Chrome、但火ox中发挥了巨大作用,它给人留下了时间,在伊埃岛上根本不工作。

是否有任何人知道一种更好的替代方法,使用户的地理位置更加稳定,或者有人对我的代码提出建议。

I set a timeout for navigator.geoposition 因为我不想让我的用户等待5秒。 增加停用时间并不能提高火ox的可靠性。

function init_tracker_map() {
 var latitude;
 var longitude;

 if(google.loader.ClientLocation) {
  latitude = (google.loader.ClientLocation.latitude);
  longitude = (google.loader.ClientLocation.longitude);
  buildMap(latitude, longitude);
 }
 else if (navigator.geolocation) { 
  navigator.geolocation.getCurrentPosition(
   function(position) {
    latitude = (position.coords.latitude);
    longitude = (position.coords.longitude);
    buildMap(latitude, longitude);
   },
   function errorCallback(error) {
    useDefaultLatLon();
   },
   {
    enableHighAccuracy:false,
    maximumAge:Infinity,
    timeout:5000
   }
  );
 }
 else {
  useDefaultLatLon();
 }
}

function useDefaultLatLon() {
 latitude = (51.81540697949437);
 longitude = (5.72113037109375);
 buildMap(latitude, longitude);
}

页: 1 我知道,就《SO》而言,还有更多的问题,但无法找到明确的答案。 我希望人们会发现一些新发现。

Update: Tried google gears aswell. Succesfull in chrome again. Fails in FF and IE.

var geo = google.gears.factory.create( beta.geolocation );
if(geo) {
  function updatePosition(position) {
    alert( Current lat/lon is:   + position.latitude +  ,  + position.longitude);
  }

  function handleError(positionError) {
    alert( Attempt to get location failed:   + positionError.message);
  }
  geo.getCurrentPosition(updatePosition, handleError);
}

www.un.org/Depts/DGACM/index_french.htm 从我的工作地点在FF进行罚款。

http://www.ohchr.org。

这发挥了巨大作用。 摘自Pipinfodb.org

var Geolocation = new geolocate(false, true);
Geolocation.checkcookie(function() {
    alert( Visitor latitude code :   + Geolocation.getField( Latitude ));
    alert( Visitor Longitude code :   + Geolocation.getField( Longitude ));
});

function geolocate(timezone, cityPrecision) {
    alert("Using IPInfoDB");
    var key =  your api code ;
    var api = (cityPrecision) ? "ip_query.php" : "ip_query_country.php";
    var domain =  api.ipinfodb.com ;
    var version =  v2 ;
    var url = "http://" + domain + "/" + version + "/" + api + "?key=" + key + "&output=json" + ((timezone) ? "&timezone=true" : "&timezone=false" ) + "&callback=?";
    var geodata;
    var JSON = JSON || {};
    var callback =  function() {
        alert("lol");
    }

    // implement JSON.stringify serialization
    JSON.stringify = JSON.stringify || function (obj) {
        var t = typeof (obj);
        if (t != "object" || obj === null) {
            // simple data type
            if (t == "string") obj =  " +obj+ " ;
            return String(obj);
        } 
        else {
            // recurse array or object
            var n, v, json = [], arr = (obj && obj.constructor == Array);
            for (n in obj) {
                v = obj[n]; t = typeof(v);
                if (t == "string") v =  " +v+ " ;
                else if (t == "object" && v !== null) v = JSON.stringify(v);
                json.push((arr ? "" :  "  + n +  ": ) + String(v));
            }
            return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
        }
    };

    // implement JSON.parse de-serialization
    JSON.parse = JSON.parse || function (str) {
        if (str === "") str =  "" ;
        eval("var p=" + str + ";");
        return p;
    };

    // Check if cookie already exist. If not, query IPInfoDB
    this.checkcookie = function(callback) {
        geolocationCookie = getCookie( geolocation );
        if (!geolocationCookie) {
            getGeolocation(callback);
        } 
        else {
            geodata = JSON.parse(geolocationCookie);
            callback();
        }
    }

    // Return a geolocation field
    this.getField = function(field) {
        try {
            return geodata[field];
        } catch(err) {}
    }

    // Request to IPInfoDB
    function getGeolocation(callback) {
        try {
            $.getJSON(url,
                    function(data){
                if (data[ Status ] ==  OK ) {
                    geodata = data;
                    JSONString = JSON.stringify(geodata);
                    setCookie( geolocation , JSONString, 365);
                    callback();
                }
            });
        } catch(err) {}
    }

    // Set the cookie
    function setCookie(c_name, value, expire) {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expire);
        document.cookie = c_name+ "=" +escape(value) + ((expire==null) ? "" : ";expires="+exdate.toGMTString());
    }

    // Get the cookie content
    function getCookie(c_name) {
        if (document.cookie.length > 0 ) {
            c_start=document.cookie.indexOf(c_name + "=");
            if (c_start != -1){
                c_start=c_start + c_name.length+1;
                c_end=document.cookie.indexOf(";",c_start);
                if (c_end == -1) {
                    c_end=document.cookie.length;
                }
                return unescape(document.cookie.substring(c_start,c_end));
            }
        }
        return   ;
    }
}
问题回答

使用java字母的地处将与5个符合要求的浏览器合作,以便完全脱离IE。

你们的替代办法是利用IP地址确定大约相对相对相对相对相对相对的排位。

采用这一替代方法,假设你找到一个有准确和完整的子/长到IP制图的供应商,你将只能获得ISP的排/长(或ISP与互联网连接的最接近点)。

希望您的期待(大约地点-准确度)

页: 1 哪些设备通过导航器支持 Java字地定位?

我也存在同样的问题,对于没有地处的浏览器来说,在用户IP的基础上使用服务器的边位。

2个免费地处:

我发现,我非常准确。

如果在你的项目内有可能,你可以先询问地点,然后再放到网页上,或要求询问地点。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签