【问题标题】:Google Maps for iOS - How can you tell if a marker is within the bounds of the screen?iOS 版 Google 地图 - 如何判断标记是否在屏幕范围内?
【发布时间】:2019-10-04 01:08:20
【问题描述】:

我试图找出一种简单的方法来确定给定的 GMSMarker 是否在可见地图的范围内。 Javascript API 中似乎有解决方案,但除了可能基于this post 进行一些复杂的推理之外,似乎没有办法。

【问题讨论】:

    标签: ios google-maps


    【解决方案1】:

    基于 Andy 的有用回复的代码示例:

    - (void)snapToMarkerIfItIsOutsideViewport:(GMSMarker *)m{
        GMSVisibleRegion region = _mapView.projection.visibleRegion;
        GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithRegion:region];
        if (![bounds containsCoordinate:m.position]){
            GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:m.position.latitude
                                                  longitude:m.position.longitude
                                                       zoom:_mapView.camera.zoom];
            [self.mapView animateToCameraPosition: camera];
        }
    }
    

    【讨论】:

    • 非常感谢!它完美无缺!节省了我很多时间
    • 如果我的标记一半在区域内,一半在区域外,它会返回什么?实际上我想检查我的标记是否完全在该区域内。我该如何检查?
    • 好吧,严格来说,你的标记是一个点,所以它没有尺寸。如果您的目标是确保与标记关联的图形图像完全可见,我建议在此条件下进行迭代,缩小直到满足,然后再缩小一级。
    【解决方案2】:

    使用GMSVisibleRegion 检索视口的边界并用它创建一个GMSCoordinateBounds。调用containsCoordinate,传入标记的位置。如果标记在视口内,则返回 true,否则返回 false。

    【讨论】:

      【解决方案3】:

      答案的 swift 4 版本。如果标记在屏幕区域内,则返回布尔值

      func isMarkerWithinScreen(marker: GMSMarker) -> Bool {
          let region = self.mapView.projection.visibleRegion()
          let bounds = GMSCoordinateBounds(region: region)
          return bounds.contains(marker.position)
      }
      

      【讨论】:

        【解决方案4】:

        我已经写了找到 GMSMarker 是特定框架的方法。 设置您的矩形框架 (x,y,maxX,maxY)。您可以从屏幕上设置任何帧,它告诉查找标记是否在该帧中..

        - (BOOL)isGoogleMapMarkerVisible:(GMSMarker*)marker {
        
            //Marker point
            CGPoint markerpoint = [self.mapview.projection pointForCoordinate:marker.position];
        
            //Maximum visible region from x and y axis
            float x = 0.0;
            float y = o.o;
            float maxX = self.mapview.frame.size.width;
            float maxY = self.mapview.frame.size.height;
        
            //If marker point is on visible region return true else return false
            if (markerpoint.x > x && markerpoint.y > y && markerpoint.x < maxX && markerpoint.y < maxY) {
                return YES;
            }
            else {
                    return NO;
            }
        }
        

        【讨论】:

          【解决方案5】:

          希望此代码对代码猎人有所帮助。

          NSMutableArray *mutArrMarkers; //Have all markers added on Map
          .
          .
          .
          .
          
          NSMutableArray *mutArrMarkersInPath = [NSMutableArray array];
          [mutArrMarkers enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
             GMSMarker *marker = obj;
             if(GMSGeometryContainsLocation(currentCoordinates, pathToCheck, YES)){
                  [mutArrMarkersInPath addObject:marker];
             }
          }];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多