html5定位经纬度不准确,转换百度地图API

使用过html5定位,通过百度地图转化,感觉位置还是很精确,误差也在十来米左右的样子,不通过转换的定位,位置相差很远。现在这个方法就基本很准确了
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="jquery-1.7.1.min.js"></script> 
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的钥匙"></script> 
<script type="text/javascript" src="convertor.js"></script> 

	<title>html5 Gps转百度</title>
	<style type="text/css">
        body, html,#allmap {width: 100%;height: 300px;overflow: hidden;margin:0;font-family:"微软雅黑";}
    </style>
</head>
<body>
	<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
	var map = new BMap.Map("allmap");  
	var longitude, latitude;  
	navigator.geolocation.getCurrentPosition(function (position) {  
		//alert("1");
		longitude = position.coords.longitude;  
		latitude = position.coords.latitude;  
	});  
	setTimeout(function () {  
		var gpsPoint = new BMap.Point(longitude, latitude);  
		BMap.Convertor.translate(gpsPoint, 0, function (point) {  
			var geoc = new BMap.Geocoder();  
			geoc.getLocation(point, function (rs) {  
						
				map.addControl(new BMap.NavigationControl()); 
				map.addControl(new BMap.ScaleControl()); 
				map.addControl(new BMap.OverviewMapControl()); 
				map.centerAndZoom(point, 18); 
				map.addOverlay(new BMap.Marker(point)) ;
				
				//alert("2");
				var addComp = rs.addressComponents;  
				alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);  
				alert('您的位置:'+rs.point.lng+','+rs.point.lat);
			});  
		});  
	}, 1000); 
</script>

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
可以使用百度地图的Elevation API来获取经纬度对应的海拔高度。以下是Java代码示例: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class ElevationAPI { public static void main(String[] args) throws Exception { String ak = "你的百度地图AK"; double lat = 39.915; double lng = 116.404; int coordType = 3; // 坐标类型,3代表WGS84坐标系 String url = "http://api.map.baidu.com/geoconv/v1/?coords=" + lng + "," + lat + "&from=" + coordType + "&to=6&ak=" + ak; String json = httpRequest(url); // 解析返回的json数据 String status = getValueByKey(json, "status"); if ("0".equals(status)) { String x = getValueByKey(json, "x"); String y = getValueByKey(json, "y"); // 获取海拔高度 url = "http://api.map.baidu.com/elevation/v1/?locations=" + y + "," + x + "&coordtype=wgs84&ak=" + ak; json = httpRequest(url); status = getValueByKey(json, "status"); if ("0".equals(status)) { String elevation = getValueByKey(json, "elevation"); System.out.println("海拔高度:" + elevation); } else { System.out.println("获取海拔高度失败,错误码:" + status); } } else { System.out.println("坐标转换失败,错误码:" + status); } } public static String httpRequest(String requestUrl) throws IOException { StringBuilder sb = new StringBuilder(); URL url = new URL(requestUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); InputStream inputStream = connection.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str; while ((str = bufferedReader.readLine()) != null) { sb.append(str); } bufferedReader.close(); inputStreamReader.close(); inputStream.close(); connection.disconnect(); return sb.toString(); } public static String getValueByKey(String json, String key) { String value = ""; int index = json.indexOf(key); if (index > 0) { index += key.length() + 2; boolean flag = true; while (flag) { String tmp = json.substring(index, index + 1); if (!",".equals(tmp) && !"}".equals(tmp)) { value += tmp; index++; } else { flag = false; } } } return value; } } ``` 其中,ak是你的百度地图API Key,lat和lng分别是经纬度,coordType是坐标类型,这里使用的是WGS84坐标系。在代码中,先使用Geoconv API经纬度转换为百度坐标系的x、y坐标,然后再使用Elevation API获取海拔高度。最后输出海拔高度即可。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值