HTML
HTML5 Geolocation(地理定位)
Google地图脚本 - 上面的链接向您演示如何使用脚本来显示带有标记、缩放和拖曳选项的交互式地图。
源代码:
点击运行 »
<p id="demo">点击按钮获取您当前坐标(可能需要比较长的时间获取):</p> <button onclick="getLocation()">点我</button> <div id="mapholder"></div> <script src="https://maps.google.com/maps/api/js?sensor=false"></script> <script> var x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition, showError); } else { x.innerHTML = "该浏览器不支持获取地理位置。"; } } function showPosition(position) { lat = position.coords.latitude; lon = position.coords.longitude; latlon = new google.maps.LatLng(lat, lon) mapholder = document.getElementById('mapholder') mapholder.style.height = '250px'; mapholder.style.width = '500px'; var myOptions = { center: latlon, zoom: 14, mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: false, navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL } }; var map = new google.maps.Map(document.getElementById("mapholder"), myOptions); var marker = new google.maps.Marker({ position: latlon, map: map, title: "You are here!" }); } function showError(error) { switch (error.code) { case error.PERMISSION_DENIED: x.innerHTML = "用户拒绝对获取地理位置的请求。" break; case error.POSITION_UNAVAILABLE: x.innerHTML = "位置信息是不可用的。" break; case error.TIMEOUT: x.innerHTML = "请求用户地理位置超时。" break; case error.UNKNOWN_ERROR: x.innerHTML = "未知错误。" break; } } </script>
运行结果:
点击运行 »