1. Introduction
html5 Provides the geolocation attribute for window.navigator, which is used to obtain the current user's geography based on the browser Location.
Window.navigator.geolocation provides 3 methods:
void getCurrentPosition(onSuccess,onError,options); //获取用户当前位置 int watchCurrentPosition(onSuccess,onError,options); //持续获取当前用户位置 void clearWatch(watchId); //watchId 为watchCurrentPosition返回的值 //取消监控 options = { enableHighAccuracy,//boolean 是否要求高精度的地理信息 timeout,//获取信息的超时限制 maximumAge//对地理信息进行缓存的时间 } //options可以不写,为默认即可
2. PositionObject
When the geographical location information is successfully obtained, The position object will be returned in the onsuccess method. Through this object, you can obtain relevant information about the geographical location, including:
position对象的属性: latitude,//纬度 longitude,//经度 altitude,//海拔高度 accuracy,//获取纬度或者经度的精度 altitudeAccurancy,//海拔高度的精度 heading,//设备前景方向。正北方向的顺时针旋转角 speed,//设备的前进速度 m/s timestamp,//获取地理位置信息时候的时间
3. Example based on google map
Look at the code directly :
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>在页面上使用google地图示例</title> </head> <body onload = 'init()'> <div id="map" style='width:800px;height:800px;'></div> <script type="text/javascript" src='http://maps.google.com/maps/api/js?sensor=false'></script> <script type="text/javascript"> function init(){ if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(function(pos){ var coords = pos.coords; var latlng =new google.maps.LatLng(coords.latitude,coords.longitude); var options = {zoom:14,center:latlng,mapTypeId : google.maps.MapTypeId.ROADMAP}; var map1; map1 =new google.maps.Map(document.getElementById('map'),options); var marker =new google.maps.Marker({ position : latlng, map : map1 }); var infowindow =new google.maps.InfoWindow({ content : '当前位置!' }); infowindow.open(map1,marker); }); } } </script> </body> </html>
The above is the detailed content of html5 navigator.geolocation obtains geographical location code case based on browser. For more information, please follow other related articles on the PHP Chinese website!