天地图实现标注用户当前坐标位置

要想获取用户的位置就必须要知道用户设备所在的经纬度,而设备获取经纬度一般采用以下方法

1、IP地址

2、GPS全球定位系统(较准确)

3、WIFI无线网络

4、基站3G4G网络(较准确)

获取用户坐标我采用的是H5的Geolocation地理位置对象

navigataor.geolocation

方法:

》经度:coords.longitude

》维度:coords.latitude

<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″/>
<meta name=”keywords” content=”天地图”/>
<title>天地图-地图API</title>
<script type=”text/javascript” src=”http://api.tianditu.com/js/maps.js”></script>
<script>
var map;
var zoom = 12;
function onLoad()
{
//初始化地图对象
map=new TMap(“mapDiv”);
navigator.geolocation.getCurrentPosition(function(position){

var y = position.coords.longitude;//经度
var x = position.coords.latitude;//维度
console.log(y,x);
//设置显示地图的中心点和级别
map.centerAndZoom(new TLngLat(y,x),zoom);
//允许鼠标滚轮缩放地图
map.enableHandleMouseScroll();

//向地图上添加自定义标注
var marker = new TMarker(new TLngLat(y,x));
//向地图上添加标注
map.addOverLay(marker);
});

}
</script>
</head>
<body onLoad=”onLoad()”>
<div id=”mapDiv” style=”position:absolute;width:600px; height:500px”></div><!–天地图地图板块Id–>
</body>
</html>

———————
作者:趁早–我是认真的
来源:CSDN
原文:https://blog.csdn.net/Mark_JF00/article/details/53440589
版权声明:本文为博主原创文章,转载请附上博文链接!

Leave a Comment