Google Maps for iOS - How can you tell if a marker is within the bounds of the screen?

后端 未结 5 1394
一向
一向 2020-12-31 15:11

I\'m trying to figure out a straightforward way to determine in Google Maps for iOS if a given GMSMarker is within the bounds of the visible map. There seems to be solution

相关标签:
5条回答
  • 2020-12-31 15:52

    A code example based on Andy's helpful response:

    - (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];
        }
    }
    
    0 讨论(0)
  • 2020-12-31 15:54

    The swift 4 version of the answer. Returning a boolean if marker is within the screen region or not

    func isMarkerWithinScreen(marker: GMSMarker) -> Bool {
        let region = self.mapView.projection.visibleRegion()
        let bounds = GMSCoordinateBounds(region: region)
        return bounds.contains(marker.position)
    }
    
    0 讨论(0)
  • 2020-12-31 15:56

    Hope this code may help to code hunters.

    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];
       }
    }];
    
    0 讨论(0)
  • 2020-12-31 16:10

    Retrieve the bounds of your viewport with GMSVisibleRegion and create a GMSCoordinateBounds with it. Call containsCoordinate, passing in the marker's position. It should return true if the marker is within the viewport and false if not.

    0 讨论(0)
  • 2020-12-31 16:11

    I have written on method to find GMSMarker is in particular frame. Set your rectangle frame (x,y,maxX,maxY). You can set any frame from screen it tell find marker is in that frame or not..

    - (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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题