惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志
Cloudbric
Cloudbric
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
I
Intezer
Simon Willison's Weblog
Simon Willison's Weblog
博客园_首页
The Cloudflare Blog
C
Cisco Blogs
AWS News Blog
AWS News Blog
IT之家
IT之家
Cyberwarzone
Cyberwarzone
罗磊的独立博客
美团技术团队
V
V2EX
Project Zero
Project Zero
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
有赞技术团队
有赞技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
S
Schneier on Security
P
Privacy International News Feed
V
Visual Studio Blog
量子位
T
Tor Project blog
S
Securelist
腾讯CDC
A
About on SuperTechFans
T
Threat Research - Cisco Blogs
G
GRAHAM CLULEY
B
Blog RSS Feed
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
B
Blog
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
V
Vulnerabilities – Threatpost
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
L
LINUX DO - 热门话题
Recorded Future
Recorded Future

博客园 - hai

会移动的文字(Marquee)学习 How To Share The Internet Connection Between Mac and PC Mac OSX 下配置 Google Android 开发环境 Mac OSX Eclipse3.6下安装erlide搭建erlang集成开发环境 Installing Erlang on Mac OS X 浏览器解析 XML DOM How to set the JAVA_HOME variable in Mac OS X – Snow Leopard code sign error doesn't match any valid certificate private key pair in the default keych 错误的解决办法 CSS的跨浏览器代码准则 Google Maps API详解--02 Google Earth API 讲解--02 Google Maps API 详解--01 Network Programming Using Properties And Synthesize In Objective-C 2.0 For Getters And Setters Tutorial: Networking and Bonjour on iPhone 5个必看的cocos2d 开源游戏 有机会都学习下 iPhone开发技巧之工具篇(1)— 将XIB文件转换为Objective-C源程序 Google Earth API 讲解--04 Google Earth API 讲解--05
浏览器获取地理位置
hai · 2010-07-20 · via 博客园 - hai

转自:http://smithsrus.com/gps-geolocation-in-safari-on-iphone-os-3-0/

GPS Geolocation in Safari on iPhone OS 3.0

I just updated my iPhone to the shiny new OS 3.0. Apple did a great job addressing a few shortcomings and adding new features. Others have alreadytold about the common features so I won’t rehash them here.

But let me tell you about my favorite new feature: Safari can now get your GPS location via javascript! This is usually referred to as geolocation. You can easily grab the current location or get periodic updates.

Okay, so I’m excited over what may seem like an obscure geeky feature, but I think it’s a big deal.

Why GPS in Safari is a Big Deal

  1. Location aware Web pages: Have you ever gone to a company’s Web site to find their nearest location? You usually have to type in your zip code. But what if you’re traveling and don’t know your zip code? Safari knowing your location solves that problem.

  2. Location aware Web applications: It’s fairly easy to create Web content looks and behaves like an iPhone application. Now those can take advantage of location information.

    For example, I’ve been working with a company on an iPhone Web application that interacts with their internal customer database. Field reps can now easily find customers within a certain radius of the rep’s current location who meet various other criteria.

Web Geolocation Privacy and Standards

Don’t worry, all of this is built with good privacy controls. You are asked permission before your location data is revealed to a site. For convenience, it stops asking about a particular site after you have approved it a few times.

These location features are based on the upcoming Geolocation API Specification. Because it’s not just a proprietary Safari thing it should get widespread adoption. In fact, it’s already built into the latest Firefox 3.5 beta.

Web Geolocation Sample Code

The specifications do a good job of explaining the parts so no need to go over all of that. However, it’s worth pointing out how to check and see if geolocation features are available or not and take appropriate action. For example, you may only want to show GPS options if it can actually be used. This simple snippet will do the job.

if (navigator.geolocation) {  
        /* Code if geolocation is available. */  
} else {  
        /* Code if geolocation is not available */
}

Let’s put it all together in a simple Web page that displays all of the geolocation variables for your current location. The code is below or you can try mygeolocation test page from your iPhone.

(Yes, you’ll probably want to use modern DOM techniques for a real project. The purpose of this is just to show a simple working example.)

<!DOCTYPE html>
<html lang="en">
        <head>
                <meta http-equiv="content-type" content="text/html; charset=utf-8">
                <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
                <title>Geolocation Test</title>
 
                <script language="javascript" type="text/javascript">
                        function getLocation() {
                                // Get location no more than 10 minutes old. 600000 ms = 10 minutes.
                                navigator.geolocation.getCurrentPosition(showLocation, showError, {enableHighAccuracy:true,maximumAge:600000});
                        }
 
                        function showError(error) {
                                alert(error.code + ' ' + error.message);
                        }
 
                        function showLocation(position) {
                                geoinfo.innerHTML='<p>Latitude: ' + position.coords.latitude + '</p>' 
                                + '<p>Longitude: ' + position.coords.longitude + '</p>' 
                                + '<p>Accuracy: ' + position.coords.accuracy + '</p>' 
                                + '<p>Altitude: ' + position.coords.altitude + '</p>' 
                                + '<p>Altitude accuracy: ' + position.coords.altitudeAccuracy + '</p>' 
                                + '<p>Speed: ' + position.coords.speed + '</p>' 
                                + '<p>Heading: ' + position.coords.heading + '</p>';
                        }
                </script>
        </head>
        <body>
                <script language="javascript" type="text/javascript">   
                        if (navigator.geolocation) {  
                                document.write('<p><input type="button" onclick="getLocation()" value="Show Geolocation Information" /></p>');
                        } else {  
                                document.write('<p>Sorry, your device or browser software does not appear to support geolocation services.</p>');  
                        }  
                </script>
 
                <div id="geoinfo"></div>
        </body>
</html>

I can’t wait to see some of the great things others will create by combining Web sites with location. And it will only get better as more devices become location aware. Be sure to let me know of good location-aware sites and web apps you run across.

Now go create something cool!