我如何获取用户的位置? [英] How do I get the location of a user?

查看:124
本文介绍了我如何获取用户的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能在我的网站来获取用户的IP地址。现在我需要他的国家,城市,用他的IP我得到区域..我想通过以下code,

谷歌的地理位置API

 < D​​IV ID =yourinfo>< / DIV>
    &LT;脚本类型=文/ JavaScript的 src=\"http://www.google.com/jsapi?key=ABQIAAAAp04yNttlQq-7b4aZI_jL5hQYPm-xtd00hTQOC0OXpAMO40FHAxQMnH50uBbWoKVHwgpklyirDEregg\"></script>
    &LT;脚本类型=文/ JavaScript的&GT;
        如果(google.loader.ClientLocation)
        {
            visitor_lat = google.loader.ClientLocation.latitude;
            visitor_lon = google.loader.ClientLocation.longitude;
            visitor_city = google.loader.ClientLocation.address.city;
            visitor_region = google.loader.ClientLocation.address.region;
            的visitor_country = google.loader.ClientLocation.address.country;
            的visitor_country code = google.loader.ClientLocation.address.country_ code;
            的document.getElementById('yourinfo')的innerHTML ='&LT; P&GT;纬度/经度:'+ visitor_lat +/+ visitor_lon +'&LT; / P&GT;&LT; P&GT;位置:'+ visitor_city +,+ visitor_region +,+的visitor_country +'('+的visitor_country code +')LT; / p&GT;';
        }
        其他
        {
            的document.getElementById('yourinfo')的innerHTML ='&LT; P&GT;哎呦&LT;!/ P&GT;'。
        }
    &LT; / SCRIPT&GT;

这真的code是不是为我工作。它正在执行else部分并显示哎呦!
如何获取用户的地理位置。任何其他的想法让我知道。

我已经尝试了以下code效果很好,但需要延时6秒免费版本。后来我才知道,谷歌是给这个服务队免费..所以我尝试这一点。

<$p$p><$c$c>http://services.ipaddresslabs.com/iplocation/locateip?key=SAK58RWP983694534K4Z&ip=\".$ClientIP

感谢您提前


解决方案

如果你很高兴去与HTML5,你应该访问这个的教程地理位置

I am able to get the user's IP address in my website. Now i need his country, city, region using his IP i got.. I am trying Google's Geolocation API with the following code,

    <div id="yourinfo"></div>
    <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAp04yNttlQq-7b4aZI_jL5hQYPm-xtd00hTQOC0OXpAMO40FHAxQMnH50uBbWoKVHwgpklyirDEregg"></script>
    <script type="text/javascript">
        if(google.loader.ClientLocation)
        {
            visitor_lat = google.loader.ClientLocation.latitude;
            visitor_lon = google.loader.ClientLocation.longitude;
            visitor_city = google.loader.ClientLocation.address.city;
            visitor_region = google.loader.ClientLocation.address.region;
            visitor_country = google.loader.ClientLocation.address.country;
            visitor_countrycode = google.loader.ClientLocation.address.country_code;
            document.getElementById('yourinfo').innerHTML = '<p>Lat/Lon: ' + visitor_lat + ' / ' + visitor_lon + '</p><p>Location: ' + visitor_city + ', ' + visitor_region + ', ' + visitor_country + ' (' + visitor_countrycode + ')</p>';
        }
        else
        {
            document.getElementById('yourinfo').innerHTML = '<p>Whoops!</p>';
        }
    </script>

Really this code is not working for me. It is executing the else part and displays Whoops!. How to get the Geolocation of users. Any other idea let me know.

I already tried the following code which works well, but takes 6 Seconds of delay in free version. Later i came to know that google is giving this sevice for free.. So i am try on that.

http://services.ipaddresslabs.com/iplocation/locateip?key=SAK58RWP983694534K4Z&ip=".$ClientIP

Thank you in Advance

解决方案

If you are happy to go with HTML5 you should visit this tutorial on geolocation.

这篇关于我如何获取用户的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆