【问题标题】:Get coordinates, measure distances, and compare them获取坐标、测量距离并进行比较
【发布时间】:2018-03-30 08:56:01
【问题描述】:

建议开发一个页面,您可以在其中展示 Google 地图的摘录,其中有几个标记在集群中组织(完成)。我没有使用 javascript 的经验,我需要的是无论是否单击按钮,都可以获得我的位置并自动获知地图上标记的哪个点离我更近。有人能帮我吗?

      function initMap() {

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 13,
          center: {lat: 40.963308, lng: -8.594651}
        });

        // Create an array of alphabetical characters used to label the markers.
        var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

        // Add some markers to the map.
        // Note: The code uses the JavaScript Array.prototype.map() method to
        // create an array of markers based on a given "locations" array.
        // The map() method here has nothing to do with the Google Maps API.
        var markers = locations.map(function(location, i) {
          return new google.maps.Marker({
            position: location,
            label: labels[i % labels.length]
          });
        });

        // Add a marker clusterer to manage the markers.
        var markerCluster = new MarkerClusterer(map, markers,
            {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }
      var locations = [
        {lat:  40.962157, lng: -8.593313},
        {lat:  40.962149, lng: -8.595695},
        {lat:  40.960351, lng: -8.598922},   
        {lat:  40.967305, lng: -8.591450},
        {lat:  40.961682, lng: -8.608136}
      ]

    </script>

【问题讨论】:

标签: javascript google-maps geolocation coordinates distance


【解决方案1】:

如果您谈论的是空中距离,请使用欧几里得公式,非常快,考虑到一切。稍微修改一下,就不用平方根了,因为它是一个昂贵的操作。

(Xpos - Xpoint)^2 + (Ypos - Ypoint)^2

应用基本最小逻辑,即始终保存最低距离点。

这是最简单的方法,如果地点很远,通常也是最近的点。



以前做道路距离的方法是一个一个地做,但幸好谷歌想到了这个案例并开发了https://developers.google.com/maps/documentation/distance-matrix/intro

我不确定您帐户的使用配额是多少,但这取决于您和您的公司根据您想要的计划类型来确定。

【讨论】:

    猜你喜欢
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 2023-03-08
    • 2018-04-01
    • 2017-10-06
    相关资源
    最近更新 更多