Geolocation API介绍
文章信息
介绍
从Series 40 Web App 1.5开始,Geolocation API(地理位置API)得到了支持。符合W3C标准的Geolocation API可以使Web App获取手机的位置信息。
在下面的表格中,列出了W3C中关于Geolocation API的对象,接口和方法以及Series 40 Web App的支持情况。
| 对象/接口 | 方法 | 支持情况
|
|---|---|---|
| Geolocation | getCurrentPosition() | 支持 |
| watchPosition() | 不支持 | |
| clearWatch() | 不支持
| |
| PositionOptions | enableHighAccuracy | 不支持 |
| timeout | 支持 | |
| maximumAge | 支持
| |
| Position | Coordinates | 支持 |
| timestamp | 支持
| |
| Coordinates | latitude | 支持 |
| longitude | 支持 | |
| altitude | 支持,但是返回的数据可能没有意义 | |
| accuracy | 支持 | |
| altitudeAccuracy | 不支持 | |
| heading | 不支持 | |
| speed | 不支持
| |
| PostionError | PERMISSION_DENIED | 支持 |
| POSITION_UNAVAILABLE | 支持 | |
| TIMEOUT | 支持 |
位置精度
Series 40 Web App通过基于网络的定位系统获取当前的位置,获取位置的具体方式与Series 40手机平台的版本有关:
- 对于Nokia C2-02,Nokia C2-03,Nokia C2-06手机来说,他们都支持多基站(三角测量)的定位系统。这种情况下的精度取决于手机当前位置的基站密度,即基站越密集则精度越高。在基站非常密集情况下,精度约为几十米;当基站分别稀疏时,精度约为几百米。
- 对于Series 40第五版 FP1以及更早版本的手机,只支持单个基站的定位。这种情况下,定位精度取决于基站的覆盖范围,在城市地区中,精度约为几百米;而在乡村地区大概为几千米。
代码示例
下面的JavaScript代码通过 navigator.geolocation.getCurrentPosition()方法获取位置信息,如果成功则调用geoSuccess方法;否则调用geoFailure方法。 在geoSuccess方法中,首先在页面上显示出当前位置的经纬度和精度。然后通过诺基亚地图服务获取当前位置的地图图片,并显示在页面上:
function getGeo(){
navigator.geolocation.getCurrentPosition(geoSuccess , geoFailure);
}
function geoSuccess(position){
document.getElementById('geoData').innerHTML = "<strong>Lat:</strong> " + position.coords.latitude + " <strong>Long:</strong> "
+ position.coords.longitude + " <strong>Acc:</strong> " + position.coords.accuracy + "m";
document.getElementById('geoData').style.display = 'block';
var container = document.getElementById("mapView");
var mapUrl = "http://m.ovi.me/?c=" + position.coords.latitude + "," + position.coords.longitude + "&u=5&z=10&w=240&h=230";
var map = document.createElement("img");
map.src = mapUrl;
container.innerHTML = "";
container.appendChild(map);
}
function geoFailure(errData){
document.getElementById('mapView').innerText = errData.message;
}
在上面代码中通过document.getElementById方法实现了对html页面的更新,其中用的"geoData"和"mapView"页面元素在index.html中定义如下:
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#B3B300"><input type="button" onclick="getGeo()" value="Get Geo data" /></td>
</tr>
<tr>
<td bgcolor="#FFFF66"><div id="geoData" style="width:100%;height:40px;padding:5px;display:none;"></div></td>
</tr>
<tr>
<td><div id="mapView" style="width:100%;"></div></td>
</tr>
</table>
</body>
上面的代码只是为了说明Geolocation API而截取的代码片段,在这里:File:Simplegeo.wgt 可以下载到完整源代码,下载完成好将wgt文件导入到Nokia Web Tools 1.5中即可。


(no comments yet)