How to show map of within 2.5 Miles from Current Place? - iphone

I have MKMapView and I am showing it on viewdidload using following code. However i want to display map of 2.5 Miles Radius when the view loads for the first time and then allow user to zoomin - zoomout.
Let me know how can i set zoom level of 2.5 Miles radius.
Please let me know if I sounds unclear.
Thanks,
Jigar
//[mapListBgView setMapType:MKTYP];
[dataDisMapView setMapType:MKMapTypeSatellite];
[dataDisMapView setZoomEnabled:YES];
[dataDisMapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 25.76 ;
region.center.longitude = -80.19;
[dataDisMapView setRegion:region animated:YES];

Use the MKCoordinateRegionMakeWithDistance() function to create an appropriately sized region. The only thing you have to do is manually convert from miles to meters.

Related

MKCoordinateregion's center changes when span changed - iphone

I have a MKCoordinateregion called region. I want to change span of this region without changing it's center. I am using following method to change zoom.
-(void) changeZoom
{
NSLog(#"before zoom span, center are %f, %f,%f,%f", region.span.latitudeDelta, region.span.longitudeDelta, mymap.centerCoordinate.latitude, mymap.centerCoordinate.longitude);
region.span.latitudeDelta = current_factor ;
region.span.longitudeDelta = current_factor ;
region.center.latitude = mymap.centerCoordinate.latitude ;
region.center.longitude = mymap.centerCoordinate.longitude ;
[mymap setRegion:region animated:TRUE];
NSLog(#"after zoom span, center are %f, %f,%f,%f", region.span.latitudeDelta, region.span.longitudeDelta, mymap.centerCoordinate.latitude, mymap.centerCoordinate.longitude);
}
Log shows :
before zoom span, center are 0.021950, 0.021950,19.068080,72.838111
after zoom span, center are 0.043901, 0.043901,19.068040,72.838154
This method set's span accurately as desired.But I dont want any change in the center's coordinate, Because when I zoom out and then zoom back I end up at a location different from starting location.
Is there any method so that zoom in and out are possible without any change in center?
Thanks for any help in advance.
Your before and after center coordinates are almost exactly the same, so it shouldn't be a problem. If you really want the exact same center coordinates, call setCenterCoordinate:animated: right after you use setRegion:animated: and pass some saved center coordinates.
CLLocationCoordinate2D originalCenter = mymap.centerCoordinate;
// ... adjust region
[mymap setCenterCoordinate:originalCenter animated:NO];
Also, you should use YES instead of TRUE for booleans:
[mymap setRegion:region animated:YES];
You need to adjust the region of the map to make sure the aspect ratio fits the frame size of the map view.
Once the region with the desired center is calculated, adjust the region as,
MKCoordinateRegion adjustedRegion = [mymap regionThatFits:region];
[mymap setRegion:adjustedRegion animated:TRUE];
See docs.

GPS location on map in application

I have one application that shows maps. I need to show current position of user and manage to do that with
[mapView setShowsLocation:YES];
But when I zoom In or zoom Out map it needs a lot of time to show me that blue pin again.
Is it normal or I need to put something else to keep that blue pin on screen all time???
Thanks.
[self setCurrentLocation:self._mapView.userLocation.location.coordinate withZoom:1.0];
self._mapView.showsUserLocation = YES;
- (void)setCurrentLocation:(CLLocationCoordinate2D)coord withZoom:(float)zoomLevel {
MKCoordinateRegion region = self._mapView.region;
region.span.latitudeDelta = self.defaultSpanLevel.latitudeDelta*zoomLevel;
region.span.longitudeDelta = self.defaultSpanLevel.longitudeDelta*zoomLevel;
region.center = coord;
[self._mapView setRegion:region animated:YES];
}
You can use this in view will appear Method
Then You can use mapview delegate methods....
region did change animated:YES { and set map's region here.... take
current user location as region centre.. }
This will solve your problem
}

Continuous zooming with MKMapView?

I am working with a MKMapView thats visible area (zoom level) is set with a slider.
I set the visible area with a MKCoordinateRegion and setRegion:animated:.
The problem is that I don't seem to be able to get extremely precise control over the visible area. It's as if the latitude\longitude deltas snap to the default ~21 zoom levels provided by Google.
What I really want is similar to the behavior of the Map.app when pinching to zoom. It scales the view until the threshold for a new zoom level is reached, and then it renders the new map level.
Is there a simple way to access\emulate this behavior? How does it work?
Code I'm using:
MKCoordinateRegion region;
MKCoordinateSpan span;
CLLocationCoordinate2D center = {45.475969,-73.64095};
region.center = center;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
region.span = span;
[mapView setRegion:region animated:TRUE];
If I use a delta of 0.01 or 0.013, I get the exact same map.
The simple answer is that MapKit does not allow you to programatically set a zoom level other than the default 21 levels supplied by google. The region span you set will always "snap" to the closest one.
Are you using setRegion:animated: ?
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instm/MKMapView/setRegion:animated:
You can set mapview's visibleMapRect to achieve continuous zooming.
Following code shows how to zoom in 1 level (it's a subclass of a MKMapView so self is an instance of MKMapView).
MKMapRect visibleRect = self.visibleMapRect;
MKMapSize size = visibleRect.size;
CGFloat aspectRatio = size.width / size.height;
CGPoint center = CGPointMake(visibleRect.origin.x + size.width/2, visibleRect.origin.y + size.height/2);
CGFloat zoomedHeight = size.height / 2; // By divide by 2, will zoom in exact 1 level
CGFloat zoomedWidth = zoomedHeight * aspectRatio;
visibleRect = MKMapRectMake(center.x - zoomedWidth/2, center.y - zoomedHeight/2, zoomedWidth, zoomedHeight);
self.visibleMapRect = visibleRect;

A Reset button and a bookmark button in mapkit in xcode

I am currently working on an application involving mapkit. I would like to add a reset button on the view which resets the view to its default view when you open the program, or better still, the mapkit resets itself when you open and close the app.
The code i have used to set the initial region is as follows:
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 54.049929;
region.center.longitude = -4.54834;
region.span.longitudeDelta = 4.5;
region.span.latitudeDelta = 4.5;
[mapView setRegion:region animated:YES];
Any help would be greatly appreciated.
Store the Location of your map
In .h file
CLLocationCoordinate2D location;
When setting initial Region
location.latitude = 54.049929;
location.longitude = -4.54834;
In your Reset Button
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = location.latitude
region.center.longitude = location.longitude;
region.span.longitudeDelta = 4.5;
region.span.latitudeDelta = 4.5;
[mapView setRegion:region animated:YES];
So, are you trying to figure out how to actually add the button to the view and link it to a method in the code?
The code itself within the method would just be the same as you used for your initial setup, as indicated by BuildSucceded above ...
You should just add a button to the toolbar/navbar(if you have one), and link it to a "resetMap()" method.

MKMapView broken in 3.2.3 / OS4 - Can't set region

In the last version of Xcode, set region worked fine, now in 3.2.3 it doesn't snap to your specified region?
After View did load...
[mapView setMapType:MKMapTypeHybrid];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 41.902245099708516;
region.center.longitude = 12.457906007766724;
region.span.longitudeDelta = 0.04f;
region.span.latitudeDelta = 0.04f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
This is the code that worked fine, now it doesn't snap to the location denoted above, it just shows the world map.
Any help greatly appreciated.
I've done it lots in 3.2.3 and iOS4. I promise it works.
I like MKCoordinateRegionMakeWithDistance().
CLLocationCoordinate2d coord = { 41.902245099708516, 12.457906007766724 };
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 1000, 1000);
[mapView setRegion:[mapView regionThatFits:region] animated:YES];
That "1000, 1000" in the second and third arg are the latitudinal and longitudinal meters of the range component of the region. Passing the region through the mapview's -regionThatFits: method is just good practice.