高得地图js api,你地理编码的异步延时
来源:互联网 发布:网络趣事排行 编辑:程序博客网 时间:2024/04/29 18:08
高得地图js api,你地理编码的异步延时问题,
1.如果只有一对坐标点时没有延时,
2.而当多对坐标点的数组时就会有一种延时,并且遍历时,数组次序会乱的情况。
参考网址:http://lbs.amap.com/api/webservice/guide/api/georegeo/
参考例子:http://lbs.amap.com/api/javascript-api/example/geocoder/regeocoding
解决的方法是:一种:在遍历时,在index等于长度时给其加上一种延时1秒,来获取这块。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="css/special.css" rel="stylesheet" />
<link href="css/demo.Default.css" rel="stylesheet" />
<link href="css/home.css?v=1.5" rel="stylesheet" />
<script src="js/jquery-1.8.3.min.js"></script>
<script src="js/special.js"></script>
<script src="js/es5.min.js"></script>
<script src="http://webapi.amap.com/maps?v=1.3&key=你自己的key"></script>
</head>
<body onLoad="mapInit()">
<div id="iCenter"></div>
<div id="iControlbox">
<ul>
<li>
<input type="text" id="key_11" value="116.51413" />
<input type="text" id="key_12" value="39.912896" />
<button onclick="javascript:geocoder2();">逆地址解析</button>
</li>
</ul>
</div>
<div id="result" style="height:400px;"></div>
</body>
<script type="text/javascript">
var mapObj;
var MGeocoder;
var marker = [];
function mapInit() {
mapObj = new AMap.Map('iCenter'); //默认定位:初始化加载地图时,center及level属性缺省,地图默认显示用户所在城市范围
};
var addressArr = [];
var addressStr = "";
//坐标转地址
var json = [{ "lngX": "117.1419139142", "latY": "39.1157409540" }, { "lngX": "117.183078", "latY": "39.121749" }, { "lngX": "117.183078", "latY": "39.121749" }, { "lngX": "117.183078", "latY": "39.121749" }];
var jsonArray = [];
for (var i = 0 ; i < json.length; i++) {
jsonArray[i] = json[i];
}
//console.log(typeof (jsonArray));
function geocoder2() { //POI搜索,关键字查询
var getLen = jsonArray.length;
$.each(jsonArray, function (index, item) {
var getlngX = parseFloat(item.lngX); //
var getlatY = parseFloat(item.latY); //
var arr_dingwei = GPS.gcj_encrypt(getlatY, getlngX); //定位,将gps坐标转成火星坐标
getlngX = arr_dingwei.lon.toFixed(6);
getlatY = arr_dingwei.lat.toFixed(6);
var lnglatXY = new AMap.LngLat(getlngX, getlatY);
//加载地理编码插件
mapObj.plugin(["AMap.Geocoder"], function () {
MGeocoder = new AMap.Geocoder({
city: "010" //城市,默认:“全国”
});
//返回逆地理编码结果
AMap.event.addListener(MGeocoder, "complete", getAddress_CallBack);
//逆地理编码
MGeocoder.getAddress(lnglatXY);
});
////加点
var marker = new AMap.Marker({
map: mapObj,
icon: "http://webapi.amap.com/images/0.png",
position: lnglatXY,
offset: new AMap.Pixel(-5, -30)
});
mapObj.setFitView();
var index2 = index + 1;
if (index2 == getLen) {
setTimeout(function () {
//alert(json.length);
console.log(addressStr);
}, 1000)
}
});
}
//回调函数
function getAddress_CallBack(data) {
var resultStr = "";
var address;
//返回地址描述
address = data.regeocode.formattedAddress;
//alert(address);
addressStr += address+",";
//addressArr.push(address);
//返回地址 结果拼接输出
resultStr = "<div style=\"font-size: 12px;padding:0px 0 4px 2px; border-bottom:1px solid #C1FFC1;\">" + "<b>地址</b>:" + address + "</div>";
$("#result").append(resultStr);
}
//几种坐标的转换
var GPS = {
PI: 3.14159265358979324,
x_pi: 3.14159265358979324 * 3000.0 / 180.0,
delta: function (lat, lon) {
var a = 6378245.0; // a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
var ee = 0.00669342162296594323; // ee: 椭球的偏心率。
var dLat = this.transformLat(lon - 105.0, lat - 35.0);
var dLon = this.transformLon(lon - 105.0, lat - 35.0);
var radLat = lat / 180.0 * this.PI;
var magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
var sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
return { 'lat': dLat, 'lon': dLon };
},
//WGS-84原始坐标系 to GCJ-02火星坐标系
gcj_encrypt: function (wgsLat, wgsLon) {
if (this.outOfChina(wgsLat, wgsLon))
return { 'lat': wgsLat, 'lon': wgsLon };
var d = this.delta(wgsLat, wgsLon);
return { 'lat': wgsLat + d.lat, 'lon': wgsLon + d.lon };
},
//GCJ-02火星坐标系 to WGS-84原始坐标系 exactly
gcj_decrypt_exact: function (gcjLat, gcjLon) {
var initDelta = 0.01;
var threshold = 0.000000001;
var dLat = initDelta, dLon = initDelta;
var mLat = gcjLat - dLat, mLon = gcjLon - dLon;
var pLat = gcjLat + dLat, pLon = gcjLon + dLon;
var wgsLat, wgsLon, i = 0;
while (1) {
wgsLat = (mLat + pLat) / 2;
wgsLon = (mLon + pLon) / 2;
var tmp = this.gcj_encrypt(wgsLat, wgsLon)
dLat = tmp.lat - gcjLat;
dLon = tmp.lon - gcjLon;
if ((Math.abs(dLat) < threshold) && (Math.abs(dLon) < threshold))
break;
if (dLat > 0) pLat = wgsLat; else mLat = wgsLat;
if (dLon > 0) pLon = wgsLon; else mLon = wgsLon;
if (++i > 10000) break;
}
return { 'lat': wgsLat, 'lon': wgsLon };
},
outOfChina: function (lat, lon) {
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
},
transformLat: function (x, y) {
var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
return ret;
},
transformLon: function (x, y) {
var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
return ret;
}
};
</script>
</html>
- 高得地图js api,你地理编码的异步延时
- 基于高德地图的地理编码
- js引入高德地图,加标注,逆地理编码
- 高德地图地理编码和逆地理编码,以及逆地理编码的时候如何去掉省、市、镇
- 高德地图之地理编码的使用
- 高德地图 正向地理编码 逆向地理编码
- 高德地图地理编码和逆地理编码
- android 百度地图api 地理编码&反地理编码
- Android 百度地图api地理编码与逆地理编码
- 高德地图之地理编码
- 高德地图之逆地理编码
- Android 地图的地理编码与地理反编码
- 高德地图Js API的使用
- Google Map API和百度地图API用于地理编码
- 百度地图开放平台 Web服务API --Geocoding API (地理编码和逆地理编码)
- 一个使用高德地图SDK进行逆地理编码的例子
- 基于高德地图api和Python的区县地理边界坐标提取
- 安卓高德地图API根据城市名获取对应的经纬度和地理编码
- 909422229_Axis配合Eclipse发布接口
- hdu1150—Machine Schedule(二分图最大匹配)
- trident demo 2
- jQuery核心技术 (二)
- java命令行运行java项目
- 高得地图js api,你地理编码的异步延时
- 仿支付宝记账本效果(二)
- LeetCode Maximum subarray,最大子串问题
- 十款免费移动应用测试框架推荐
- 字节序
- 【Mongo】创建索引
- 下载出错Error:Flash Download Failed-"Cortex-M3"
- mysql之MyISAM和InnoDB
- 在Xshell上使用XFTP上传文件到Linux服务器