html在不使用任何付费服务的情况下获取用户的地理位置(代码片段)

author author     2023-01-01     159

关键词:

<!-- need to add jquery and jquery.cookie.js-->
<body onload="loadLocation()">
<div id="container">
	<div class="inner-container">
		<div class="box box-50">
			<div class="boxin">
				<div class="header">
					<h3>HTML5 Geo-Location</h3>
				</div>
				<div class="content">
					<ul class="simple">
                        <li class="even">
                            <strong><span id="status"></span></strong>
                        </li>
						<li class="odd">
                            <strong>Your Latitude</strong>
                            <span id="latitude"></span>
                        </li>
                        <li class="even">
                            <strong>Your Longitude</strong>
                            <span id="longitude"></span>
                        </li>
                        <li class="odd">
                            <strong>Accuracy (in Meters)</strong>
                            <span id="accuracy"></span>
                        </li>
						</ul>
                        <div id="placeholder" style="margin: 20px 0px 10px; padding-left: 20px; width: 100%; height: 100%; position: relative;">
                        <i>Note: May take a few seconds to get the location.</i>
                        </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
            
    var latitude;
    var longitude;
    var accuracy;
    
    function loadLocation() 
    
        if(navigator.geolocation) 
            document.getElementById("status").innerHTML = "HTML5 Geolocation is supported in your browser.";
            document.getElementById("status").style.color = "#1ABC3C";
            
            if($.cookie("posLat")) 
                latitude = $.cookie("posLat");
                longitude = $.cookie("posLon");
                accuracy = $.cookie("posAccuracy");
                document.getElementById("status").innerHTML = "Location data retrieved from cookies. <a id=\"clear_cookies\" href=\" javascript:clear_cookies();\" style=\"cursor:pointer; margin-left: 15px;\"> clear cookies</a>";
                updateDisplay();
                
             else 
                navigator.geolocation.getCurrentPosition(
                                    success_handler, 
                                    error_handler, 
                                    timeout:10000);
            
        
    

    function success_handler(position) 
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
        accuracy = position.coords.accuracy;
        
        if (!latitude || !longitude) 
            document.getElementById("status").innerHTML = "HTML5 Geolocation supported, but location data is currently unavailable.";
            return;
        
        
        updateDisplay();
        
        $.cookie("posLat", latitude);
        $.cookie("posLon", longitude);
        $.cookie("posAccuracy", accuracy);
      
    
    
    function updateDisplay() 
        //var gmapdata = '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;ie=UTF8&amp;hq=&amp;ll=' + latitude + ',' + longitude + '&amp;output=embed"></iframe>';
        var gmapdata = '<img src="http://maps.google.com/maps/api/staticmap?center=' + latitude + ',' + longitude + '&zoom=16&size=425x350&sensor=false" />';
                
        document.getElementById("placeholder").innerHTML = gmapdata;
        document.getElementById("latitude").innerHTML = latitude;
        document.getElementById("longitude").innerHTML = longitude;
        document.getElementById("accuracy").innerHTML = accuracy;
    
    
    
    function error_handler(error) 
        var locationError = '';
        
        switch(error.code)
        case 0:
            locationError = "There was an error while retrieving your location: " + error.message;
            break;
        case 1:
            locationError = "The user prevented this page from retrieving a location.";
            break;
        case 2:
            locationError = "The browser was unable to determine your location: " + error.message;
            break;
        case 3:
            locationError = "The browser timed out before retrieving the location.";
            break;
        

        document.getElementById("status").innerHTML = locationError;
        document.getElementById("status").style.color = "#D03C02";
    
    
    function clear_cookies() 
        $.cookie('posLat', null);
        document.getElementById("status").innerHTML = "Cookies cleared.";
    
    
   
</script>

Reference link : http://www.codediesel.com/javascript/adding-html5-geolocation-to-your-applications/

如何在不使用任何付费工具的情况下将 dbf 文件导入 mysql?

】如何在不使用任何付费工具的情况下将dbf文件导入mysql?【英文标题】:howtoimportdbffiletomysqlwithoutusinganypaidtools?【发布时间】:2013-12-0111:58:40【问题描述】:如何在不使用任何付费工具的情况下将dbf文件导入mysql?或者使用任何... 查看详情

如何在不使用核心位置服务的情况下获取用户当前的城市名称?

】如何在不使用核心位置服务的情况下获取用户当前的城市名称?【英文标题】:Howtogettheuser\'scurrentcitynamewithoutusingthecorelocationservice?【发布时间】:2014-07-1101:55:14【问题描述】:所以最初的要求是在应用启动时,移动应用将用... 查看详情

在不提示的情况下在 Powershell 中获取当前用户的凭据对象

】在不提示的情况下在Powershell中获取当前用户的凭据对象【英文标题】:Getcurrentuser\'scredentialsobjectinPowershellwithoutprompting【发布时间】:2010-10-1218:26:40【问题描述】:我有一个Powershell脚本,它将通过自动化工具针对多台服务器... 查看详情

在不使用地图Android的情况下获取用户(纬度,经度)位置[重复]

】在不使用地图Android的情况下获取用户(纬度,经度)位置[重复]【英文标题】:Takeuser(Latitude,Longitude)positionwithoutusingthemapAndroid[duplicate]【发布时间】:2017-03-2815:21:13【问题描述】:我想问一下是否有另一种方法可以在不使用地... 查看详情

如何在不使用 python 中的任何库函数的情况下获取字符的 ASCII 值? [复制]

】如何在不使用python中的任何库函数的情况下获取字符的ASCII值?[复制]【英文标题】:Howcangetthecharacter\'sASCIIvaluewithoutusinganylibraryfunctioninpython?[duplicate]【发布时间】:2021-06-1519:29:31【问题描述】:如何在不使用python的任何库函... 查看详情

在没有浏览器弹出权限的情况下获取粗略用户位置的方法

...ion【发布时间】:2012-04-0213:31:03【问题描述】:是否可以在不使用html5地理定位内容的情况下在网络上找到用户的大致位置,该内容会弹出一个请求用户许可的框?我试过MaxMind但它似乎很差。【问题讨论】:【参考方案1】:当... 查看详情

如何在不使用任何 ID 的情况下从 spotify API 获取艺术家列表

】如何在不使用任何ID的情况下从spotifyAPI获取艺术家列表【英文标题】:HowcanIgetlistofartistsfromspotifyAPIwithoutusinganyID\'S【发布时间】:2020-04-0203:59:14【问题描述】:我正在使用SpotifyAPIS创建一个音乐应用。我想在不使用任何ID的情... 查看详情

如何在不定义任何限制的情况下在 python 中获取任意数量的输入?

】如何在不定义任何限制的情况下在python中获取任意数量的输入?【英文标题】:Howtotakeanynumberofinputsinpythonwithoutdefininganylimit?【发布时间】:2015-10-1402:29:49【问题描述】:现在,问题是我应该从用户那里获取未知数量的输入,... 查看详情

如何在不使用输入元素的情况下捕获 Vuejs 中的任何按键事件

】如何在不使用输入元素的情况下捕获Vuejs中的任何按键事件【英文标题】:HowtocaptureanykeypresseventinVuejswithoutusinginputelement【发布时间】:2017-03-1001:36:18【问题描述】:我正在制作一种游戏,我希望用户输入特定的单词,然后我... 查看详情

在不显示提示信息的情况下,如何获取用户当前位置的经纬度

】在不显示提示信息的情况下,如何获取用户当前位置的经纬度【英文标题】:withoutdisplayingthealertmessage,howtogettheusercurrentlocatationlatitudeandlongitude【发布时间】:2013-08-2412:54:33【问题描述】:我的客户要求在显示用户当前位置以... 查看详情

我可以在不付费的情况下使用 Apple/Google 应用内购买吗?

】我可以在不付费的情况下使用Apple/Google应用内购买吗?【英文标题】:CanIuseApple/GoogleInappPurchasewithoutpaying?【发布时间】:2017-10-0311:48:50【问题描述】:我的任务是为使用应用内购买(使用ionic)的客户创建应用原型。我发现thi... 查看详情

如何在不使用语言环境模块的情况下获取国际货币符号?

】如何在不使用语言环境模块的情况下获取国际货币符号?【英文标题】:Howgettheinternationalcurrencysymbolwithoutusinglocalemodule?【发布时间】:2019-03-2518:21:08【问题描述】:我需要获取iso货币符号,而不使用内置的python语言环境模块... 查看详情

我可以在不返回 html 文件的情况下使用 NodeJS 吗?

】我可以在不返回html文件的情况下使用NodeJS吗?【英文标题】:CanIuseNodeJSwithoutreturninghtmlfiles?【发布时间】:2016-11-1201:27:53【问题描述】:我正在为大多数网站创建一个使用php和javascript的网站。我只想将NodeJS用于网站上的实时... 查看详情

获取特定地理位置的时间

...是白天还是黑夜,或者至少知道那里的时间。有没有办法在不使用网络服务的情况下做到这一点?【问题讨论】:为用户当前位置还是任意位置?您真的想知道太阳是否升起吗?因为这是整个地球的单一计算。但是当地时间要困... 查看详情

java示例代码_在不使用用户名或密码的情况下获取AdminClient Websphere对象

java示例代码_在不使用用户名或密码的情况下获取AdminClient Websphere对象 查看详情

如何在不通过 Web 调用的情况下在浏览器中查找地理位置

】如何在不通过Web调用的情况下在浏览器中查找地理位置【英文标题】:HowtofindgeographicallocationinthebrowserwithoutcallsovertheWeb【发布时间】:2012-01-1315:06:03【问题描述】:我有一个Web应用程序,希望您为自己设置时区和地理位置(国... 查看详情

如何在不使用任何支付网关的情况下验证信用卡?

】如何在不使用任何支付网关的情况下验证信用卡?【英文标题】:HowcanIverifycreditcardwithoutusinganypaymentgateway?【发布时间】:2013-09-1612:26:45【问题描述】:我有一个购物网站。根据要求,我需要提供两种选择——PayPal支付(如果... 查看详情

在不使用 3rd 方库的情况下获取访客位置(IP)[重复]

】在不使用3rd方库的情况下获取访客位置(IP)[重复]【英文标题】:GetVisitorslocation(IP)withoutuseof3rdpartylib[duplicate]【发布时间】:2013-09-1611:52:25【问题描述】:我正在为我的用户创建一个Javascript,并且我正在努力使其尽可能轻。... 查看详情