有谁知道 iPhone 是否支持或即将支持W3C 地理定位规范?
我希望为移动用户构建一个应用程序,但与其花时间为每个不同的平台(iPhone、Android 等)开发应用程序,我更愿意创建一个利用W3C 标准。
有谁知道 iPhone 是否支持或即将支持W3C 地理定位规范?
我希望为移动用户构建一个应用程序,但与其花时间为每个不同的平台(iPhone、Android 等)开发应用程序,我更愿意创建一个利用W3C 标准。
这段代码对我有用——在 iPhone 网络浏览器Safari 上 ,作为一个额外的好处,它甚至可以在我的笔记本电脑上与FireFox 3.5一起使用!地理定位 API 规范是 W3 联盟标准的一部分,但请注意:它还没有最终确定。
(来源:bemoko.com) (来源:bemoko.com)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Geolocation API Demo</title>
<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" name="viewport"/>
<script>
function successHandler(location) {
var message = document.getElementById("message"), html = [];
html.push("<img width='256' height='256' src='http://maps.google.com/maps/api/staticmap?center=", location.coords.latitude, ",", location.coords.longitude, "&markers=size:small|color:blue|", location.coords.latitude, ",", location.coords.longitude, "&zoom=14&size=256x256&sensor=false' />");
html.push("<p>Longitude: ", location.coords.longitude, "</p>");
html.push("<p>Latitude: ", location.coords.latitude, "</p>");
html.push("<p>Accuracy: ", location.coords.accuracy, " meters</p>");
message.innerHTML = html.join("");
}
function errorHandler(error) {
alert('Attempt to get location failed: ' + error.message);
}
navigator.geolocation.getCurrentPosition(successHandler, errorHandler);
</script>
</head>
<body>
<div id="message">Location unknown</div>
</body>
</html>
You can now get location from Javascript APIs in the safari browser following the iPhone 3.0 release - we've created a working example @ http://blog.bemoko.com/2009/06/17/iphone-30-geolocation-javascript-api/
Since iPhone OS 3.0 Safari supports getting geo location. See: Safari Reference Library:Getting Geographic Locations On the other side W3C Geo API specification is still in draft.
I updated MyWhirledView's code. Works great on my iOS 4.3 device. Google no longer requires an API key to access their static map library.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>iPhone 4.0 geolocation demo</title>
<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" name="viewport"/>
<script>
function handler(location) {
var message = document.getElementById("message");
message.innerHTML ="<img src='http://maps.google.com/maps/api/staticmap?center=" + location.coords.latitude + "," + location.coords.longitude + "&zoom=14&size=256x256&maptype=roadmap&sensor=false&markers=color:blue%7Clabel:ABC%7C" + location.coords.latitude + "," + location.coords.longitude + "' />";
message.innerHTML+="<p>Longitude: " + location.coords.longitude + "</p>";
message.innerHTML+="<p>Accuracy: " + location.coords.accuracy + "</p>";
message.innerHTML+="<p>Latitude: " + location.coords.latitude + "</p>";
}
navigator.geolocation.getCurrentPosition(handler);
</script>
</head>
<body>
<div id="message">Location unknown</div>
</body>
</html>
This gap is why I developed the Locatable application -- it's essentially a plug-in for iPhone Safari. Currently it's only available for jailbroken phones.