編集:このコードは整数ズームレベルで機能すると思うが、フロートズームレベルで動作させたいと思う。MapKitマップのズーム/境界をRouteMeマップのズーム/境界と一致させる
私は、ユーザーがRouteMeベースのマップとMapKitベースのマップを切り替えることができるiOSアプリケーションを持っています。
ソースを切り替えると、まったく同じ領域を他のものと同じにすることができます。しかし、RouteMeとMapKitはマップ境界を記述するために異なるデータ構造を使用するため、どのようにマッチさせるかを理解できません。
これは、いくらか近くになるようなコードですが、正確ではありません。このコードは以下のところから入手できます:http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/
このコードを修正する必要があるのか、もっと簡単な解決策が見落とされているのか分かりません。 AppleのMapKitコードは、基礎となるGoogleマップAPIの際にあなたを呼んでいる:
#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];
}
私がMKMapViewについて気づいたことは、リージョンを設定して直ちにそれを読み返すと、別の値に戻ってしまうことです。私たちのアプリでは、デリゲートコールバックをトリガーする値を設定してから、新しい値を読み取り、それを使用してオーバーレイを配置する場所を決めました。これが役立つかどうかはわかりません。 :-( – EricS