如何利用【百度地圖API】進行定位?非GPS定位

原文: 如何利用【百度地圖API】進行定位?非GPS定位

若是你能夠上網,若是你有火狐瀏覽器,那麼恭喜你。你能很容易使用如下代碼進行定位!javascript

-------------------------------------------------------------------------------html

百度地圖API是一套由JavaScript語言編寫的應用程序接口,它可以幫助您在網站中構建功能豐富、交互性強的地圖應用程序。java

其中,有Geolocation這個類,能夠幫助你進行定位。chrome

它的原理是,利用瀏覽器自身的定位功能。目前能夠支持地理定位的瀏覽器有Opera 10.6 (包括 Opera Mobile 10.1)、Chrome五、Firefox 3.6以及Safari 5(包括iPhone上的Safari Mobile)。IE瀏覽器各個版本(包括IE9)目前都不支持這個功能。    IE9  RC版本支持該功能。(感謝@diligentpig糾正,2011-4-14 15:30)api

請保存如下代碼爲html格式,並在火狐瀏覽器下運行它。你就能知道你的具體位置啦~瀏覽器

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />
<!-- 引用百度地圖API -->
< script type ="text/javascript" src ="http://api.map.baidu.com/api?v=1.1&services=true" ></ script >
</ head >
< body >
<!-- 百度地圖容器 -->
< div style ="width:697px;height:550px;border:#ccc solid 1px;" id ="dituContent" ></ div >
</ body >
< script type ="text/javascript" >
var map = new BMap.Map( " dituContent " );
var point = new BMap.Point( 116.331398 , 39.897445 );
map.centerAndZoom(point,
12 );
map.enableScrollWheelZoom();

var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(
function (r){
if ( this .getStatus() == BMAP_STATUS_SUCCESS){
var mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
alert(
' 您的位置: ' + r.point.lng + ' , ' + r.point.lat);
}
else {
alert(
' failed ' + this .getStatus());
}
})
</ script >
</ html >

爲何瀏覽器知道你在哪裏?(瀏覽器定位技術)

http://www.iefans.net/liulanqi-zhidao-weizhi/

相關文章
相關標籤/搜索