<!doctype html> <html> <head> <meta charset="utf-8"> <title>無標題文檔</title> <script type='text/javascript'> /*獲得用戶的地理位置*/ function getLocation(){ if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(showMap,errorHandler,{ enableHighAccurancy:true, maximumAge:1000}); alert('您的瀏覽器支持'); } else{ alert('您的瀏覽器不支持'); } } /*獲得地理位置成功*/ function showMap(value){ //獲得用戶的經緯度 var longitude = value.coords.longitude; var latitude = value.coords.latitude; alert('用戶位置' + longtitude + ',' + latitude); //調用百度地圖API var map = new BMap.Map('map'); //創建坐標點 var point = new BMap.Point(longitude,latitude); //設置中心點和縮放級別 map.centerAndZoom(point,17); //創建標記 var marker = new BMap.Marker(point); //在地圖上添加標記 map.addOverlay(marker); } /*當發生錯誤時*/ function errorHandler(value){ switch(value.code){ case 1:alert('地理位置服務被拒絕'); break; case 2:alert('地理位置獲取失敗'); break; case 3:alert('獲得地理位置時間超時'); break; case 4:alert('發生未知錯誤'); break; } } /*初始化*/ window.onload = function(){ getLocation(); } </script> </head> <body> <div id='map' style="width:800px; height:800px;"></div> </body> </html>
