Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. CLLocationCoordinate2D myNorthEast, mySouthWest;
  2.  
  3. -(void)viewDidLoad{
  4. myNorthEast = CLLocationCoordinate2DMake(lat1,lon1);
  5. mySouthWest = CLLocationCoordinate2DMake(lat2,lon2);
  6. [super viewDidLoad];
  7. }
  8.  
  9. -(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
  10. /*
  11. / Check if the map region is going to change outside your fence.
  12. / If so, programmatically set it back to the edge of your fence.
  13. */
  14.  
  15. if(!animated){
  16. return; // Don't want to get stuck in a loop after you set your region below.
  17. }
  18.  
  19. MKCoordinateRegion region = [mapView region];
  20.  
  21. // You will need to get the NE and SW points of the new region to compare
  22. // First we need to calculate the corners of the map so we get the points
  23. CGPoint nePoint = CGPointMake(mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
  24. CGPoint swPoint = CGPointMake(mapView.bounds.origin.x, bounds.origin.y + mapView.bounds.size.height);
  25.  
  26. // Then transform those point into lat,lng values
  27. CLLocationCoordinate2D neCoord;
  28. neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];
  29.  
  30. CLLocationCoordinate2D swCoord;
  31. swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];
  32.  
  33. /*
  34. You will need to mess around with the lat/lon & sign of the new center calculation for the other cases.
  35. */
  36. if(neCoord.latitude > myNorthEast.latitude){
  37. MKCoordinateRegion newRegion;
  38. newRegion.span = region.span;
  39. CLLocationCoordinate2D newCenter;
  40. newCenter.longitude = region.center.longitude;
  41. newCenter.latitude = myNorthEast.latitude - region.span.latitudeDelta;
  42. newRegion.center = newCenter;
  43. [mapView setRegion:newRegion animated:NO];
  44. }else if(neCoord.longitude < myNorthEast.longitude){
  45.  
  46. }else if(swCoord.latitude < mySouthWest.latitude){
  47.  
  48. }else if(swCoord.longitude > mySouthWest.longitude){
  49.  
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement