mapView region latitude and longitude delta's decrease each search - ios

Each time I search for something on my mapView the map zooms to the desired location. Yet, after the first search the zoomed region is zoomed too much and continues to zoom in more with each additional search. I've tried to adding to the regions latitude and longitude delta's but it doesn't increase. I've added the code I use to zoom to the location searched. Thanks in advance.
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 100.0;
region.span.latitudeDelta /= 100.0;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:placemark];
NSLog(#"Region span long: %f",region.span.longitudeDelta);
NSLog(#"Region. span lat:: %f",region.span.latitudeDelta);
region = self.mapView.region;
region.span.longitudeDelta *= 100;
region.span.latitudeDelta *= 100;

You grab region each time from mapView and do delta /= 100, then set it to mapView again.
Your *=100 has no effect, because it is not stored anywhere after.
So this gives you effect of zomming each search)

Related

MKMapView frozen after setting region

I have a MKMapView and want to limit the zoom-out-level.
My approach is the following using the regionWillChangeAnimated-Method:
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
//Maximum zoom-out-level
MKCoordinateSpan maxSpan;
maxSpan.latitudeDelta = 0.2;
maxSpan.longitudeDelta = 0.2;
if (self.mapView.region.span.latitudeDelta > maxSpan.latitudeDelta ) {
CLLocation *location = [self.locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
MKCoordinateRegion region;
region.center = coordinate;
region.span = maxSpan;
[self.mapView setRegion:region animated:YES];
}
}
When I zoom out of the maximum span, the map-region is reset correctly to the maximum.
Unfortunately, after I call setRegion:, the mapView freezes and I can't zoom or interact anymore with the mapView. The app doesn't crash though.
Does anyone know what's wrong in my code? Is there maybe a better way to limit the zoom-out-level?
Thanks a lot in advance!

How to set-up zoom level in iOS?

I am using the code given below to set a region in map view:
CLLocationCoordinate2D loc = (CLLocationCoordinate2D )[self geoCodeUsingAddress:searchBar.text];
CLLocationDistance visibleDistance = 100000;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, visibleDistance, visibleDistance);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:region];
[self.mapView setRegion:adjustedRegion animated:YES];
[self.mapView setCenterCoordinate:loc];
[self.mapView setZoomEnabled:YES];
It shows the entire map in a small area. How can increment the zoom level?
You can also take a help of span value for MapView. Animate to that small area and span map to zoom for required value.
Use below code:
CLLocationCoordinate2D loc = (CLLocationCoordinate2D )[self geoCodeUsingAddress:searchBar.text];
[UIView animateWithDuration:1.5 animations:^{
MKCoordinateRegion region;
region.center.latitude = loc.latitude;
region.center.longitude = loc.longitude;
region.span.latitudeDelta = 0.15f; // Decrement value to zoom
region.span.longitudeDelta = 0.15f; // Decrement value to zoom
[self.mapView setRegion:region animated:YES];
} completion:^(BOOL finished) { }];
Change the visibleDistance to a smaller number. Experiment until you find something you like.
Finally I have solved the issue by removing
[self.mapView setCenterCoordinate:loc];
I worked well when i removed the above.
Try this,
CLLocationCoordinate2D loc = (CLLocationCoordinate2D )[self geoCodeUsingAddress:searchBar.text];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, visibleDistance, visibleDistance);
region.span.latitudeDelta = 0.15f;
region.span.longitudeDelta = 0.15f;
[self.mapView setRegion:region animated:YES];
[self.mapView setZoomEnabled:YES];

current viewing coordinates on map for zooming tools

My final goal is to have buttons to zoom in and out on the map, but I am stuck in finding a way to keep the center coordinates the same as I zoom. How should I go about doing this?
You can do something like the following, which grab's the map's current region, leaves the center unchanged, but adjusts the span:
- (IBAction)zoomIn:(id)sender {
MKCoordinateRegion region = self.mapView.region;
region.span.latitudeDelta /= 2.0;
region.span.longitudeDelta /= 2.0;
[self.mapView setRegion:region animated:YES];
}
- (IBAction)zoomOut:(id)sender {
MKCoordinateRegion region = self.mapView.region;
region.span.latitudeDelta = MIN(region.span.latitudeDelta * 2.0, 180.0);
region.span.longitudeDelta = MIN(region.span.longitudeDelta * 2.0, 180.0);
[self.mapView setRegion:region animated:YES];
}

MKMapRectMake how to zoom out after setup

I use MKMapRectMake to mark north east and south west to display a region. Here's how I do that:
routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
[self.mapView setVisibleMapRect:routeRect];
After I set up this display region, how can I zoom out the map a little? What is the best way to do this?
UPDATE
This is code that I use to get rect for setVisibleMapRect function:
for(Path* p in ar)
{
self.routeLine = nil;
self.routeLineView = nil;
// while we create the route points, we will also be calculating the bounding box of our route
// so we can easily zoom in on it.
MKMapPoint northEastPoint;
MKMapPoint southWestPoint;
// create a c array of points.
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * ar.count);
for(int idx = 0; idx < ar.count; idx++)
{
Path *m_p = [ar objectAtIndex:idx];
[NSCharacterSet characterSetWithCharactersInString:#","]];
CLLocationDegrees latitude = m_p.Latitude;
CLLocationDegrees longitude = m_p.Longitude;
// create our coordinate and add it to the correct spot in the array
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
MKMapPoint point = MKMapPointForCoordinate(coordinate);
// adjust the bounding box
// if it is the first point, just use them, since we have nothing to compare to yet.
if (idx == 0) {
northEastPoint = point;
southWestPoint = point;
}
else
{
if (point.x > northEastPoint.x)
northEastPoint.x = point.x;
if(point.y > northEastPoint.y)
northEastPoint.y = point.y;
if (point.x < southWestPoint.x)
southWestPoint.x = point.x;
if (point.y < southWestPoint.y)
southWestPoint.y = point.y;
}
pointArr[idx] = point;
}
// create the polyline based on the array of points.
self.routeLine = [MKPolyline polylineWithPoints:pointArr count:ar.count];
_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
// clear the memory allocated earlier for the points
free(pointArr);
[self.mapView removeOverlays: self.mapView.overlays];
// add the overlay to the map
if (nil != self.routeLine) {
[self.mapView addOverlay:self.routeLine];
}
// zoom in on the route.
[self zoomInOnRoute];
}
Try this: (Edit)
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
CLLocationCoordinate2D zoomLocation = newLocation.coordinate;
region.center = zoomLocation;
region.span = span;
region = [mapViewObject regionThatFits:region];
[mapViewObject setRegion:region animated:NO];
You can use this custom function to center the Map around two points
- (void)centerMapAroundSourceAndDestination
{
MKMapRect rect = MKMapRectNull;
MKMapPoint sourcePoint = MKMapPointForCoordinate(southWestPoint);
rect = MKMapRectUnion(rect, MKMapRectMake(sourcePoint.x, sourcePoint.y, 0, 0));
MKMapPoint destinationPoint = MKMapPointForCoordinate(_northEastPoint);
rect= MKMapRectUnion(rect, MKMapRectMake(destinationPoint.x, destinationPoint.y, 0, 0));
MKCoordinateRegion region = MKCoordinateRegionForMapRect(rect);
[_mapView setRegion:region animated:YES];
}
So in that case you need to find Centroid of a polygon and then pass that centroid values to this method so it would zoom to center of polygon i.e Centroid.
- (void)zoomMapView:(MKMapView *)mapview withLatitude:(Float32 )latitude andLongitude:(Float32 )longitude {
MKCoordinateRegion region;
region.span.latitudeDelta =0.005; //Change values to zoom. lower the value to zoom in and vice-versa
region.span.longitudeDelta = 0.009;//Change values to zoom. lower the value to zoom in and vice-versa
CLLocationCoordinate2D location;
location.latitude = latitude; // Add your Current Latitude here.
location.longitude = longitude; // Add your Current Longitude here.
region.center = location;
[mapview setRegion:region];
}
To use this method you need to pass three thing mapView, latitude and longitude i.e Position where to zoom.
how can I zoom out the map a little?
Unfortunatley MkMapView setRegion behaves so strange that this does not work on iPhone. (ios 6.1.3)
It works on iPad (ios 6.1.3)
setRegion and setVisibleMapRect
both changes the zoom factor only by steps of two. So you cannot programmatically zoom out by e.g 10%. Although Apple maps are vector based, they still snap the next higher zoom level that (would) fit the map tile pixels 1:1. Maybe to be compatible to map satellite display mode, which uses prerendered bitmaps.
Although bot methods should only correct the aspect ratio if you provided one of the lat/lon spans not perfectly, they additonally snap to said zoom levels.
Try it out: display map, then on button press: get the current region, make the longitude span 10% bigger (factor 1.1), set the region, then read it back, you will see on iphone simu and on iphone4 device the longitude span is now double the size, instead of factor 1.1.
Till today there exists no good solution.
Shame on you Apple.

MKMapView Setting Initial Zoom level

I use this code in viewWillLayoutSubviews to set the initial region of my map.
CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(13.747266, 100.526804);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord, 800, 800)];
[self.mapView setRegion:adjustedRegion animated:YES];
NSLog(#"%f",adjustedRegion.span.latitudeDelta);
However, the initial zoom level doesn't work. The coordinate is correct, but it always zoom in to probably the max level. I check the span of the region and got 0.0. How do I fix this.
You need to set your span.So give your span value here.
adjustedRegion.span.longitudeDelta = 0.005;
adjustedRegion.span.latitudeDelta = 0.005;
Custom span setting:
region.span.longitudeDelta = 0.04;
region.span.latitudeDelta = 0.04;
else programmatically:
region.span.longitudeDelta = geoMapView.region.span.latitudeDelta;
region.span.latitudeDelta = geoMapView.region.span.latitudeDelta;

Resources