很多朋友会接触到地图相关的开发,例如我们使用帝国cms开发商店模型。在国内由于google服务很难访问到,所以用到最多的还是百度地图api,首先我们使用前要去百度api申请appkey,申请链接:http://lbsyun.baidu.com/
接着我们使用js在地图中加入坐标点标注,这里的详细参数或者进阶用法,大家可以查阅百度地图api相关说明和示例。
折叠展开JavaScript 代码
-
- var map = new BMap.Map("map_canvas");
- var point = new BMap.Point(longitudeP , latitudeP);
- map.centerAndZoom(point, 15);
- var marker = new BMap.Marker(point);
- map.addOverlay(marker);
参数解释:其中的longitudeP表示经度,latitudeP表示纬度,很多地方也会用lon和lat来表示,这里不细说。map_canvas是html代码中设置的地图容器ID,可以看下面的源码。
页面完整源码:
折叠展开XML/HTML 代码
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>利用html5获取经纬度并在百度地图中展现 cmsdx.com</title>
-
- <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=8827ee39511568ac0705d037d67b2624"></script>
- </head>
-
- <body>
- <span id="support">将下面的经纬度输入谷歌地图:</span>
- <div id="show">
- 纬度:<span id="latitude"></span><br />
- 经度:<span id="longitude"></span><br />
- 准确度:<span id="accuracy"></span>
- </div>
- <script type="text/javascript">
- var doc = document,
- latitude = doc.getElementById('latitude'),
- longitude = doc.getElementById('longitude'),
- accuracy = doc.getElementById('accuracy'),
- support = doc.getElementById('support'),
- showDiv = doc.getElementById('show');
- function lodeSupport(){
- if(navigator.geolocation){
- support.innerHTML = '将下面的经纬度输入谷歌地图(纬度 经度)查看自己位置:';
- showDiv.style.display = 'block';
- navigator.geolocation.getCurrentPosition(updataPosition);
- }else{
- support.innerHTML = '对不起,浏览器不支持!';
- showDiv.style.display = 'none';
- }
- }
- function updataPosition(position){
- var latitudeP = position.coords.latitude,
- longitudeP = position.coords.longitude,
- accuracyP = position.coords.accuracy;
- latitude.innerHTML = latitudeP;
- longitude.innerHTML = longitudeP;
- accuracy.innerHTML = accuracyP;
- //在百度 map中显示地址
- var map = new BMap.Map("map_canvas"); // 创建地图实例
- var point = new BMap.Point(longitudeP , latitudeP); // 创建点坐标
- map.centerAndZoom(point, 15);// 初始化地图,设置中心点坐标和地图级别
- var marker = new BMap.Marker(point);
- map.addOverlay(marker);
-
-
- }
- window.addEventListener('load', lodeSupport , true);
- </script>
- <div id="map_canvas" style="width:500px; height:500px;"></div>
- </body>
- </html>
使用效果:
折叠展开XML/HTML 代码
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>利用html5获取经纬度并在百度地图中展现 cmsdx.com</title>
-
- <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=8827ee39511568ac0705d037d67b2624"></script>
- </head>
-
- <body>
- <span id="support">将下面的经纬度输入谷歌地图:</span>
- <div id="show">
- 纬度:<span id="latitude"></span><br />
- 经度:<span id="longitude"></span><br />
- 准确度:<span id="accuracy"></span>
- </div>
- <script type="text/javascript">
- var doc = document,
- latitude = doc.getElementById('latitude'),
- longitude = doc.getElementById('longitude'),
- accuracy = doc.getElementById('accuracy'),
- support = doc.getElementById('support'),
- showDiv = doc.getElementById('show');
- function lodeSupport(){
- if(navigator.geolocation){
- support.innerHTML = '将下面的经纬度输入谷歌地图(纬度 经度)查看自己位置:';
- showDiv.style.display = 'block';
- navigator.geolocation.getCurrentPosition(updataPosition);
- }else{
- support.innerHTML = '对不起,浏览器不支持!';
- showDiv.style.display = 'none';
- }
- }
- function updataPosition(position){
- var latitudeP = position.coords.latitude,
- longitudeP = position.coords.longitude,
- accuracyP = position.coords.accuracy;
- latitude.innerHTML = latitudeP;
- longitude.innerHTML = longitudeP;
- accuracy.innerHTML = accuracyP;
- //在百度 map中显示地址
- var map = new BMap.Map("map_canvas"); // 创建地图实例
- var point = new BMap.Point(longitudeP , latitudeP); // 创建点坐标
- map.centerAndZoom(point, 15);// 初始化地图,设置中心点坐标和地图级别
- var marker = new BMap.Marker(point);
- map.addOverlay(marker);
-
-
- }
- window.addEventListener('load', lodeSupport , true);
- </script>
- <div id="map_canvas" style="width:500px; height:500px;"></div>
- </body>
- </html>
实际效果: