2011-11-09 18 views
5

編集:このコードは整数ズームレベルで機能すると思うが、フロートズームレベルで動作させたいと思う。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]; 
} 
+0

私がMKMapViewについて気づいたことは、リージョンを設定して直ちにそれを読み返すと、別の値に戻ってしまうことです。私たちのアプリでは、デリゲートコールバックをトリガーする値を設定してから、新しい値を読み取り、それを使用してオーバーレイを配置する場所を決めました。これが役立つかどうかはわかりません。 :-( – EricS

答えて

4

残念ながら、これは、マップのズームレベルを設定する際に整数値のみを受け入れるGoogle Maps APIの制限は、次のとおりです。コードが記載されている最後の方法で始まる実行しますMKMapViewの表示領域を設定します。その結果は、どのMapKitメソッドを使って領域を設定するかにかかわらず、最も近い整数のズームレベルにズームアウトされたマップになります。

Troy Brantのコードでは、MapKit APIの上にレイヤーを置いてズームレベルを直接設定することができますが、最終的にはMKMapViewで表示される領域を正確に制御することはできませんあなたの望むマップのレベルは整数になります。

Stack Overflow(例:MKMapView setRegion "snaps" to predefined zoom levels?およびMKMapView show incorrectly saved region)にいくつかのバリエーションがありましたが、これまで非整数ズームレベルのマップを作成するプログラマチックな方法はありませんでした。 GoogleとAppleの協力を得てこれを実現させる。

+0

問題の概要と解決策の欠如。これは現時点では不可能なので、私は別のものを見つけなければならないと思う。 –

関連する問題