MKMapPointForCoordinate returning invalid coordinates - objective-c

I am working with MKMapView's, annotations, overlays, etc, but I'm having a pain in the butt issue with MKMapPointForCoordinate() returning an invalid coordinate.
Code:
MKMapPoint* pointArr;
for (Category* route in validRoutes){
NSString* routeID = [route routeid];
NSArray* pointData = [routes objectForKey:routeID];
pointArr = malloc(sizeof(MKMapPoint) * [pointData count]);
int i = 0;
for (NSDictionary* routeData in pointData) {
NSString* latitude = [routeData objectForKey:#"latitude"];
NSString* longitude = [routeData objectForKey:#"longitude"];
NSLog(#"L: %# L: %#",latitude, longitude);
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([[f numberFromString:latitude] doubleValue], [[f numberFromString:longitude] doubleValue]);
NSLog(#"Coord: %f %f",coord.latitude,coord.longitude);
MKMapPoint point = MKMapPointForCoordinate(coord);
NSLog(#"Point: %f %f",point.x,point.y);
pointArr[i] = point;
i++;
}
MKPolyline *polyline = [MKPolyline polylineWithPoints:pointArr count: i];
polyline.title = [route name];
[routeOverlays setObject:polyline forKey: [route routeid]];
[map addOverlay:polyline];
free(pointArr);
}
Output Example:
L: 41.380840 L: -83.641319
Coord: 41.380840 -83.641319
Point: 71850240.204982 100266073.824832
I don't understand why the conversion to a MKMapPoint is destroying the values of my CLLocationCoordinate2D. The overlay doesn't show up on the map because the values are invalid...
EDIT: I got the point working by using MKMapPointMake instead, BUT, my overlay still isn't showing. This is the mapView: viewForOverlay: code:
-(MKOverlayView*)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
MKOverlayView *overlayView = nil;
//Checks if the overlay is of type MKPolyline
if([overlay isKindOfClass:[MKPolyline class]]){
MKPolylineView *routeLineView = [[MKPolylineView alloc] initWithPolyline:overlay];
routeLineView.strokeColor = [UIColor orangeColor];
routeLineView.lineWidth = 10;
return overlayView;
}
return nil;
}
The method gets called (Used a breakpoint to confirm), and I have annotations working (So the delegate has to be setup correctly, I assume)
Double edit: :facepalm: I was returning nil every time in the delegate code. That's what I get for copying and pasting the previous versions code ;P

An MKMapPoint is not a latitude/longitude in degrees (like CLLocationCoordinate2D).
They are not interchangeable and so you should not expect the MKMapPoint x,y values to show any obvious relation to the corresponding latitude and longitude.
An MKMapPoint is the conversion of latitude and longitude onto a flat projection using x,y values which are not in the same scale or range as latitude and longitude. Please see the Map Coordinate Systems section in the Location Awareness Programming Guide for a more detailed explanation.
By the way, if you have CLLocationCoordinate2D values, it's much easier to create a polyline using polylineWithCoordinates instead of polylineWithPoints. That way, you don't need to bother with any conversion.
See iOS SDK: MapKit MKPolyLine not showing for some more details and an example.

Related

Google Maps iOS SDK: How do I get accurate latitude and longitude coordinates from a camera's visibleRegion?

EDIT: This is now a confirmed bug with this SDK
I'm using version 1.1.1.2311 of the Google Maps for iOS SDK, and I'm looking to find the bounding latitude and longitude coordinates for the visible map on screen.
I'm using the following code to tell me what the current projection is:
NSLog(#"\n%#,%#\n%#,%#\n%#,%#\n%#,%#\n",
[NSNumber numberWithDouble:mapView.projection.visibleRegion.farLeft.latitude],
[NSNumber numberWithDouble:mapView.projection.visibleRegion.farLeft.longitude],
[NSNumber numberWithDouble:mapView.projection.visibleRegion.farRight.latitude],
[NSNumber numberWithDouble:mapView.projection.visibleRegion.farRight.longitude],
[NSNumber numberWithDouble:mapView.projection.visibleRegion.nearLeft.latitude],
[NSNumber numberWithDouble:mapView.projection.visibleRegion.nearLeft.longitude],
[NSNumber numberWithDouble:mapView.projection.visibleRegion.nearRight.latitude],
[NSNumber numberWithDouble:mapView.projection.visibleRegion.nearRight.longitude]);
From reading the headers, it seems that it may not be updated when the camera moves. Fair enough...
/**
* The GMSProjection currently used by this GMSMapView. This is a snapshot of
* the current projection, and will not automatically update when the camera
* moves. The projection may be nil while the render is not running (if the map
* is not yet part of your UI, or is part of a hidden UIViewController, or you
* have called stopRendering).
*/
But, it appears to update each time the delegate method is called, so I attempted to plot the coordinates to test them...
For the following on my phone:
The output of the NSLog from above gives me the following:
37.34209003645947,-122.0382353290915
37.34209003645947,-122.010769508779
37.30332095984257,-122.0382353290915
37.30332095984257,-122.010769508779
When plotting those coordinates using this I get a projection that seems off:
These coordinates are consistent across app launches which leads me to believe that I'm either consistently doing something wrong, I'm misunderstanding what visibleRegion is, or I've discovered a bug. Anyone care to help me figure out which one it is?
To get the bounding latitude and longitude you have to do the following steps:
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithRegion:self.googleMapsView.projection.visibleRegion];
CLLocationCoordinate2D northEast = bounds.northEast;
CLLocationCoordinate2D northWest = CLLocationCoordinate2DMake(bounds.northEast.latitude, bounds.southWest.longitude);
CLLocationCoordinate2D southEast = CLLocationCoordinate2DMake(bounds.southWest.latitude, bounds.northEast.longitude);
CLLocationCoordinate2D southWest = bounds.southWest;
Best regards
Robert
I saw your issue here. Hope that they fix this issue in next update.
But now we can take the real visible region like this:
CGPoint topLeftPoint = CGPointMake(0, 0);
CLLocationCoordinate2D topLeftLocation =
[_mapView.projection coordinateForPoint: topLeftPoint];
CGPoint bottomRightPoint =
CGPointMake(_mapView.frame.size.width, _mapView.frame.size.height);
CLLocationCoordinate2D bottomRightLocation =
[_mapView.projection coordinateForPoint: bottomRightPoint];
CGPoint topRightPoint = CGPointMake(_mapView.frame.size.width, 0);
CLLocationCoordinate2D topRightLocation =
[_mapView.projection coordinateForPoint: topRightPoint];
CGPoint bottomLeftPoint =
CGPointMake(0, _mapView.frame.size.height);
CLLocationCoordinate2D bottomLeftLocation =
[_mapView.projection coordinateForPoint: bottomLeftPoint];
GMSVisibleRegion realVisibleRegion;
realVisibleRegion.farLeft = topLeftLocation;
realVisibleRegion.farRight = topRightLocation;
realVisibleRegion.nearLeft = bottomLeftLocation;
realVisibleRegion.nearRight = bottomRightLocation;
[self drawPolylineWithGMSVisibleRegion:realVisibleRegion color:[UIColor redColor] width:10.0f forMap:mapView];
Drawing polyline method:
- (void)drawPolylineWithGMSVisibleRegion:(GMSVisibleRegion)visibleRegion
color:(UIColor*)color
width:(double)width
forMap:(GMSMapView*)mapView{
GMSPolylineOptions *rectangle = [GMSPolylineOptions options];
rectangle.color = color;
rectangle.width = width;
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:visibleRegion.nearRight];
[path addCoordinate:visibleRegion.nearLeft];
[path addCoordinate:visibleRegion.farLeft];
[path addCoordinate:visibleRegion.farRight];
[path addCoordinate:visibleRegion.nearRight];
rectangle.path = path;
[mapView addPolylineWithOptions:rectangle];
}
It works even for map with non-default bearing and angle.
The solution is to download the latest version of the SDK (1.2 at the time of this writing) as the issue has been fixed.
From the 1.2 release notes:
Resolved Issues:
- visibleRegion now reports correctly sized region on Retina devices
Download here.
Looks like the latitude and longitude coordinates you are printing out and manually plotting are possibly off a bit / being truncated. %f defaults to only print out to 6 decimal places.
Here's a related question that might help:
How to print a double with full precision on iOS?
Maybe you give the location Manager the wrong accuracy..
Try to increase it:
locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
The battery is draining moreyy

MKMapView not refreshing annotations

I have a MKMapView (obviously), that shows housing locations around the user.
I have a Radius tool that when a selection is made, the annotations should add/remove based on distance around the user.
I have it add/removing fine but for some reason the annotations won't show up until I zoom in or out.
This is the method that adds/removes the annotations based on distance. I have tried two different variations of the method.
Adds the new annotations to an array, then adds to the map by [mapView addAnnotations:NSArray].
Add the annotations as it finds them using [mapView addAnnotation:MKMapAnnotation];
1.
- (void)updateBasedDistance:(NSNumber *)distance {
//Setup increment for HUD animation loading
float hudIncrement = ( 1.0f / [[[[self appDelegate] rssParser]rssItems] count]);
//Remove all the current annotations from the map
[self._mapView removeAnnotations:self._mapView.annotations];
//Hold all the new annotations to add to map
NSMutableArray *tempAnnotations;
/*
I have an array that holds all the annotations on the map becuase
a lot of filtering/searching happens. So for memory reasons it is
more efficient to load annoations once then add/remove as needed.
*/
for (int i = 0; i < [annotations count]; i++) {
//Current annotations location
CLLocation *tempLoc = [[CLLocation alloc] initWithLatitude:[[annotations objectAtIndex:i] coordinate].latitude longitude:[[annotations objectAtIndex:i] coordinate].longitude];
//Distance of current annotaiton from user location converted to miles
CLLocationDistance miles = [self._mapView.userLocation.location distanceFromLocation:tempLoc] * 0.000621371192;
//If distance is less than user selection, add it to the map.
if (miles <= [distance floatValue]){
if (tempAnnotations == nil)
tempAnnotations = [[NSMutableArray alloc] init];
[tempAnnotations addObject:[annotations objectAtIndex:i]];
}
//For some reason, even with ARC, helps a little with memory consumption
tempLoc = nil;
//Update a progress HUD I use.
HUD.progress += hudIncrement;
}
//Add the new annotaitons to the map
if (tempAnnotations != nil)
[self._mapView addAnnotations:tempAnnotations];
}
2.
- (void)updateBasedDistance:(NSNumber *)distance {
//Setup increment for HUD animation loading
float hudIncrement = ( 1.0f / [[[[self appDelegate] rssParser]rssItems] count]);
//Remove all the current annotations from the map
[self._mapView removeAnnotations:self._mapView.annotations];
/*
I have an array that holds all the annotations on the map becuase
a lot of filtering/searching happens. So for memory reasons it is
more efficient to load annoations once then add/remove as needed.
*/
for (int i = 0; i < [annotations count]; i++) {
//Current annotations location
CLLocation *tempLoc = [[CLLocation alloc] initWithLatitude:[[annotations objectAtIndex:i] coordinate].latitude longitude:[[annotations objectAtIndex:i] coordinate].longitude];
//Distance of current annotaiton from user location converted to miles
CLLocationDistance miles = [self._mapView.userLocation.location distanceFromLocation:tempLoc] * 0.000621371192;
//If distance is less than user selection, add it to the map.
if (miles <= [distance floatValue])
[self._mapView addAnnotation:[annotations objectAtIndex:i]];
//For some reason, even with ARC, helps a little with memory consumption
tempLoc = nil;
//Update a progress HUD I use.
HUD.progress += hudIncrement;
}
}
I have also attempted at the end of the above method:
[self._mapView setNeedsDisplay];
[self._mapView setNeedsLayout];
Also, to force a refresh (saw somewhere it might work):
self._mapView.showsUserLocation = NO;
self._mapView.showsUserLocation = YES;
Any help would be very much appreciated and as always, thank you for taking the time to read.
I'm going to guess that updateBasedDistance: gets called from a background thread. Check with NSLog(#"Am I in the UI thread? %d", [NSThread isMainThread]);. If it's 0, then you should move the removeAnnotations: and addAnnotation: to a performSelectorOnMainThread: invocation, or with GCD blocks on the main thread.

Route Me: Location & Screen Position

I am drawing a bunch of Markers on my Map View.
The information about the position of all objects (latitude and longitude) is stored in an Array.
To optimize performance i don't want to draw ALL Markers. I only want to alloc Markers in the area i am seeing at the moment. But the only information i get from the Route Me API about position/screen is:
center.latitude;
center.longitude;
But this value is only returning the center of my whole map and i want to see the center position (lat and long) of the actual view. I am also able to get the GPS position, but not the center position of the screen. Do you think there is an easy way to get this information?
This is a part of my implementation:
UIImage* object = [UIImage imageNamed:#"object.png"];
CLLocationCoordinate2D buoyLocation;
NSString* templatString;
NSString* templongString;
for (int i =0; i<(myArray.count); i=i+2)
{
templongString = [myArray objectAtIndex:i];
templatString = [myarray objectAtIndex:i+1];
objectLocation.latitude = [templatString floatValue];
objectLocation.longitude = [templongString floatValue];
myMarker = [[RMMarker alloc] initWithUIImage:object anchorPoint:CGPointMake(xspec, yspec)]; //0.5 1.0
[markerManager1 addMarker:myMarker AtLatLong:objectLocation];
}
Take a look at latitudeLongitudeBoundingBoxForScreen in RMMapContents.
Ciao!
-- Randy

Eliminate Pin Overlap in the MKMapView

I am working with MKMapView Based application. I need a clarification whether it is possible to eliminate the pin OverLap in the MKMapView? Because at some places there are large number of pins displaying. It is difficult to me to identify the pins.
If you have an Apple Developer Account, I would strongly recommend getting the Session 111 video from the 2011 WWDC Conference Sessions, entitled "Visualizing Information Geographically with MapKit". One of the segments specifically covers how to cluster content from large data sets to allow you to group or ungroup pins based on density at various zoom levels.
Their example is elegantly simple, but at the heart of the problem you want to replace a group of overlapping pins with a single pin and as you zoom in the single pin will split back into the individual pins.
How and when you decide to group things can be varied considerably. Apple's solution simply subdivides the map into a grid and any box that has more than 1 pin results in a group. You could also take an algorithmic approach such as using a kMeansCluster algorithm which is incredibly simple and you could feed all of your annotations through the algorithm and get an array of groups out the other side logically organized.
From there it's a matter of keeping track of all the individual pins and how they are grouped as you zoom in and out. You will only display a single annotation for each group or any individual pins that are left over. It's also possible to animate the transitions as the map zooms in and out so you can visually reinforce what is happening.
My own technique is too closely related to Apple's approach for me to post here so I'm hoping you can access the above video which covers almost all of these points.
For this you have to implement clustering concept to your map.By using Apple demo code it's easy to implement clustering concept in our code. Reference link
Simply we can use following code for the Clustering
Steps to implement clustering
Step1 : The important thing is for clustering we use two mapviews(allAnnotationsMapView, ), One is for reference(allAnnotationsMapView).
#property (nonatomic, strong) MKMapView *allAnnotationsMapView;
#property (nonatomic, strong) IBOutlet MKMapView *mapView;
In viewDidLoad
_allAnnotationsMapView = [[MKMapView alloc] initWithFrame:CGRectZero];
Step2 : Add all annotations to the _allAnnotationsMapView, In below _photos are the annotations array.
[_allAnnotationsMapView addAnnotations:_photos];
[self updateVisibleAnnotations];
Step3 : Add below methods for clustering, in this PhotoAnnotation is the custom annotation.
MapViewDelegate methods
- (void)mapView:(MKMapView *)aMapView regionDidChangeAnimated:(BOOL)animated {
[self updateVisibleAnnotations];
}
- (void)mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views {
for (MKAnnotationView *annotationView in views) {
if (![annotationView.annotation isKindOfClass:[PhotoAnnotation class]]) {
continue;
}
PhotoAnnotation *annotation = (PhotoAnnotation *)annotationView.annotation;
if (annotation.clusterAnnotation != nil) {
// animate the annotation from it's old container's coordinate, to its actual coordinate
CLLocationCoordinate2D actualCoordinate = annotation.coordinate;
CLLocationCoordinate2D containerCoordinate = annotation.clusterAnnotation.coordinate;
// since it's displayed on the map, it is no longer contained by another annotation,
// (We couldn't reset this in -updateVisibleAnnotations because we needed the reference to it here
// to get the containerCoordinate)
annotation.clusterAnnotation = nil;
annotation.coordinate = containerCoordinate;
[UIView animateWithDuration:0.3 animations:^{
annotation.coordinate = actualCoordinate;
}];
}
}
}
clustering Handling methods
- (id<MKAnnotation>)annotationInGrid:(MKMapRect)gridMapRect usingAnnotations:(NSSet *)annotations {
// first, see if one of the annotations we were already showing is in this mapRect
NSSet *visibleAnnotationsInBucket = [self.mapView annotationsInMapRect:gridMapRect];
NSSet *annotationsForGridSet = [annotations objectsPassingTest:^BOOL(id obj, BOOL *stop) {
BOOL returnValue = ([visibleAnnotationsInBucket containsObject:obj]);
if (returnValue)
{
*stop = YES;
}
return returnValue;
}];
if (annotationsForGridSet.count != 0) {
return [annotationsForGridSet anyObject];
}
// otherwise, sort the annotations based on their distance from the center of the grid square,
// then choose the one closest to the center to show
MKMapPoint centerMapPoint = MKMapPointMake(MKMapRectGetMidX(gridMapRect), MKMapRectGetMidY(gridMapRect));
NSArray *sortedAnnotations = [[annotations allObjects] sortedArrayUsingComparator:^(id obj1, id obj2) {
MKMapPoint mapPoint1 = MKMapPointForCoordinate(((id<MKAnnotation>)obj1).coordinate);
MKMapPoint mapPoint2 = MKMapPointForCoordinate(((id<MKAnnotation>)obj2).coordinate);
CLLocationDistance distance1 = MKMetersBetweenMapPoints(mapPoint1, centerMapPoint);
CLLocationDistance distance2 = MKMetersBetweenMapPoints(mapPoint2, centerMapPoint);
if (distance1 < distance2) {
return NSOrderedAscending;
} else if (distance1 > distance2) {
return NSOrderedDescending;
}
return NSOrderedSame;
}];
PhotoAnnotation *photoAnn = sortedAnnotations[0];
NSLog(#"lat long %f %f", photoAnn.coordinate.latitude, photoAnn.coordinate.longitude);
return sortedAnnotations[0];
}
- (void)updateVisibleAnnotations {
// This value to controls the number of off screen annotations are displayed.
// A bigger number means more annotations, less chance of seeing annotation views pop in but decreased performance.
// A smaller number means fewer annotations, more chance of seeing annotation views pop in but better performance.
static float marginFactor = 2.0;
// Adjust this roughly based on the dimensions of your annotations views.
// Bigger numbers more aggressively coalesce annotations (fewer annotations displayed but better performance).
// Numbers too small result in overlapping annotations views and too many annotations on screen.
static float bucketSize = 60.0;
// find all the annotations in the visible area + a wide margin to avoid popping annotation views in and out while panning the map.
MKMapRect visibleMapRect = [self.mapView visibleMapRect];
MKMapRect adjustedVisibleMapRect = MKMapRectInset(visibleMapRect, -marginFactor * visibleMapRect.size.width, -marginFactor * visibleMapRect.size.height);
// determine how wide each bucket will be, as a MKMapRect square
CLLocationCoordinate2D leftCoordinate = [self.mapView convertPoint:CGPointZero toCoordinateFromView:self.view];
CLLocationCoordinate2D rightCoordinate = [self.mapView convertPoint:CGPointMake(bucketSize, 0) toCoordinateFromView:self.view];
double gridSize = MKMapPointForCoordinate(rightCoordinate).x - MKMapPointForCoordinate(leftCoordinate).x;
MKMapRect gridMapRect = MKMapRectMake(0, 0, gridSize, gridSize);
// condense annotations, with a padding of two squares, around the visibleMapRect
double startX = floor(MKMapRectGetMinX(adjustedVisibleMapRect) / gridSize) * gridSize;
double startY = floor(MKMapRectGetMinY(adjustedVisibleMapRect) / gridSize) * gridSize;
double endX = floor(MKMapRectGetMaxX(adjustedVisibleMapRect) / gridSize) * gridSize;
double endY = floor(MKMapRectGetMaxY(adjustedVisibleMapRect) / gridSize) * gridSize;
// for each square in our grid, pick one annotation to show
gridMapRect.origin.y = startY;
while (MKMapRectGetMinY(gridMapRect) <= endY) {
gridMapRect.origin.x = startX;
while (MKMapRectGetMinX(gridMapRect) <= endX) {
NSSet *allAnnotationsInBucket = [self.allAnnotationsMapView annotationsInMapRect:gridMapRect];
NSSet *visibleAnnotationsInBucket = [self.mapView annotationsInMapRect:gridMapRect];
// we only care about PhotoAnnotations
NSMutableSet *filteredAnnotationsInBucket = [[allAnnotationsInBucket objectsPassingTest:^BOOL(id obj, BOOL *stop) {
return ([obj isKindOfClass:[PhotoAnnotation class]]);
}] mutableCopy];
if (filteredAnnotationsInBucket.count > 0) {
PhotoAnnotation *annotationForGrid = (PhotoAnnotation *)[self annotationInGrid:gridMapRect usingAnnotations:filteredAnnotationsInBucket];
[filteredAnnotationsInBucket removeObject:annotationForGrid];
// give the annotationForGrid a reference to all the annotations it will represent
annotationForGrid.containedAnnotations = [filteredAnnotationsInBucket allObjects];
[self.mapView addAnnotation:annotationForGrid];
for (PhotoAnnotation *annotation in filteredAnnotationsInBucket) {
// give all the other annotations a reference to the one which is representing them
annotation.clusterAnnotation = annotationForGrid;
annotation.containedAnnotations = nil;
// remove annotations which we've decided to cluster
if ([visibleAnnotationsInBucket containsObject:annotation]) {
CLLocationCoordinate2D actualCoordinate = annotation.coordinate;
[UIView animateWithDuration:0.3 animations:^{
annotation.coordinate = annotation.clusterAnnotation.coordinate;
} completion:^(BOOL finished) {
annotation.coordinate = actualCoordinate;
[self.mapView removeAnnotation:annotation];
}];
}
}
}
gridMapRect.origin.x += gridSize;
}
gridMapRect.origin.y += gridSize;
}
}
By following above steps we can achieve clustering on mapview, it is not necessary to use any third party code or framework. Please check the Apple sample code here. Please let me know if you have any doubts on this.
It's quite easy to implement your own annotation clustering framework. Here's an example of a basic one that you can refer here.
If your pins are overlapping then it must be that your zoom level is high for that place.
You can think of removing some annotations in that zoom level until you dont have annotation overlaps and while zooming in you can add the annotations so that there are enough space between the annotations.

Equivalent Objective-C code for Google Maps GMap2.getBoundsZoomLevel(bounds)?

I'm familiar with the Google Maps API and I'm trying to learned the iOS MapKit library now. I have an iPhone application which takes an input string and geocodes it using Google Maps geocoder service. I also want to set an appropriate zoom level for the new map but I'm not quite sure how to do it.
After reading, Determining zoom level from single LatLong in Google Maps API
My plan was to parse the JSON response from the Google Maps API and extract the ExtendedData field.
"ExtendedData":{
"LatLonBox":{
"north":34.13919,
"south":34.067018,
"east":-118.38971,
"west":-118.442796
}
Then using that I would like to set the bounds of my map accordingly using the MapKit setRegion function.
I started laying out a function to do this, but I'm a little lost on the logic...
- (void) setMapZoomForLocation(CLLocationCoordinate2D location, double north, double south, double east, double west){
// some fancy math here....
// set map region
MKCoordinateRegion region;
region.center = location;
MKCoordinateSpan span;
// set the span so that the map bounds are correct
span.latitudeDelta = ???;
span.longitudeDelta = ???;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
Alternatively, I guess I could just used the Accuracy result from a geocode result to set a sort of default zoom level. I'm just not sure how to compute the equivalent default zoom levels for the various results.
See https://code.google.com/apis/maps/documentation/geocoding/v2/#GeocodingAccuracy
------------------------ Update: Solution I Used ------------------------------
// parse json result
NSDictionary *results = [jsonString JSONValue];
NSArray *placemarks = (NSArray *) [results objectForKey:#"Placemark"];
NSDictionary *firstPlacemark = [placemarks objectAtIndex:0];
// parse out location
NSDictionary *point = (NSDictionary *) [firstPlacemark objectForKey:#"Point"];
NSArray *coordinates = (NSArray *) [point objectForKey:#"coordinates"];
CLLocationCoordinate2D location;
location.latitude = [[coordinates objectAtIndex:1] doubleValue];
location.longitude = [[coordinates objectAtIndex:0] doubleValue];
// DEBUG
//NSLog(#"Parsed Location: (%g,%g)", location.latitude, location.longitude);
// parse out suggested bounding box
NSDictionary *extendedData = (NSDictionary *) [firstPlacemark objectForKey:#"ExtendedData"];
NSDictionary *latLngBox = (NSDictionary *) [extendedData objectForKey:#"LatLonBox"];
double north = [[latLngBox objectForKey:#"north"] doubleValue];
double south = [[latLngBox objectForKey:#"south"] doubleValue];
double east = [[latLngBox objectForKey:#"east"] doubleValue];
double west = [[latLngBox objectForKey:#"west"] doubleValue];
// DEBUG
//NSLog(#"Parsed Bounding Box: ne = (%g,%g), sw = (%g,%g)", north, east, south, west);
MKCoordinateSpan span = MKCoordinateSpanMake(fabs(north - south), fabs(east - west));
MKCoordinateRegion region = MKCoordinateRegionMake(location, span);
[self.mapView setRegion:region animated:YES];
If all you want to do is generate a MKCoordinateRegion, you shouldn't need to know anything at all about zoom level. Just create a coordinate region using the width and height of the LatLonBox.
MKCoordinateSpan span = MKCoordinateSpanMake(fabs(north - south), fabs(east - west));
MKCoordinateRegion region = MKCoordinateRegionMake(location, span);
[self.mapView setRegion:region animated:YES];