获取MKMapvIew的边界 [英] Getting the bounds of an MKMapvIew

查看:135
本文介绍了获取MKMapvIew的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了设置对外部服务器的查询,我想获取当前地图视图在我正在建立的iPhone应用程序的边界。 UIView应该响应边界,但似乎MKMapView不。设置一个区域和放大地图后,我尽量得到边界。我坚持的第一步是试图获得代表地图SE和NW角的CGPoints。之后我要使用:

In order to setup a query to an external server I want to get the bounds of the current Map View in an iPhone app I'm building. UIView should respond to bounds but it seems MKMapView doesn't. After setting a region and zooming in the map I try to get the bounds. I'm stuck on the first step which is to try to get the CGPoints that represent the SE and NW corners of the map. After that I was going to use:

- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view

将点转换为地图坐标。但我甚至不能得到那么远...

To transform the points into map coordinates. But I can't even get that far...

//Recenter and zoom map in on search location
MKCoordinateRegion region =  {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = mySearchLocation.searchLocation.coordinate;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[self.mapView setRegion:region animated:YES];


//After the new search location has been added to the map, and the map zoomed, we need to update the search bounds
//First we need to calculate the corners of the map
CGPoint se = CGPointMake(self.mapView.bounds.origin.x, mapView.bounds.origin.y);
CGPoint nw = CGPointMake((self.mapView.bounds.origin.x + mapView.bounds.size.width), (mapView.bounds.origin.y + mapView.bounds.size.height));
NSLog(@"points are: se %@, nw %@", se, nw);

代码编译时没有警告,但se和nw都为null。看看self.mapView.bounds.origin.x的变量是0.尝试NSLog直接self.mapView.bounds.size.width给我一个程序接收信号:EXC_BAD_ACCESS。这似乎来自NSLog。

The code compiles without warnings however se and nw are both null. Looking at self.mapView.bounds.origin.x the variable is 0. Trying to NSLog directly self.mapView.bounds.size.width gives me a "Program received signal: "EXC_BAD_ACCESS"." which seems to come from NSLog.

任何人都知道正确的方法从MKMapView的可见区域获得东南角和西北角(在地图坐标)?

Anyone know the proper way to get the south east corner and northwest corner (in map coordinates) from the visible area of a MKMapView?

编辑:似乎每当你问这里的答案,答案就在你身后。我使用%@而不是@f打印NSLog中的每个变量,在那里引发错误。我还发现了MKMapview的annotationVisibleRect属性。看起来虽然annotationVisibleRect是基于父视图坐标。

It seems whenever you asked something here the answer comes to you right after. I was using %@ instead of @f to print each variable in NSLog which was throwing errors there. I also discovered the annotationVisibleRect property of MKMapview. It seems though that the annotationVisibleRect is based on the parent view coordinates.

推荐答案

好,我正式回答了自己的问题,在任何地方找到它之前我将在这里发布答案:

Okay I officially answered my own question but since I didn't find it anywhere before I'll post the answer here:

//To calculate the search bounds...
//First we need to calculate the corners of the map so we get the points
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));

//Then transform those point into lat,lng values
CLLocationCoordinate2D neCoord;
neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];

CLLocationCoordinate2D swCoord;
swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];

这篇关于获取MKMapvIew的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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