H5定位的实现

<!DOCTYPE html>
<html>
<body>
	<p id="demo">点击这个按钮,获得您的坐标:</p>
	<button οnclick="getLocation()">试一下</button>
	<script>
		var x = document.getElementById("demo");
		function getLocation() {
			if (navigator.geolocation) {
				navigator.geolocation.getCurrentPosition(showPosition, showError);
			} else {
				x.innerHTML = "Geolocation is not supported by this browser.";//浏览器不支持定位
			}
		}
		function showPosition(position) {
			x.innerHTML = "Latitude: " + position.coords.latitude
					+ "<br />Longitude: " + position.coords.longitude;
		}
		function showError(error) {
			switch (error.code) {
			case error.PERMISSION_DENIED:
				x.innerHTML = "User denied the request for Geolocation.";//用户不允许地理定位
				break;
			case error.POSITION_UNAVAILABLE:
				x.innerHTML = "Location information is unavailable.";//无法获取当前位置
				break;
			case error.TIMEOUT:
				x.innerHTML = "The request to get user location timed out.";//操作超时
				break;
			case error.UNKNOWN_ERROR:
				x.innerHTML = "An unknown error occurred.";//未知错误
				break;
			}
		}
	</script>
</body>
</html>

原文地址:https://www.cnblogs.com/dulinan/p/12033044.html