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

查看:29
本文介绍了获取 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 都是空的.查看 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];

SWIFT 5

public extension MKMapView {

  var newBounds: MapBounds {
    let originPoint = CGPoint(x: bounds.origin.x + bounds.size.width, y: bounds.origin.y)
    let rightBottomPoint = CGPoint(x: bounds.origin.x, y: bounds.origin.y + bounds.size.height)

    let originCoordinates = convert(originPoint, toCoordinateFrom: self)
    let rightBottomCoordinates = convert(rightBottomPoint, toCoordinateFrom: self)

    return MapBounds(
      firstBound: CLLocation(latitude: originCoordinates.latitude, longitude: originCoordinates.longitude),
      secondBound: CLLocation(latitude: rightBottomCoordinates.latitude, longitude: rightBottomCoordinates.longitude)
    )
  }
}

public struct MapBounds {
  let firstBound: CLLocation
  let secondBound: CLLocation
}

使用

self.mapView.newBounds

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

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