Monday, November 9, 2009

MKMapView Category

I've been working with MapKit quite a bit the last few days as I finish the chapter on MapKit. I was surprised to find that the map view didn't have a method for telling you whether specific coordinates were currently being shown on screen. The map view has a method to tell you whether the user's current location is visible, but I couldn't find one to that indicated if any arbitrary coordinates were currently visible on screen.

This category rectifies that.

MKMapView-CoordsDisplay.h
#import <MapKit/MapKit.h>

@interface MKMapView(CoordsDisplay)
- (BOOL)coordinatesInRegion:(CLLocationCoordinate2D)coords;
@end


MKMapView-CoordsDisplay.m
#import "MKMapView-CoordsDisplay.h"

@implementation MKMapView(CoordsDisplay)
- (BOOL)coordinatesInRegion:(CLLocationCoordinate2D)coords
{
CLLocationDegrees leftDegrees = self.region.center.longitude - (self.region.span.longitudeDelta / 2.0);
CLLocationDegrees rightDegrees = self.region.center.longitude + (self.region.span.longitudeDelta / 2.0);
CLLocationDegrees bottomDegrees = self.region.center.latitude - (self.region.span.latitudeDelta / 2.0);
CLLocationDegrees topDegrees = self.region.center.latitude + (self.region.span.latitudeDelta / 2.0);

return leftDegrees <= coords.longitude && coords.longitude <= rightDegrees && bottomDegrees <= coords.latitude && coords.latitude <= topDegrees;
}

@end





8 comments:

Slava said...

Thanks. that's really useful several times.

高山洋一 said...

I am looking for a sample code for the MapView delegate methods to manage annotations using data stored e.g. on our web service. I don't want to load all location data at once. When the MapView is scrolling or zooming, I can imagine that the "region" change can be translated to a fetch request, e.g. maybe it can send regionBefore and regionAfter, then the server can work out the delta, and if there are points that fall in this new zone, it can send them back to the iPhone app. The app add thse new locations as annotations. It should also work out what annotations should be removed by this change, if that is necessary. Have you seen any code like this? Thanks.

Edwin said...

scrub m65 kamagra attorney lawyer body scrub field jacket lovegra marijuana attorney injury lawyer

Yoichi said...

I found a code at http://code.google.com/p/javageomodel/issues/list

It uses a technique called "geomodel" or "geocells" that pre-register geopoints in a hierarchy of different sized cells on the whole globe.

That means, in my database, the geopoints are indexed with a decending geocells from the top resolution to the max resolution on the map. This way, I can quickly get to the geopoints I want, at any resolutino level efficently.

However, the SVN code has some bugs or unimplemented bits, which you have to fix or write missing parts yourself.

Whacko said...

The CLRegion class has a method called:
containsCoordinate. which returns bool.

so you can just do:
[mapView.region containsCoordinate: myCoordinate];

JeansPilot said...

JeansPilot offers the chance to buy a large variety of men’s and women’s jeans clothing from the world famous Italian Brands.
Online jeans clothing store looks for original fashion clothing sales and clearances of worldwide known designers. We participate in fashion auctions to get the lowest possible price for Top quality Clothes, Shoes and Accessories.
Buy Jeans

SEO Services Consultants said...

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck! Web Design Company

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels