如何将由NE和SW坐标组成的特定边界拟合到可见地图视图中? [英] How to fit a certain bounds consisting of NE and SW coordinates into the visible map view?

查看:97
本文介绍了如何将由NE和SW坐标组成的特定边界拟合到可见地图视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在地图中适应某些边界。我通过调用谷歌地理编码器并阅读视口属性来获得界限:

I need to fit a certain bounds within a map. I get the bounds from calling the google geocoder and reading the viewport property which looks like:

{
    northeast =     {
        lat = "30.4212235";
        lng = "-97.486942";
    };
    southwest =     {
        lat = "30.1128403";
        lng = "-97.99917959999999";
    };
}

然后我将这些转换为CLLocationCoordinate2D

I then convert these into CLLocationCoordinate2D

NSDictionary *viewport = [[[results objectAtIndex:0] objectForKey:@"geometry"] 
                                                     objectForKey:@"viewport"];
NSDictionary *NEDictionary = [viewport objectForKey:@"northeast"];
NSDictionary *SWDictionary = [viewport objectForKey:@"southwest"];

CLLocationCoordinate2D SWCoordinate = 
    CLLocationCoordinate2DMake(
        [[SWDictionary objectForKey:@"lat"] floatValue], 
        [[SWDictionary objectForKey:@"lng"] floatValue]
    );
CLLocationCoordinate2D NECoordinate = 
    CLLocationCoordinate2DMake(
        [[NEDictionary objectForKey:@"lat"] floatValue], 
        [[NEDictionary objectForKey:@"lng"] floatValue]
    );    

我知道我需要从这些坐标生成MKMapRect(或MKMapRegion,哪个更容易)然后[mapView setVisibleRect:newRect animated:YES](或[mapView setRegion:newRegion animated:YES]但我不太清楚如何到达那里。我需要一种方法将边界转换为正确的数据结构,如:

I know I need to generate a MKMapRect (or MKMapRegion, whichever is easier) from these coordinates and then [mapView setVisibleRect:newRect animated:YES] (or [mapView setRegion:newRegion animated:YES] but I'm not quite sure how to get there. I need a method to convert the bounds into the proper data structure, something like:

- (MKMapRect) mapRectThatFitsBoundsSW:(CLLocationCoordinate2D)sw 
                                   NE:(CLLocationCoordinate2D)ne {
    // CGFloat x = ??
    // CGFloat y = ??
    // CGFloat width = ??
    // CGFloat height = ??
    MKMapRect mapRectFromBounds = MKMapRectMake(x,y,width,height);
    return mapRectFromBounds;
}

有什么想法吗?

推荐答案

如果您的界限可以跨越第180个子午线,则必须在转换中考虑到这一点:

If your bounds can span the 180th meridian, you have to account for that in the conversion:

- (MKMapRect) mapRectThatFitsBoundsSW:(CLLocationCoordinate2D)sw 
                                   NE:(CLLocationCoordinate2D)ne 
{
    MKMapPoint pSW = MKMapPointForCoordinate(sw);
    MKMapPoint pNE = MKMapPointForCoordinate(ne);

    double antimeridianOveflow = 
      (ne.longitude > sw.longitude) ? 0 : MKMapSizeWorld.width;    

    return MKMapRectMake(pSW.x, pNE.y, 
                        (pNE.x - pSW.x) + antimeridianOveflow, 
                        (pSW.y - pNE.y));
}

但要注意那些 MKMapRect s跨越 anitmeridian ,因为它们来自龙所在的土地。如果你想了解那里存在的一些危险,请查看
MKMapRect并显示跨越180度子午线的地图叠加层。你被警告了!

But beware those MKMapRects that span the anitmeridian, because they come from the land where the dragons live. If you want to learn about some of the dangers that lie there, have a look at MKMapRect and displaying map overlays that span 180th meridian. You have been warned!

这篇关于如何将由NE和SW坐标组成的特定边界拟合到可见地图视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆