【问题标题】:Show users coordinates and trigger event显示用户坐标和触发事件
【发布时间】:2013-10-31 14:56:26
【问题描述】:

我正在使用 Google Maps Utilities Library V3 中的地理位置标记脚本来显示用户的位置。

我想要实现的(我是 Google Maps API 的新手!)是:

  • 显示用户当前坐标(例如,在页面某处的简单 CSS 容器中)

  • 将事件连接到标记。我应该在用户关闭时触发。

感谢您的帮助!

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 geolocation


    【解决方案1】:

    要向用户显示坐标,您需要对 DOM 元素的引用。那么更新内容就很简单了。

    页面上的 HTML

    <div id="UserCoordinates">Waiting on GPS Position ...</div>
    

    在脚本中

    google.maps.event.addListener(GeoMarker, 'position_changed', function() {
      var UserPosition = this.getPosition();
      var DisplayElement = document.getElementById('UserCoordinates');
      if(UserPosition === null) {
        DisplayElement.innerHTML = 'Waiting on GPS Position...';
      } else {
        DisplayElement.innerHTML =
            'Current Position: ' + UserPosition.toUrlValue();
      }
    });
    

    这将在用户更改时向用户显示他们当前的位置。如果您要继续使用全屏地图,您可能希望将UserCoordinates div 实现为地图控件。 API Reference 对此有很好的概述和多个示例。

    当用户在某个位置 X 米范围内时显示信息窗口

    这有点棘手,因为要处理多种情况,并且您不希望信息窗口在您的半径范围内移动时重复打开。

    距离计算

    我看到您的代码中有一个距离函数,但我建议使用 API 的 Spherical Geometry library 中的一个。您只需要使用您的 api 脚本标签专门加载库:

    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=true_or_false">
    </script>
    

    然后你需要添加到position_changed事件处理器:

    var IsWithinRadius = false; //assume they start outside of the circle
    var RadiusInMeters = 1000; //within 1 km
    var LocationOfInterest = map.getCenter();
    google.maps.event.addListener(GeoMarker, 'position_changed', function() {
      var UserPosition = this.getPosition();
      var DisplayElement = document.getElementById('UserCoordinates');
      if(UserPosition === null) {
        DisplayElement.innerHTML = 'Waiting on GPS Position...';
        IsWithinRadius = false; //you don't know where they are
      } else {
        DisplayElement.innerHTML =
            'Current Position: ' + UserPosition.toUrlValue();
        var IsCurrentPositionInRadius =
            Math.abs(google.maps.geometry.spherical.computeDistanceBetween(
                UserPosition, LocationOfInterest)) <= RadiusInMeters;
        var JustEnteredRadius = !IsWithinRadius && IsCurrentPositionInRadius;
        IsWithinRadius = IsCurrentPositionInRadius;
        if(JustEnteredRadius) {
          //trigger action here.
          alert("Within raidus");
        }
      }
    });
    

    【讨论】:

    • 太棒了,乍得!非常感谢。但是我还有一件事我不明白。如何将您的“半径内”脚本应用于多个标记,例如: var marker1 = new google.maps.Marker({ position: new google.maps.LatLng(49.455,11.075), map: map, draggable: false });说,我希望 event1 在用户在 marker1 周围 100 米的半径内时触发,如果在 marker2 周围 100 米内,则 event2 ...
    • 我正在尝试为包含多个地点和相关事件的孩子进行寻宝......
    • 假设您有一个位置数组,您只需在事件中循环遍历该数组并进行类似检查。 IsWithinRaidusLocationOfInterest 变量都需要是特定于每个位置的属性。
    • 这很有意义,但我不得不承认我不知道该怎么做...我在这里尝试了多个自定义标记的脚本link,但无法做到工作。即使我可以,我仍然不知道如何遍历这些位置并检查用户是否靠近...帮助!
    • 您应该为此提出一个新问题。确保发布您已经尝试过的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多