【问题标题】:Vuejs google map show marker/coordinates inside in the center circleVuejs谷歌地图在中心圈内显示标记/坐标
【发布时间】:2020-08-07 16:55:25
【问题描述】:

如何显示在我的中心半径内的标记?我需要根据半径显示地点,因为这将是我根据地点的公里半径创建查找此类地点的功能的基础。

这是我的地图

如您所见,我有很多标记。通过调用 axios 请求来自我的数据库。

这是我的代码 sn-p

     data() {
         return {
            clinics: [],
            points: '',
            property: {
               lat: 1.28237,
               lng: 103.783098
            },
            diameter: 5000
         }
      },
      methods: {
         initMap() {
            //center marker from circle
            const map = new google.maps.Map(document.getElementById('map'), {
               zoom: 13,
               center: this.property
            })
            const circle = new google.maps.Circle({
               map: map,
               trokeColor: '#FF0000',
               strokeOpacity: 0.8,
               strokeWeight: 2,
               fillColor: '#FF0000',
               fillOpacity: 0.35,
               radius: this.diameter,
               center: this.property
            });
            const marker = new google.maps.Marker({
               position: this.property,
               map: map
            });

            //other Markers
            for (var i = 0; i < this.clinics.data.length; i++) {
               var coords = this.clinics.data[i].coord;
               var details = this.clinics.data[i].clinic;
               var latLng = new google.maps.LatLng(coords['lat'], coords['lng']);
               var markers = new google.maps.Marker({
                  position: latLng,
                  map: map,
               });
               const contentString = '<div id="content"><p>' + details + '</p></div>';
               //for Info window function
               this.infoWindowShow(markers, contentString);
            }
         }

【问题讨论】:

  • 如果您只对圈内的标记感兴趣,为什么要加载所有标记? + 我已经向您指出了有关如何根据中心点和半径查询数据库以查找标记的完整答案。这不适合你吗?
  • 是的,它对我不起作用,先生@MrUpsidown
  • 什么不起作用? 它不起作用 不是问题描述+您上面的代码没有显示任何沿着这条路走的尝试。然而,这将是一个比您接受的解决方案更好的解决方案。

标签: javascript google-maps vue.js google-maps-api-3


【解决方案1】:

您可以在 for 循环中放置一个函数,该函数将检查圆心与标记坐标之间的距离是否小于或等于您在绘制之前设置的半径。它应该看起来像这样。

    for (var i = 0; i < this.clinics.data.length; i++) {
               var coords = this.clinics.data[i].coord;
               var details = this.clinics.data[i].clinic;
               var latLng = new google.maps.LatLng(coords['lat'], coords['lng']);

        //Get distance between the center of the circle and the coordinates
    var dist  = google.maps.geometry.spherical.computeDistanceBetween(CenterOfCircle,latLng);    

    //If distance is less than the radius, plot the markers in the map
    if (dist <= radiusOfCircle){
               var markers = new google.maps.Marker({
                  position: latLng,
                  map: map,
               });
               const contentString = '<div id="content"><p>' + details + '</p></div>';
               //for Info window function
               this.infoWindowShow(markers, contentString);
            }
  }

您可以使用computeDistanceBetween() of the Geometry library。确保在调用 Google Maps API 的脚本中添加“libraries=geometry”。

这是一个简单的代码,只是looping in the set of coordinates,这是对简单代码的修改,只是plotting only the markers inside the circle

希望这适用于您的实现!

【讨论】:

  • 您好先生,我修改了您的代码以及为什么我得到这样的Uncaught TypeError: a.lat is not a function
  • 该错误意味着您传递的值不是坐标。您传递的值可能是一个字符串。您需要在函数中提供两个 google.maps.LatLng 对象。你检查过你经过的坐标吗?
  • 我用过这个var latLng = new google.maps.LatLng(coords['lat'], coords['lng']);
  • 我可以在这个fiddle 中复制错误。当我传递一个未由“new google.maps.LatLng()”声明的坐标时,就会发生这种情况。
  • 我们可以继续聊天吗?看来我没有错误,但我只能看到我的中心标记和圆圈,现在没有显示任何标记。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 2014-03-16
  • 1970-01-01
  • 2020-06-08
  • 1970-01-01
相关资源
最近更新 更多