常用的navigator.geolocation对象有以下三种方法:

获取当前地理位置:navigator.geolocation.getCurrentPosition(success_callback_function, error_callback_function, position_options)

持续获取地理位置(时时定位):navigator.geolocation.watchPosition(success_callback_function, error_callback_function, position_options)

清除持续获取地理位置事件:navigator.geolocation.clearWatch(watch_position_id)

其中success_callback_function为成功之后处理的函数,error_callback_function为失败之后返回的处理函数,参数position_options是配置项,由JSON格式传入:

enableHighAccuracy:true/false,它将告诉浏览器是否启用高精度设备,所谓的高精度设备包含但 不局限于前面所提到的 GPS 和 WIFI,值为 true 的时候,浏览器会尝试启用这些设备,默认指为 true,在这种情况下,浏览器会尽可能地进行更为精确的查询,简单地说,如果用户有可用的 GPS 设备,会返回 GPS 设备的查询结果,IP 是最后的选择,对于移动设备来说,网络接入点(基站)或许成为另

getCurrentPosition() 方法 - 返回数据

若成功,则 getCurrentPosition() 方法返回对象。始终会返回 latitude、longitude 以及 accuracy 属性。如果可用,则会返回其他下面的属性。

属性 描述
coords.latitude 十进制数的纬度
coords.longitude 十进制数的经度
coords.accuracy 位置精度
coords.altitude 海拔,海平面以上以米计
coords.altitudeAccuracy 位置的海拔精度
coords.heading 方向,从正北开始以度计
coords.speed 速度,以米/每秒计
timestamp 响应的日期/时间

检测浏览器是否支持:

 if (navigator.geolocation) {
//console.log("浏览器支持!");
}
else {
// console.log("浏览器不支持!");
}
void getCurrentPosition(onSuccess,onError,options);
//获取用户当前位置 int watchCurrentPosition(onSuccess,onError,options);
//持续获取当前用户位置 void clearWatch(watchId);
//watchId 为watchCurrentPosition返回的值
//取消监控

onSuccess方法成功时调用的(必选),onError方法失败是调用的(可选),options其他参数(可选)

options:

options = {
enableHighAccuracy,   //boolean 是否要求高精度的地理信息
timeout,         //表示等待响应的最大时间,默认是0毫秒,表示无穷时间
maximumAge        /应用程序的缓存时间
}
               

onsuccess方法中会返回position对象,通过这个对象可以获取地理位置的相关信息并在百度地图上显示

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>基于浏览器的HTML5查找地理位置</title>
<!-- 百度API -->
<script src="http://api.map.baidu.com/api?v=1.2" type="text/javascript"></script>
<script> function getLocation(){
var options={
enableHighAccuracy:true,
maximumAge:1000

}
if(navigator.geolocation){
//浏览器支持geolocation
navigator.geolocation.getCurrentPosition(onSuccess,onError,options); }else{
//浏览器不支持geolocation
}
} //成功时
function onSuccess(position){
//返回用户位置
//经度
var longitude =position.coords.longitude;
//纬度
var latitude = position.coords.latitude; //使用百度地图API
//创建地图实例
var map =new BMap.Map("container"); //创建一个坐标
var point =new BMap.Point(longitude,latitude);
//地图初始化,设置中心点坐标和地图级别
map.centerAndZoom(point,15); } //失败时
function onError(error){
switch(error.code){
case 1:
alert("位置服务被拒绝");

break; case 2:
alert("暂时获取不到位置信息");

break; case 3:
alert("获取信息超时");

break; case 4:
alert("未知错误");

break;
} } window.onload=getLocation;
</script>
</head>
<body> <div id="container" style="width:600px;height:600px"></div>
</body>
</html>

嵌入谷歌地图:

function showPosition(position)
{
var latlon=position.coords.latitude+","+position.coords.longitude;

var img_url="http://maps.googleapis.com/maps/api/staticmap?center="
+latlon+"&zoom=14&size=400x300&sensor=false";
document.getElementById("mapholder").innerHTML="<img src='"+img_url+"' />";
}

 谷歌地图脚本:

<body>
<p id="demo">点击这个按钮,获得您的位置:</p>
<button onclick="getLocation()">试一下</button>
<div id="mapholder"></div>
<script src="http://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="Geolocation is not supported by this browser.";}
}

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="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>

时时定位:

Geolocation 对象 - 其他有趣的方法

watchPosition() - 返回用户的当前位置,并继续返回用户移动时的更新位置(就像汽车上的 GPS)。

clearWatch() - 停止 watchPosition() 方法

下面的例子展示 watchPosition() 方法。您需要一台精确的 GPS 设备来测试该例(比如 iPhone):

实例

<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.watchPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br />Longitude: " + position.coords.longitude;
}
</script>

获取当前位置加上偏移量会准一点 

经度+经度校正值: 0.008774687519; 
纬度+纬度校正值: 0.00374531687912;

百度地图API的使用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>百度地图API的使用</title>
<!-- 百度地图API-->
<script src="http://api.map.baidu.com/api?v=1.2" type="text/javascript"></script>
<script type="text/javascript">
function initialize() {
//创建地图实例
var map = new BMap.Map('map');
//创建一个坐标
var point =new BMap.Point(113.264641,23.154905);
//地图初始化,设置中心点坐标和地图级别
map.centerAndZoom(point,15);
}
window.onload = initialize;
</script>
</head>
<body>
<!-- 百度地图地图容器-->
<div id="map" style="width:500px;height:320px"></div>
</body>
</html>
//添加控件
map.addControl(new BMap.MapTypeControl());

MapTypeControl ---------地图类型控件

CopyrightControl --------版权控件

ScaleControl       --------比例尺控件

NavigationControl  ------缩放控件

OverviewMapControl  ----缩略图控件

创建标注:

var marker = new BMap.Marker(point);        // 创建标注
map.addOverlay(marker); // 将标注添加到地图中
创建信息窗口对象
var infoWindow = new BMap.InfoWindow("I am here");    // 创建信息窗口对象
map.openInfoWindow(infoWindow,point);

更多请参考百度开放文档:

http://developer.baidu.com/map/jsdemo.htm#a1_2

最新文章

  1. CADisplayLink 及定时器的使用
  2. Promiscuous Mode
  3. js点击后将文字复制到剪贴板,将图片复制到剪贴板
  4. 最小生成树算法——Kruskal算法
  5. mysql innodb 奔溃问题
  6. RabbitMQ 用户角色详解
  7. Leetcode: Sort Characters By Frequency
  8. sql server 查找包含字符串的对象
  9. Codeforces Round# 305 (Div 1)
  10. PHP LINUX Notice: undefined $_GET完美解决方法
  11. Linux中nat模式上不了网的问题怎么解决?
  12. JS - 侧边导航收缩伸展
  13. jquery 日期获取
  14. style scoped
  15. JVM&#183;垃圾收集器与内存分配策略之垃圾回收算法!
  16. 使用Navicat Premium对mssql2008r2授权用户
  17. android-------- 强引用、软引用、弱引用、虚引用使用
  18. docker私有仓库pull/push
  19. 关于Forsaken Isle
  20. HTML5本地存储(Local Storage) 的前世今生

热门文章

  1. 使用原生js写ajax
  2. 配置samba服务器
  3. Angular学习(2)- ng-app
  4. 【python】浅谈包
  5. C语言每日一题之No.3
  6. 【转】一道SQL SERVER DateTime的试题
  7. JVM知识学习与巩固
  8. linux下安装mysql数据库与相关操作
  9. 图片_ _Android有效解决加载大图片时内存溢出的问题 2
  10. IT项目管理