Question

I have several thousand locations stored in CoreData and I would like to search for locations that are within a Google Maps visibleRegion. I was previously doing a search with a bounding box but the addition of the bearing feature breaks this type of query. I have several ideas but this must be a common problem with some well thought out solutions. I'd be interested to see if any solutions use geohashes.

This is my query that breaks when the bearing is not due north.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(lat > %f AND lat < %f AND lng > %f AND lng < %f)",
                                    [self.googleMap.projection visibleRegion].nearLeft.latitude,
                                    [self.googleMap.projection visibleRegion].farLeft.latitude,
                                    [self.googleMap.projection visibleRegion].nearLeft.longitude,
                                    [self.googleMap.projection visibleRegion].nearRight.longitude
                               ];
Était-ce utile?

La solution

You can calculate an axis-aligned bounding box of the visible region, and then use that to look up your locations. Some of them will still be outside of the actual visible area, but at least you'll filter out most of them.

The GMSCoordinateBounds class in GMSCoordinateBounds.h can be used to make this easier:

GMSMapView* _mapView = ...;

GMSCoordinateBounds* bounds = 
    [[GMSCoordinateBounds alloc] 
        initWithRegion: [_mapView.projection visibleRegion]];
CLLocationCoordinate2D northEast = bounds.northEast;
CLLocationCoordinate2D southWest = bounds.southWest;

Note also that there is currently a bug with visibleRegion being too large, see here:

https://code.google.com/p/gmaps-api-issues/issues/detail?id=5107

See here for a workaround to that problem:

Google Maps iOS SDK: How do I get accurate latitude and longitude coordinates from a camera's visibleRegion?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top