BAT大佬推荐使用的HTML5的十个功能
    		       		warning:
    		            这篇文章距离上次修改已过451天,其中的内容可能已经有所变动。
    		        
        		                
                以下是BAT大佬推荐的HTML5的第二个功能:地理位置(Geolocation)API。
HTML5的地理位置(Geolocation)API可以让开发者在无需配置额外的软件的情况下,通过浏览器获取用户的位置信息。
解决方案一:使用navigator.geolocation.getCurrentPosition()方法获取当前位置。
if ("geolocation" in navigator) {
  navigator.geolocation.getCurrentPosition(function(position) {
    console.log("纬度:", position.coords.latitude);
    console.log("经度:", position.coords.longitude);
  });
} else {
  console.log("Geolocation is not supported by this browser.");
}解决方案二:使用navigator.geolocation.watchPosition()方法实时监控位置变化。
if ("geolocation" in navigator) {
  navigator.geolocation.watchPosition(function(position) {
    console.log("纬度:", position.coords.latitude);
    console.log("经度:", position.coords.longitude);
  });
} else {
  console.log("Geolocation is not supported by this browser.");
}注意:获取位置信息可能会受到用户和浏览器的隐私设置的影响,需要用户授权。
以上代码只是获取了位置信息,如果需要在地图上显示,还需要借助其他地图服务API,如Google Maps API、百度地图API等。
评论已关闭