<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>地理位置测试</title>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
//var map;
//var gpsPoint;
//var baiduPoint;
//var gpsAddress;
//var baiduAddress; //function getLocation() {
// //根据IP获取城市
// var myCity = new BMap.LocalCity();
// myCity.get(getCityByIP); // //获取GPS坐标
// if (navigator.geolocation) {
// navigator.geolocation.getCurrentPosition(showMap, handleError, { enableHighAccuracy: true, maximumAge: 1000 });
// } else {
// alert("您的浏览器不支持使用HTML 5来获取地理位置服务");
// }
//} //function showMap(value) {
// var longitude = value.coords.longitude;
// var latitude = value.coords.latitude;
// //alert(longitude+" , "+ latitude);
// map = new BMap.Map("map");
// //alert("坐标经度为:" + latitude + ", 纬度为:" + longitude );
// gpsPoint = new BMap.Point(longitude, latitude); // 创建点坐标
// map.centerAndZoom(gpsPoint, 15); // //根据坐标逆解析地址
// //var geoc = new BMap.Geocoder();
// //geoc.getLocation(gpsPoint, getCityByCoordinate); // BMap.Convertor.translate(gpsPoint, 0, translateCallback);
//} //translateCallback = function (point) {
// $("#txtLocation").val(point.lng + "," + point.lat);
// //baiduPoint = point;
// //var geoc = new BMap.Geocoder();
// //geoc.getLocation(baiduPoint, getCityByBaiduCoordinate);
//} //function getCityByBaiduCoordinate(rs) {
// baiduAddress = rs.addressComponents;
// var address = "百度标注:" + baiduAddress.province + "," + baiduAddress.city + "," + baiduAddress.district + "," + baiduAddress.street + "," + baiduAddress.streetNumber;
// var marker = new BMap.Marker(baiduPoint); // 创建标注
// map.addOverlay(marker); // 将标注添加到地图中
// var labelbaidu = new BMap.Label(address, { offset: new BMap.Size(20, -10) });
// marker.setLabel(labelbaidu); //添加百度标注
//} ////根据IP获取城市
//function getCityByIP(rs) {
// var cityName = rs.name;
// alert("根据IP定位您所在的城市为:" + cityName);
//} //function handleError(value) {
// switch (value.code) {
// case 1:
// alert("位置服务被拒绝");
// break;
// case 2:
// alert("暂时获取不到位置信息");
// break;
// case 3:
// alert("获取信息超时");
// break;
// case 4:
// alert("未知错误");
// break;
// }
//} //function init() {
// getLocation();
//} //window.onload = init; </script>
<script>
//初始化时调用获取地理位置方法
$(function () {
startgps();
}); //获取地理位置方法
function startgps() {
//判断是否支持
if (navigator.geolocation) {
//navigator.geolocation.watchPosition(showgps,
navigator.geolocation.getCurrentPosition(showgps,//成功回调函数
getPositionError, //失败回调函数
{ enableHighAcuracy: true, timeout: 1000, maximumAge: 0 }); // 这里设置超时为1000毫秒,即1秒
}
else {
alert("navigator.geolocation获取结果为false");
}
}
function showgps(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
BMap.Convertor.translate(position.coords, 0, function (point) {
alert(point.lng + "," + point.lat);
}); alert("地理位置为latitude:" + latitude + "---longitude=" + longitude);
} function getPositionError(error) {
alert("获取位置失败");
//switch (error.code) {
// case error.TIMEOUT:
// alert("连接超时,请重试");
// break;
// case error.PERMISSION_DENIED:
// alert("您拒绝了使用位置共享服务,查询已取消");
// break;
// case error.POSITION_UNAVAILABLE:
// alert("亲爱的火星网友,非常抱歉,我们暂时无法为您所在的星球提供位置服务");
// break;
//}
}
</script>
</head>
<body>
<input type="text" readonly="readonly" id="txtLocation" />
<div id="map" style="width:600px;height:600px;"></div>
</body>
</html>
05-11 10:57