How to get longitude and latitude on MKMapView and handling the drag event.
I uploaded GeoHex v3 implementation by objective-c the othere day(http://d.hatena.ne.jp/seikoudoku2000/20110504), and I'll write some details about it.
I wanted to implement not only GeoHex encoding which converts longitude + latitude to GeoHex code but also drawing action according to your finger's drag move, like yubichiz(http://latlonglab.yahoo.co.jp/map/) created by yahoo Japan.
Although I can just guess about yubichiz, finding the spots along the line which a user draw would be a complicated algorithm and cost a lot machine power.
But if you can replace the line by GeoHex (see some neighboring GeoHex as line) , giving a GeoHex code to each spot makes it possible to search spots along the GeoHex line with simple query(ex. SELECT * FROM spot_table WHERE hex_level_6 = 'xxx' ).
I tried it before by using javascript , but I was not satisfied with the drawing response. There exists a time lag between your finger's drag move and the drawing hexes.
I hoped implementing by objective-C would make the situation better and wanted to try it!
Well, so much for preliminaries, I'll get into the point. I need to implement two functions to realize it.
- get the longitude and latitude where a user touches on MKMapView.
- controlling the drag event on MKMapView to draw hexes and ,by changing modes, dragging the map when you need to. (like yahoo's yubichiz.)
I googled a lot about it, I can't find the standard way, so l note the my way.
To tell the conclusion first,
- using a function which MKMapView provides.
- using UIPanGestureRecognizer. when you want to draw GeoHex, disabling the map drag and valid the UIPanGestureRecognizer , vice versa.
abridgment of codes are like this. (all codes are uploaded on GitHub.)
- (void)viewDidLoad { [super viewDidLoad]; mapView_.delegate = self; MKCoordinateRegion region = mapView_.region; region.span.latitudeDelta = 0.05; region.span.longitudeDelta = 0.05; region.center = CLLocationCoordinate2DMake(35.658517, 139.701334); //near Shibuya,Tokyo [mapView_ setRegion:region animated:YES]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; [mapView_ addGestureRecognizer:tapGesture]; [tapGesture release]; panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; level = [[levelLabel text] intValue]; hexCodeSet = [[NSMutableSet set] retain]; polyArray = [[NSMutableArray alloc] init]; } - (void) handlePanGesture:(UIPanGestureRecognizer*)sender { CGPoint location = [sender locationInView:mapView_]; CLLocationCoordinate2D mapPoint = [mapView_ convertPoint:location toCoordinateFromView:mapView_]; [self drawHex:mapPoint.latitude lon:mapPoint.longitude level:level]; } - (void) handleTapGesture:(UITapGestureRecognizer*)sender { CGPoint location = [sender locationInView:mapView_]; CLLocationCoordinate2D mapPoint = [ mapView_ convertPoint:location toCoordinateFromView:mapView_]; [self drawHex:mapPoint.latitude lon:mapPoint.longitude level:level]; } -(IBAction)changeMode:(UISegmentedControl *)sender { if ([sender selectedSegmentIndex] == 0) { mapView_.scrollEnabled = TRUE; [mapView_ removeGestureRecognizer:panGesture]; } else { mapView_.scrollEnabled = FALSE; [mapView_ addGestureRecognizer:panGesture]; } }
I'm an Objective-C amateur and the reason why I noticed UIPanGestureRecognizer is oreilly's Programming iOS4(now, early release version) which I was reading (with lots of efforts..). I knew the book by the deal of the day feeds from oreilly , saw the good review, bought the epub version , and put it into the iPad.
I feel I got a return on my investment.
Not only that, I felt this book is very good for understanding what is pointer, object, class, message.(It's taking a lot of pages before objective-c code appears at the same time..) If this book is tranlated into Japanese, I want to recommend this book to other engineers , especially new recruits.