• <bdo id='qkTHj'></bdo><ul id='qkTHj'></ul>
  • <small id='qkTHj'></small><noframes id='qkTHj'>

  • <legend id='qkTHj'><style id='qkTHj'><dir id='qkTHj'><q id='qkTHj'></q></dir></style></legend>
      <i id='qkTHj'><tr id='qkTHj'><dt id='qkTHj'><q id='qkTHj'><span id='qkTHj'><b id='qkTHj'><form id='qkTHj'><ins id='qkTHj'></ins><ul id='qkTHj'></ul><sub id='qkTHj'></sub></form><legend id='qkTHj'></legend><bdo id='qkTHj'><pre id='qkTHj'><center id='qkTHj'></center></pre></bdo></b><th id='qkTHj'></th></span></q></dt></tr></i><div id='qkTHj'><tfoot id='qkTHj'></tfoot><dl id='qkTHj'><fieldset id='qkTHj'></fieldset></dl></div>
        <tfoot id='qkTHj'></tfoot>
      1. 地理位置离我的位置最近的位置(纬度,经度)

        时间:2023-09-06

      2. <small id='drtq2'></small><noframes id='drtq2'>

          <legend id='drtq2'><style id='drtq2'><dir id='drtq2'><q id='drtq2'></q></dir></style></legend>
            <tbody id='drtq2'></tbody>
            • <bdo id='drtq2'></bdo><ul id='drtq2'></ul>
              <i id='drtq2'><tr id='drtq2'><dt id='drtq2'><q id='drtq2'><span id='drtq2'><b id='drtq2'><form id='drtq2'><ins id='drtq2'></ins><ul id='drtq2'></ul><sub id='drtq2'></sub></form><legend id='drtq2'></legend><bdo id='drtq2'><pre id='drtq2'><center id='drtq2'></center></pre></bdo></b><th id='drtq2'></th></span></q></dt></tr></i><div id='drtq2'><tfoot id='drtq2'></tfoot><dl id='drtq2'><fieldset id='drtq2'></fieldset></dl></div>
              1. <tfoot id='drtq2'></tfoot>
                • 本文介绍了地理位置离我的位置最近的位置(纬度,经度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想根据我所在的位置显示特定信息.

                  I want to show specific information depending on where i am.

                  我有五个信息不同的城市,我想显示我最接近的城市(信息).

                  I have five cities with different information, and i want to show that city(information) that i'm closest to.

                  我如何用最简单的方式做到这一点,使用 javascript.

                  How to i do that the simplest way, using javascript.

                  例如

                  如果我将城市经纬度存储在一个数组中

                  If i store the cities lat, long in an array

                  var cities = [
                    ['new york', '111111', '222222', 'blablabla']
                    ['boston', '111111', '222222', 'blablabla']
                    ['seattle', '111111', '222222', 'blablabla']
                    ['london', '111111', '222222', 'blablabla']
                  ]
                  

                  并且以我当前的位置(纬度,经度),我想要离我最近的城市.

                  And with my current location(lat, long) i want the city that i'm closet to.

                  推荐答案

                  这是一个使用 HTML5 地理定位来获取用户位置的基本代码示例.然后它调用 NearestCity() 并计算从该位置到每个城市的距离(公里).我继续使用 Haversine 公式,转而使用更简单的毕达哥拉斯公式和 equirectangular 投影来调整经度线的曲率.

                  Here is a basic code example using HTML5 geolocation to get the user's position. It then calls NearestCity() and calculates the distance (km) from the location to each city. I passed on using the Haversine formulae and instead used the simpler Pythagoras formulae and an equirectangular projection to adjust for the curvature in longitude lines.

                  // Get User's Coordinate from their Browser
                  window.onload = function() {
                    // HTML5/W3C Geolocation
                    if (navigator.geolocation) {
                      navigator.geolocation.getCurrentPosition(UserLocation);
                    }
                    // Default to Washington, DC
                    else
                      NearestCity(38.8951, -77.0367);
                  }
                  
                  // Callback function for asynchronous call to HTML5 geolocation
                  function UserLocation(position) {
                    NearestCity(position.coords.latitude, position.coords.longitude);
                  }
                  
                  
                  // Convert Degress to Radians
                  function Deg2Rad(deg) {
                    return deg * Math.PI / 180;
                  }
                  
                  function PythagorasEquirectangular(lat1, lon1, lat2, lon2) {
                    lat1 = Deg2Rad(lat1);
                    lat2 = Deg2Rad(lat2);
                    lon1 = Deg2Rad(lon1);
                    lon2 = Deg2Rad(lon2);
                    var R = 6371; // km
                    var x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
                    var y = (lat2 - lat1);
                    var d = Math.sqrt(x * x + y * y) * R;
                    return d;
                  }
                  
                  var lat = 20; // user's latitude
                  var lon = 40; // user's longitude
                  
                  var cities = [
                    ["city1", 10, 50, "blah"],
                    ["city2", 40, 60, "blah"],
                    ["city3", 25, 10, "blah"],
                    ["city4", 5, 80, "blah"]
                  ];
                  
                  function NearestCity(latitude, longitude) {
                    var minDif = 99999;
                    var closest;
                  
                    for (index = 0; index < cities.length; ++index) {
                      var dif = PythagorasEquirectangular(latitude, longitude, cities[index][1], cities[index][2]);
                      if (dif < minDif) {
                        closest = index;
                        minDif = dif;
                      }
                    }
                  
                    // echo the nearest city
                    alert(cities[closest]);
                  }
                  

                  这篇关于地理位置离我的位置最近的位置(纬度,经度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:google.loader.clientlocation 是否仍受支持 下一篇:HTML5 地理位置如何工作?

                  相关文章

                  最新文章

                  1. <small id='GxDgg'></small><noframes id='GxDgg'>

                    <legend id='GxDgg'><style id='GxDgg'><dir id='GxDgg'><q id='GxDgg'></q></dir></style></legend>

                    <tfoot id='GxDgg'></tfoot>

                    <i id='GxDgg'><tr id='GxDgg'><dt id='GxDgg'><q id='GxDgg'><span id='GxDgg'><b id='GxDgg'><form id='GxDgg'><ins id='GxDgg'></ins><ul id='GxDgg'></ul><sub id='GxDgg'></sub></form><legend id='GxDgg'></legend><bdo id='GxDgg'><pre id='GxDgg'><center id='GxDgg'></center></pre></bdo></b><th id='GxDgg'></th></span></q></dt></tr></i><div id='GxDgg'><tfoot id='GxDgg'></tfoot><dl id='GxDgg'><fieldset id='GxDgg'></fieldset></dl></div>
                      • <bdo id='GxDgg'></bdo><ul id='GxDgg'></ul>