【问题标题】:How to get square meter or square km using in html5 / ionic如何在 html5 / ionic 中使用平方米或平方公里
【发布时间】:2018-11-05 04:31:15
【问题描述】:

这就是我的想法。我想使用 GPS 获取用户位置。我想计算平方公里或米,而不是发送当前位置。

----------------------------------
|                                |
|                                |
|                                | 
|                                |
|              .                 |
|              ^                 |
|      My current position       |
|                                |
|                                |
|                                |
----------------------------------

如上图所示,我当前的位置,但我想在 HTML5 或 Ionic 中计算周围的整个区域会更受欢迎。

更新

在上图中,红点是我的位置,我需要将整个区域置于红色矩形中。获取该存储在数据库中。我研究了多边形面积公式,但这需要几个顶点,而地理位置我只能得到经度和纬度两个坐标。我将如何使用这两点来做到这一点?

更新

我找到了一个解决方案here,但这个解决方案是持续跟踪用户的当前位置,在calculateDistance 函数中,公式同时使用当前位置(经度和纬度)和跟踪位置(经度和纬度),即Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * Math.sin(dLon/2) * Math.sin(dLon/2);

这对于我的第二种情况非常有效,但首先我不想首先跟踪用户的位置。首先,我简单地获取当前用户位置(经度和纬度),从中计算面积并将其发送到服务器。现在我不确定我将如何实现这一目标。有什么帮助吗?

【问题讨论】:

  • 问题是该区域不是矩形。地球是一个球体,因此您必须自己使用图书馆或do the math
  • 尝试找出地理围栏,我认为这会有所帮助。
  • 您希望您的数据是什么样的?你想计算那个区域的平方公里吗?你想要角落的经纬度坐标吗?请为您的问题提供所需的输出
  • @Ivaro18 我不想绘制任何矩形,我只想获取用户的当前位置,然后找到它的多边形/区域,例如 1000 平方米并将其作为 json 发送到我所在的服务器将存储。稍后当用户使用时,我将根据 GPS 跟踪用户位置,当他们进入该范围时,我将向他们显示您在该范围内的消息。就是这样。
  • 好吧,如果你有 lat-lng 的中点,你不能只计算 1km 的 lat 和 long 值,然后加上和减去它们来创建多边形的角吗?

标签: javascript html ionic-framework geolocation gps


【解决方案1】:

我在谷歌地图中创建了一个圆圈,然后得到了他的边界,它返回一个方形边界框。

function getBounds(latLng, radius) {
  /*
  * Params:
  *     latLng: google maps LatLng object or object literal  // example: { lat: -34.397, lng: 150.644 }
  *     radius: Number(int) in Miters. For example 1000 will be 1 sq Kilometer
  * Returns:
  *     object literal in the following format:
  *         {
                northEast: {
                  lat: (float),
                  lng: (float),
                },
                southWest: {
                  lat: (float),
                  lng: (float),
                },
            }
  * */

  const circle = new google.maps.Circle({
    center: latLng,
    radius: radius
  });
  const bounds = circle.getBounds();
  const northEast = bounds.getNorthEast();
  const southWest = bounds.getSouthWest();
  return {
    northEast: {
      lat: northEast.lat(),
      lng: northEast.lng(),
    },
    southWest: {
      lat: southWest.lat(),
      lng: southWest.lng(),
    },
  };
}

我使用图书馆为您添加了一个简短的 sn-p,看看这是否是您正在寻找的正确答案。 单击地图后,您将看到一个标记及其周围的三角形。

注意:请复制代码并添加您的谷歌地图 API 密钥。 没有您的 API 密钥,地图将无法加载。

let map;
let marker;
let rectangle;

function initMap() {
  const latLng = {
    lat: -34.397,
    lng: 150.644
  };
  const radius = 1000;
  map = new google.maps.Map(document.getElementById('map'), {
    center: latLng,
    zoom: 11,
  });
  google.maps.event.addListener(map, 'click', function(event) {
    const location = event.latLng;
    const bounds = getBounds(location, 1000);
    console.log(bounds);
    addRectangle(bounds);
    addMarker(location);
  });
}

function addMarker(location) {
  if (marker) {
    marker.setMap(null);
  }
  marker = new google.maps.Marker({
    position: location,
    map: map,
  });
}

function addRectangle(bounds) {
  if (rectangle) {
    rectangle.setMap(null);
  }
  rectangle = new google.maps.Rectangle({
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    map: map,
    bounds: {
      north: bounds.northEast.lat,
      south: bounds.southWest.lat,
      east: bounds.northEast.lng,
      west: bounds.southWest.lng,
    },
  });
}

function getBounds(latLng, radius) {
  /*
  * Params:
  *     latLng: google maps LatLng object or object literal  // example: { lat: -34.397, lng: 150.644 }
  *     radius: Number(int) in Miters. For example 1000 will be 1 sq Kilometer
  * Returns:
  *     object literal in the following format:
  *         {
                northEast: {
                  lat: (float),
                  lng: (float),
                },
                southWest: {
                  lat: (float),
                  lng: (float),
                },
            }
  * */

  const circle = new google.maps.Circle({
    center: latLng,
    radius: radius
  });
  const bounds = circle.getBounds();
  const northEast = bounds.getNorthEast();
  const southWest = bounds.getSouthWest();
  return {
    northEast: {
      lat: northEast.lat(),
      lng: northEast.lng(),
    },
    southWest: {
      lat: southWest.lat(),
      lng: southWest.lng(),
    },
  };
}
/* Always set the map height explicitly to define the size of the div
         * element that contains the map. */

#map {
  height: 100%;
}


/* Optional: Makes the sample page fill the window. */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>
<div id="map"></div>

【讨论】:

  • 感谢您的回答,我会试一试,但是当我尝试获取 google maps API 密钥时,它要求我提供信用卡详细信息。没有详细信息,我无法继续进行:(。没有谷歌地图 API,还有其他解决方案吗?
  • 我会尝试另一种解决方案。您是否在客户端中使用任何地图引擎(如谷歌地图或传单)?
  • 不,我还没有使用任何地图,但我的计划是使用谷歌地图,由于我的上述回复,现在似乎很难。
  • 我只是看了看感觉不错的传单。也许我会试一试。
猜你喜欢
  • 2018-01-06
  • 2015-09-07
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
相关资源
最近更新 更多