How to convert MKCoordinateRegion to a CBLGeoRect?

45 views
Skip to first unread message

Brendan Duddridge

unread,
Oct 21, 2015, 3:57:13 AM10/21/15
to Couchbase Mobile
Hi,

I have an MKMapView which is providing me with the displayed region in the format of an MKCoordinateRegion.

The structure looks like this:

typedef struct {

    CLLocationDegrees latitudeDelta;

    CLLocationDegrees longitudeDelta;

} MKCoordinateSpan;


typedef struct {

    CLLocationCoordinate2D center;

    MKCoordinateSpan span;

} MKCoordinateRegion;



Does anyone know how I can convert that to a CBLGeoRect?


Thanks,


Brendan


Jens Alfke

unread,
Oct 21, 2015, 11:51:28 AM10/21/15
to mobile-c...@googlegroups.com
Looks like simple addition & subtraction? Or am I missing something?
I mean, the MK struct gives you the center point and dimensions of the rect. A CBLGeoRect contains the min and max coords.

—Jens

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/49442b61-7c86-4edb-b8a8-9167a17b1ecc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brendan Duddridge

unread,
Oct 21, 2015, 7:12:16 PM10/21/15
to Couchbase Mobile
In CBLGeoRect, there's min and max.

Does x in the CBLGeoPoint structs correspond to longitude and y is latitude?

And does min correspond to the North West and max to the South East?

I'm not quite sure I'm emitting my coordinates properly in my map function.

Here's my map function emit:

emit(CBLGeoPointKey([locationData[@"lat"] doubleValue], [locationData[@"lon"] doubleValue]),

nil);


I'm assuming it's latitude first and then longitude.

In the SQLite database there's x0, x1, y0, y1. Can I assume that x0,y0 corresponds to the min coordinates and x1, y1 the max coordinates?


Thanks,

Brendan

Brendan Duddridge

unread,
Oct 22, 2015, 2:58:06 AM10/22/15
to Couchbase Mobile
I finally got it working tonight the way it should.

I switched my map function to emit longitude first and then latitude:

emit(CBLGeoPointKey([locationData[@"lon"] doubleValue], [locationData[@"lat"] doubleValue]),

nil);


And my routine to convert from my MKCoordinateRegion to a CBLGeoRect is this:


- (CBLGeoRect)geoRectForCoordinateRegion:(MKCoordinateRegion)region {

CLLocationCoordinate2D bottomLeftCoordinate =

CLLocationCoordinate2DMake(region.center.longitude - (region.span.longitudeDelta/2.0),

                                    region.center.latitude - (region.span.latitudeDelta/2.0));

CLLocationCoordinate2D topRightCoordinate =

CLLocationCoordinate2DMake(region.center.longitude + (region.span.longitudeDelta/2.0),

    region.center.latitude + (region.span.latitudeDelta/2.0));

CBLGeoPoint minPoint = (CBLGeoPoint){bottomLeftCoordinate.latitude, bottomLeftCoordinate.longitude};

CBLGeoPoint maxPoint = (CBLGeoPoint){topRightCoordinate.latitude, topRightCoordinate.longitude};

CBLGeoRect bbox = (CBLGeoRect){minPoint, maxPoint};


TFFLog(@"bbox 2: min.x: %f, min.y: %f, max.x: %f, max.y: %f", minPoint.x, minPoint.y, maxPoint.x, maxPoint.y);


return bbox;

}



I'm getting records now that match with this configuration. So unless this is completely wrong, I'll stick with it because I'm getting results at least.

Thanks,

Brendan
Reply all
Reply to author
Forward
0 new messages