通过浏览器获取用户地理位置
来源:互联网 发布:网络打国际长途 编辑:程序博客网 时间:2024/05/29 08:36
<span style="font-size:14px;">$(function(){if (window.navigator.geolocation){ var options = { enableHighAccuracy: true//是否选择高精度 }; window.navigator.geolocation.getCurrentPosition(handleSuccess, handleError, options); } else { alert("Your browser does not support getting location"); }});function handleSuccess(position){ // 获取到当前位置经纬度,WGS坐标系 var lng = position.coords.longitude; var lat = position.coords.latitude; var lct_wgs = new Location(lat,lng);//定义一个坐标对象 var lct_gcj = transformFromWGSToGCJ(lct_wgs);//将WGS-84坐标转换为GCJ-02坐标 var lct_db = bd_encrypt(lct_gcj);//将GCJ坐标转换为 BD-09坐标,即百度坐标 $('#lat').val(lct_db.lat); $('#lng').val(lct_db.lng);}function handleError(error){alert("Failed to get location");}function Location(lat,lng){this.lat=lat;this.lng=lng;}function transformFromWGSToGCJ(wgLoc){var a = 6378245.0;var ee = 0.00669342162296594323;//alert("begin"+wgLoc.lat); var mgLoc = new Location(); if (outOfChina(wgLoc.lat, wgLoc.lng)) { mgLoc = wgLoc; return mgLoc; } var dLat = transformLat(wgLoc.lng - 105.0, wgLoc.lat - 35.0); var dLon = transformLon(wgLoc.lng - 105.0, wgLoc.lat - 35.0); var radLat = wgLoc.lat / 180.0 * Math.PI; var magic = Math.sin(radLat); magic = 1 - ee * magic * magic; var sqrtMagic = Math.sqrt(magic); //alert("sqrtMagic:"+sqrtMagic); dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Math.PI); // alert("dLat:"+dLat); dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math.PI); //alert("dLon:"+dLon); mgLoc.lat = wgLoc.lat + dLat; mgLoc.lng = wgLoc.lng + dLon; //alert("转换为GCJ坐标end"); //alert("GCJ纬度:"+mgLoc.lat); return mgLoc;}function outOfChina(lat,lon){ if (lon < 72.004 || lon > 137.8347) return true; if (lat < 0.8293 || lat > 55.8271) return true; return false;}function transformLat(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 * Math.PI) + 20.0 *Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0; ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0; ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0; return ret;}function transformLon(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 * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0; ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0; ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0; return ret;}////// GCJ-02 坐标转换成 BD-09 坐标///function bd_encrypt(gcLoc){var x_pi = 3.14159265358979324 * 3000.0 / 180.0; var x = gcLoc.lng, y = gcLoc.lat; var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi); var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi); return new Location(z * Math.cos(theta) + 0.0065, z * Math.sin(theta) + 0.006);}////// BD-09 坐标转换成 GCJ-02坐标///function bd_decrypt(bdLoc){var x = bdLoc.lng - 0.0065, y = bdLoc.lat - 0.006;var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);var theta = atan2(y, x) - 0.000003 * Math.cos(x * x_pi); return LocationMake(z * Math.cos(theta), z * Math.sin(theta));}</span>
0 0
- 通过浏览器获取用户地理位置
- 【百度地图api】之获取当前用户地理位置-浏览器定位
- android 通过GPS获取用户地理位置并监听位置变化
- android 通过GPS获取用户地理位置并监听位置变化
- android 通过GPS获取用户地理位置并监听位置变化
- JS获取用户地理位置
- 获取用户地理位置定位
- 通过IP获取地理位置
- 获取用户的地理位置接口
- Android通过基站获取地理位置
- PHP通过IP 获取 地理位置
- 通过html5获取地理位置信息
- php 通过ip获取地理位置
- 通过经纬度获取地理位置信息
- UserAgent:通过浏览器获取用户浏览器和电脑信息
- UserAgent:通过浏览器获取用户浏览器和电脑信息
- 【代码】android 通过GPS获取用户地理位置并监听位置变化
- 借助第三方插件(有道)通过ip获取用户所在的地理位置
- Qt WebKit---实战QWebView--2
- dyld: lazy symbol binding failed: Symbol not found: _OSAtomicIncrement32Barrier
- c#中Split 分离字符以及空格消除方法
- Qt WebKit---实战QWebView—3
- 第9周项目5-程序填充题(1)
- 通过浏览器获取用户地理位置
- javascript 算法笔记-小例
- R:工作空间
- iPhone6分辨率与适配
- Java 模拟单链表
- TCP长连接与短连接的区别
- 黑马程序员--OC--内存管理(一)
- Mybatis中resultMap
- 在MAC OS X中默认的Web共享目录