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

推荐订阅源

月光博客
月光博客
博客园_首页
大猫的无限游戏
大猫的无限游戏
Recent Announcements
Recent Announcements
量子位
H
Help Net Security
D
Docker
小众软件
小众软件
Google DeepMind News
Google DeepMind News
U
Unit 42
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
S
SegmentFault 最新的问题
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
Martin Fowler
Martin Fowler
D
DataBreaches.Net
AI
AI
SecWiki News
SecWiki News
V
Visual Studio Blog
Google Online Security Blog
Google Online Security Blog
腾讯CDC
J
Java Code Geeks
Jina AI
Jina AI
O
OpenAI News
N
News | PayPal Newsroom
Help Net Security
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cloudbric
Cloudbric
S
Secure Thoughts
V
V2EX
N
News and Events Feed by Topic
F
Full Disclosure
MyScale Blog
MyScale Blog
The Cloudflare Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Forbes - Security
Forbes - Security
T
Troy Hunt's Blog
WordPress大学
WordPress大学
H
Hacker News: Front Page
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog
Engineering at Meta
Engineering at Meta
Latest news
Latest news
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

博客园 - 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!