Map MapKit map scale / borders to RouteMe map zooms / borders

EDIT: I believe my problem is that this code works for entire zoom levels, but I would like it to work for floating zoom levels.

I have an iOS application in which a user can switch between a map based on RouteMe and a map based on MapKit.

When they switch sources, I would like to be able to show the same area in one as in the other. However, I cannot figure out how to map them, because RouteMe and MapKit use different data structures to describe map boundaries.

Here is some code that makes it be close, but not exact. This code comes from: http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/

I'm not sure if this code should be fixed, or maybe I'm losing sight of a much easier solution. The code starts from the last method specified:

#define MERCATOR_OFFSET 268435456
#define MERCATOR_RADIUS 85445659.44705395

#pragma mark -
#pragma mark Map conversion methods

- (double)longitudeToPixelSpaceX:(double)longitude {
  return round(MERCATOR_OFFSET + MERCATOR_RADIUS * longitude * M_PI / 180.0);
}

- (double)latitudeToPixelSpaceY:(double)latitude {
  return round(MERCATOR_OFFSET - MERCATOR_RADIUS * logf((1 + sinf(latitude * M_PI / 180.0)) / (1 - sinf(latitude * M_PI / 180.0))) / 2.0);
}

- (double)pixelSpaceXToLongitude:(double)pixelX {
  return ((round(pixelX) - MERCATOR_OFFSET) / MERCATOR_RADIUS) * 180.0 / M_PI;
}

- (double)pixelSpaceYToLatitude:(double)pixelY {
  return (M_PI / 2.0 - 2.0 * atan(exp((round(pixelY) - MERCATOR_OFFSET) / MERCATOR_RADIUS))) * 180.0 / M_PI;
}


- (MKCoordinateSpan)coordinateSpanWithMapView:(MKMapView *)mapView
                             centerCoordinate:(CLLocationCoordinate2D)centerCoordinate
                                 andZoomLevel:(NSInteger)zoomLevel {
  // convert center coordiate to pixel space
  double centerPixelX = [self longitudeToPixelSpaceX:centerCoordinate.longitude];
  double centerPixelY = [self latitudeToPixelSpaceY:centerCoordinate.latitude];

  // determine the scale value from the zoom level
  NSInteger zoomExponent = 20 - zoomLevel;
  double zoomScale = pow(2, zoomExponent);

  // scale the map’s size in pixel space
  CGSize mapSizeInPixels = mapView.bounds.size;
  double scaledMapWidth = mapSizeInPixels.width * zoomScale;
  double scaledMapHeight = mapSizeInPixels.height * zoomScale;

  // figure out the position of the top-left pixel
  double topLeftPixelX = centerPixelX - (scaledMapWidth / 2);
  double topLeftPixelY = centerPixelY - (scaledMapHeight / 2);

  // find delta between left and right longitudes
  CLLocationDegrees minLng = [self pixelSpaceXToLongitude:topLeftPixelX];
  CLLocationDegrees maxLng = [self pixelSpaceXToLongitude:topLeftPixelX + scaledMapWidth];
  CLLocationDegrees longitudeDelta = maxLng - minLng;

  // find delta between top and bottom latitudes
  CLLocationDegrees minLat = [self pixelSpaceYToLatitude:topLeftPixelY];
  CLLocationDegrees maxLat = [self pixelSpaceYToLatitude:topLeftPixelY + scaledMapHeight];
  CLLocationDegrees latitudeDelta = -1 * (maxLat - minLat);

  // create and return the lat/lng span
  MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
  return span;
}


- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
                    zoomLevel:(NSUInteger)zoomLevel
                     animated:(BOOL)animated {

  // use the zoom level to compute the region
  MKCoordinateSpan span = [self coordinateSpanWithMapView:self       
                               centerCoordinate:centerCoordinate
                                   andZoomLevel:zoomLevel];
  MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);

  // set the region like normal
  [self setRegion:region animated:animated];
}
+5
source share
1 answer

Unfortunately, this is a limitation of the Google Maps API , which only accepts integer values ​​when setting the level of map scaling: the Apple MapKit code calls the basic Google Maps APIs when you set the display area of ​​MKMapView, and the result no matter which MapKit method you use to configure areas, is a map that has shrunk to the nearest integer zoom level.

API- MapKit, ... , MKMapView, .

Qaru (, MKMapView setRegion "snaps" ? MKMapView ), , , Google Apple, - .

+4

All Articles