javascript 算 经纬度中心点

javascript 算 经纬度中心点,第1张

eastPoint(ex,ey);东 southPoint(sx,sy);南 westPoint(wx,wy);西 northPint(nx,ny);北 中心点坐标 centerPoint((ex+sx)/2,(wy+ny)/2);

数据库存取的是经纬度以及坐标的相关信息,读取后根据百度地图api调用显示。 示例如下 // 百度地图API功能 var map = new BMapMap("allmap"); var point = new BMapPoint(116417854,39921988);

需要引入jquery!!

HTML:

<div>

    <input id="geohash" type="text" placeholder="GeoHash" maxlength="12">

    <br><br>

    <input id="coordinate" type="text" placeholder="Lat, Lng" maxlength="40">

    <br><br>

    <input id="precision" type="number" min="1" max="12" placeholder="Precision">

  </div>

js代码-执行代码:

$('#geohash')keyup(function() {

    var coordinate = decodeGeoHash(thisvalue);

    if (!coordinate || !coordinatelatitude[2] || !coordinatelongitude[2])

    {

        $('#coordinate')val('');

        $('#precision')val('');

    }

    else

    {

        $('#coordinate')val(coordinatelatitude[2]toFixed(8) + ", " + coordinatelongitude[2]toFixed(8));

        $('#precision')val(thisvaluelength);

    }

  });

  function validatePrecision()

  {

      var precision = $('#precision')val();      

      if (!precision)

      {

          return '';

      }

      else if (!$isNumeric(precision))

      {

        return 1;

      }

      else if (precision > 12)

      {

        return 12;

      }

      else if (precision < 1)

      {

        return 1;

      }

      return precision;

  }

  function toGeohash()

  {

    var precision = $('#precision')val();

    if (!$isNumeric(precision))

    {

      precision = 12;

    }

    var coordinate = $('#coordinate')val()split(",");

    var latlng = coordinate;

    if (latlnglength >= 2)

    {

        var lat = latlng[0]trim();

        var lng = latlng[1]trim();

        if (/^(\-\d+(\\d+))$/test(lat) && /^(\-\d+(\\d+))$/test(lng))

        {

          consolelog(precision);

            return encodeGeoHash(lat, lng, precision);

        }

    }

    return '';

  }

  $('#coordinate')keyup(function() {

    $('#geohash')val(toGeohash());

  });

  $('#precision')change(function() {

    $('#precision')val(validatePrecision());

    $('#geohash')val(toGeohash());

  });

  $('#precision')keyup(function() {

    $('#precision')val(validatePrecision());

    $('#geohash')val(toGeohash());

  });

js代码-geohashjs

// geohashjs

// Geohash library for Javascript

// (c) 2008 David Troy

// Distributed under the MIT License

BITS = [16, 8, 4, 2, 1];

BASE32 =     "0123456789bcdefghjkmnpqrstuvwxyz";

NEIGHBORS = { right  : { even :  "bc01fg45238967deuvhjyznpkmstqrwx" },

left   : { even :  "238967debc01fg45kmstqrwxuvhjyznp" },

top    : { even :  "p0r21436x8zb9dcf5h7kjnmqesgutwvy" },

bottom : { even :  "14365h7k9dcfesgujnmqp0r2twvyx8zb" } };

BORDERS   = { right  : { even : "bcfguvyz" },

left   : { even : "0145hjnp" },

top    : { even : "prxz" },

bottom : { even : "028b" } };

NEIGHBORSbottomodd = NEIGHBORSlefteven;

NEIGHBORStopodd = NEIGHBORSrighteven;

NEIGHBORSleftodd = NEIGHBORSbottomeven;

NEIGHBORSrightodd = NEIGHBORStopeven;

BORDERSbottomodd = BORDERSlefteven;

BORDERStopodd = BORDERSrighteven;

BORDERSleftodd = BORDERSbottomeven;

BORDERSrightodd = BORDERStopeven;

function refine_interval(interval, cd, mask) {

if (cd&mask)

interval[0] = (interval[0] + interval[1])/2;

  else

interval[1] = (interval[0] + interval[1])/2;

}

function calculateAdjacent(srcHash, dir) {

srcHash = srcHashtoLowerCase();

var lastChr = srcHashcharAt(srcHashlength-1);

var type = (srcHashlength % 2)  'odd' : 'even';

var base = srcHashsubstring(0,srcHashlength-1);

if (BORDERS[dir][type]indexOf(lastChr)!=-1)

base = calculateAdjacent(base, dir);

return base + BASE32[NEIGHBORS[dir][type]indexOf(lastChr)];

}

function decodeGeoHash(geohash) {

var is_even = 1;

var lat = []; var lon = [];

lat[0] = -900;  lat[1] = 900;

lon[0] = -1800; lon[1] = 1800;

lat_err = 900;  lon_err = 1800;

for (i=0; i<geohashlength; i++) {

c = geohash[i];

cd = BASE32indexOf(c);

for (j=0; j<5; j++) {

mask = BITS[j];

if (is_even) {

lon_err /= 2;

refine_interval(lon, cd, mask);

} else {

lat_err /= 2;

refine_interval(lat, cd, mask);

}

is_even = !is_even;

}

}

lat[2] = (lat[0] + lat[1])/2;

lon[2] = (lon[0] + lon[1])/2;

return { latitude: lat, longitude: lon};

}

function encodeGeoHash(latitude, longitude, precision) {

var is_even=1;

var i=0;

var lat = []; var lon = [];

var bit=0;

var ch=0;

geohash = "";

lat[0] = -900;  lat[1] = 900;

lon[0] = -1800; lon[1] = 1800;

while (geohashlength < precision) {

  if (is_even) {

mid = (lon[0] + lon[1]) / 2;

    if (longitude > mid) {

ch |= BITS[bit];

lon[0] = mid;

    } else

lon[1] = mid;

  } else {

mid = (lat[0] + lat[1]) / 2;

    if (latitude > mid) {

ch |= BITS[bit];

lat[0] = mid;

    } else

lat[1] = mid;

  }

is_even = !is_even;

  if (bit < 4)

bit++;

  else {

geohash += BASE32[ch];

bit = 0;

ch = 0;

  }

}

return geohash;

}

网页获取用户位置信息的办法

1 调用百度地图的地图标注功能,通过百度地图API获取对应的经度和纬度进而获取地区信息

优点是比较准确,缺点是需要用户自己选择位置

2 通过H5 geolocation属性获取经度和纬度,优点是用户只需要点击允许获取即可,缺点是浏览器获取的经度相对不是很准确而且仍旧需要用户确认。

3 通过用户IP来分析用户位置

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8"/>

    <title>基于浏览器的HTML5查找地理位置</title>

    <!-- 百度API -->

    <script src=">

给覆盖物添加拖拽时间,然后在事件的回调函数里就能取到经纬度了

比如给地图本身添加拖拽事件,拖拽结束后再浏览器控制台打印拖拽后的经纬度的实例如下:

var map = new BMapMap('map');

var centerpoint=new BMapPoint(121491, 31233);

mapcenterAndZoom(centerpoint, 15);

mapaddEventListener("dragend", function(e){

consoleinfo(epoint);

});

以上就是关于javascript 算 经纬度中心点全部的内容,包括:javascript 算 经纬度中心点、javascript怎样根据经纬度在百度地图上显示地点、在JavaScript怎么把经纬度转换成geohash等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9586884.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存