Is it possible using MapKit to show a map in iPhone, like i´m doing, to display zoom controls on the map?
If not, what are the flags or methods i should use to increase and diminish the zoom, so i can created methods which will do this at the push of a button.
There aren't any built-in controls for it.
A couple of suggestions:
You can predefine some spans and store them in an array somewhere and then store your position in that array as well. Zooming in/out changes the position and gets the span.
Zooming in takes the current span and divides by 2, zooming out multiplies by 2.
You change the span in the region of the mapView. So you have to:
Get the region of the mapView.
Get the span of the region.
Change the span to what you want.
Set the regions span to the new span.
Set the mapView's region to the new region.
Here's some code for suggestion 2:
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span;
span.latitudeDelta = region.span.latitudeDelta*2;
span.longitudeDelta = region.span.longitudeDelta*2;
region.span = span;
[mapView setRegion:region animated:TRUE];
For swift 4, this is what I use:
func zoom(_ zoomin : Bool) {
var region = mapView.region;
var span = MKCoordinateSpan();
span.latitudeDelta = zoomin ? region.span.latitudeDelta / 2 : region.span.latitudeDelta * 2;
span.longitudeDelta = zoomin ? region.span.longitudeDelta / 2 : region.span.longitudeDelta * 2;
region.span = span;
mapView.setRegion(region, animated: true);
}
Off the top of my head I don't remember (at work on a windows box) if there is a switch to display the zoom controls. I assume you are talking about displaying the level of zoom, because by default you can pinch/spread the view for zoom.
If there isn't a switch to display the controls, then you will need to create a custom view layer and put it on top of the mapkit view. Then you will need to call the different functions to change the zoom level.
Those functions are all documented in the mapkit docs. Just do a search for MapKit in the documentation center.
Edit:
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/doc/uid/TP40008205
According to the docs there isn't a switch to display the controls, but the zoom enabled property lets you turn the ability on and off.
So you can subscribe to the delegate function regionWillChange and use the mapView object to get the zoom level and then set your graphics accordingly.
I don't think you should do that, the expected way to zoom in and out is using the pinch gestures. However if you still want to go ahead and do it, you should place yourself the buttons over the MKMapView, and then with those modify the region property of the MKMapView
Related
I'm working on component using MKMapView. Map should double zoom on annotation tap. To do this, I try to reduce twice map region span, but sometimes it works incorrectly:
Here peace of code:
MKCoordinateSpan newSpan = mapView.region.span;
NSLog(#"old: %f, %f", newSpan.latitudeDelta, newSpan.longitudeDelta);
newSpan = MKCoordinateSpanMake(newSpan.latitudeDelta / 2.0, newSpan.longitudeDelta / 2.0);
NSLog(#"new: %f, %f", newSpan.latitudeDelta, newSpan.longitudeDelta);
MKCoordinateRegion region = [mapView regionThatFits:MKCoordinateRegionMake(centerCoordinate, newSpan)];
NSLog(#"!!!! (%f, %f) (%f, %f)", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta);
I take current span, reduce it and pass to regionThatFits. Sometimes results are:
old: 0.609257, 0.914612
new: 0.304629, 0.457306
!!!! (55.805472, 37.579371) (0.608178, 0.914612)
regionThatFits doubles span passed to it. So visual effect is centering of view annotation without zooming.
Any suggestions?
I had the same problem, and it happen to be that I was sometimes changing the mapView size (for the needs of the application). Then I was using regionThatFits on my mapView when its height was 0.
As FKDev answer says, regionThatFits recalculates the span according to the mapView actual frame (what was causing the crash). Hope that can help someone.
You shouldn't use regionThatFits: because it is used to change the span value to match the view frame.
From Apple's Doc :
A region that is still centered on the same point of the map but whose span values are adjusted to fit in the map view’s frame.
In your case, you can just change the span value of the map region directly.
[mapView setRegion:MKCoordinateRegionMake(centerCoordinate, newSpan)];
MKMapView returns an invalid region under iOS 5 -- latitude + latitudeDelta/2 is over 100 and shouldn't be over 90.
Has anyone seen this problem?
Steps to Reproduce:
Create an MKMapView
Log the mapView.region from within the regionDidChangeAnimated delegate method
Zoom out the map as far as possible and drag it to the right, so the view is scrolled to the top/left
Expected Results:
In iOS 4, the mapView.region is reasonable:
lat=2.202047 lon=-67.500000 latDelta=165.698164 lonDelta=225.000000
In iOS 5, however, the mapView.region is out of bounds:
lat=17.978733 lon=-67.500000 latDelta=165.698164 lonDelta=225.000000
Latitudes should be within the -90 to 90 range. However, in iOS 5, lat + latDelta/2 is 100.827815. That is not possible. While I can clamp the values at +/- 90, the offset difference is causing problems with our overlays.
Regression:
Does not happen in iOS 4.3. Happens regularly in iOS 5. Screen dumps of the map views look identical even though the center latitude is 15 degrees off.
Notes:
Project file and screen dumps can be downloaded here.
This seems to be an adequate workaround. Rather than reading the mapView.region property, call this method instead:
#implementation MKMapView(fixedRegion)
-(MKCoordinateRegion) fixedRegion_
{
// this call is broken on iOS 5, as is the region property, so don't use them
// return( [self convertRect:self.bounds toRegionFromView:self] );
CLLocationCoordinate2D topLeft = [self convertPoint:CGPointZero toCoordinateFromView:self];
CLLocationCoordinate2D bottomRight = [self convertPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height) toCoordinateFromView:self];
MKCoordinateRegion region;
region.center.latitude = (topLeft.latitude + bottomRight.latitude)/2;
region.center.longitude = (topLeft.longitude + bottomRight.longitude)/2;
region.span.latitudeDelta = fabs( topLeft.latitude - bottomRight.latitude );
region.span.longitudeDelta = fabs( topLeft.longitude - bottomRight.longitude );
return region;
}
#end
Now one could argue (correctly!) that this code isn't 100% correct either because the center value of a Mercator projection in lon/lat isn't really halfway between the top and bottom, but since this matches iOS 4 functionality and keeps the values within the legal range for the map, it works for me.
By using the MKMapView+ZoomLevel category, you won’t have to bother setting the region at all.
here is very good tutorial on the same
http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/
or
http://mayurbirari.wordpress.com/2011/02/07/how-to-access-mkmapkit-in-iphone/
After you perform the zoom/pinch operation try to load the region in
-(void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
instead in
regionDidChangeAnimated.
hope this will help..:)
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.
I have a method that works to move the the map so that it follows the current user location. This works fine, but currently the zoom scale doesn't work how I want. I would like the user to be able to zoom out or zoom in on the map how they want and still have it follow the current location at that NEW zoom scale that the user just set on the map.
I tried to do the following but it doesn't work well:
/**
* Centers the map on the current location
*
* #version $Revision: 0.1
*/
- (void)centerMapOnLocation:(CLLocation *)location {
// Make a region using our current zoom level
CLLocationDistance latitude = mapView.region.span.latitudeDelta*100;
CLLocationDistance longitude = mapView.region.span.longitudeDelta*100;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location.coordinate, latitude, longitude);
[mapView setRegion:region animated:YES];
}//end
MKMapView Zoom and Region describes how to get the current view window.
When you're creating the new MKCoordinateRegion to set the new view to, use the constructor that allows you to specify the size:
UIKIT_STATIC_INLINE MKCoordinateRegion MKCoordinateRegionMake(
CLLocationCoordinate2D centerCoordinate,
MKCoordinateSpan span
);
You'll want to pass in the span from the current region and the centerCoordinate from the user location, which I'll take it you already got.
friends,
i am new to iphone development and just implement google map in iphone application.
now i am facing a problem with following description
i have list of latitude and longitude values when display them on map those pins are displayed on different locations.
now i want to set zoom level so that all pins are displayed on the screen.
any help would be appreciated.
If you're using an MKMapView you can use the setRegion:animated: method to change the region displayed. I would use annotationsInMapRect: to determine which annotations are being shown. Since annotationsInMapRect: returns an NSSet you could make an NSSet of all your annotations, and zoom out the MKMapView until the NSSet returned from annotationsInMapRect: matches your NSSet of all annotations.
The values 3.0 and 2.0 are only examples you can increase or decrease the values according to your requirement(how much you have to zoom) ,map is my mkmapview object
MKCoordinateSpan span;
span.latitudeDelta = 3.0;
span.longitudeDelta = 2.0;
MKCoordinateRegion region;
region.span = span;
[map setRegion:region animated:YES];
[map regionThatFits:region];