日期:2014-05-17 浏览次数:20861 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">?
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">?
??? <head>?
??????? <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">?
??????? <title>html5获取地理位置并显示在goole地图</title>?
??? </head>?
??? <body>?
??????? <input type="button" id="getPos" value="获取我的位置">?
??????? <div id="info" class="">?
??????????? 您所在的位置: 经度?
??????????? <span class="tip">unknown</span>,纬度?
??????????? <span class="tip">unknown</span>?
??????? </div>?
?
?
? <script src="http://www.google.cn/maps?file=api&v=2&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA&hl=zh-CN"
?????????? type="text/javascript">
???
</script>
?
??????? <script type="text/javascript">?
??????????? var t = 0;?
??????????? var dom = {?
??????????????? btn : document.getElementById('getPos'),?
??????????????? info : document.getElementById('info')?
??????????? };?
?
??????????? dom.btn.onclick = function(){?
??????????????? if (navigator.geolocation) {?
??????????????????? dom.info.innerHTML = "请等待查询结果返回";?
??????????????????? dom.info.className = "warn";?
??????????????????? navigator.geolocation.getCurrentPosition(getPositionSuccess,getPositionError,{timeout:5000});?
??????????????? }else {?
???????????????????? dom.info.innerHTML = "抱歉,您所使用的浏览器不支持 Geolocation 接口";?
???????????????????? dom.info.className = "warn";?
??????????????? }?
??????????? }?
?????????????
?????????????
??????????? function getPositionSuccess(position){?
??????????????? var lat = position.coords.latitude;?
??????????????? var lng = position.coords.longitude;?
??????????????? dom.info.innerHTML = "您所在的位置: 经度" + lat + ",纬度" + lng;?
?????????????????
?????????????????
??????????????? if(typeof position.address === "undefined"){?
??????????????????? dom.info.innerHTML += "<br /><span class='tip'>您的浏览器目前仅提供坐标查询,使用 Firefox 3.5+ 可获得更多信息</span>";?
??????????????? }else{?
??????????????????? dom.info.innerHTML += "<br /><span class='tip'>" + position.address.country + " , " + position.address.region + " , " + position.address.city+"</span>";?
??????????????? }?
????
????goolemap(position);
?????
??????????? }?
?????????????
??????????? function goolemap(position) {
????if (GBrowserIsCompatible()) {
?????var map = new GMap2(document.getElementById("map_canvas"));
?????
?????var center = new GLatLng(position.coords.latitude,position.coords.longitude);
?????
?????map.setCenter(center, 14);
?????
?????map.addControl(new GSmallMapControl());
?????map.addControl(new GMapTypeControl());
?????
?????var marker = new GMarker(center, {draggable: false});
?????marker.openInfoWindowHtml("公司所在地" + position.coords.latitude + "??? " +? position.coords.longitude + "??? " + map.getZoom());
??????? map.addOverlay(marker);
????}?
???}
???????????
??????????? function getPositionError(error){?
??????????????? switch(error.code){?
??????????????????? case error.TIMEOUT :?
??????????????????????? dom.info.innerHTML = "连接超时,请重试";?
??????????????????????? break;?
??????????????????? case error.PERMISSION_DENIED :?
??????????????????????? dom.info.innerHTML = "您拒绝了使用位置共享服务,查询已取消";?
??????????????????????? break;?
??????????????????? case error.POSITION_UNAVAILABLE :??
??????????????????????? dom.info.innerHTML = "亲爱的火星网友,非常抱歉<br />我们暂时无法为您所在的星球提供位置服务";?
??????????????????????? break;?
??????????????? }?
??????????? }?
?????????????
??????? <