通过浏览器获取用户地理位置

来源:互联网 发布:网络打国际长途 编辑:程序博客网 时间: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
原创粉丝点击