ios-检测MKCoordinateRegion

问题描述

检测MKCoordinateRegion

如何不用 MKMapView 检测 MKCoordinateRegion 有没有包括 CLLocationCoordinate2D

我需要检测用户的位置是否属于**MKCoordinateRegion**,但是没有找到解决方法,比如**CGRectContainsCGPoint(rect, point)**。

找到了参考的代码片段:

CLLocationCoordinate2D topLeftCoordinate =
    CLLocationCoordinate2DMake(region.center.latitude
                               + (region.span.latitudeDelta/2.0),
                               region.center.longitude
                               - (region.span.longitudeDelta/2.0));

    CLLocationCoordinate2D bottomRightCoordinate =
    CLLocationCoordinate2DMake(region.center.latitude
                               - (region.span.latitudeDelta/2.0),
                               region.center.longitude
                               + (region.span.longitudeDelta/2.0));

        if (location.latitude < topLeftCoordinate.latitude || location.latitude > bottomRightCoordinate.latitude || location.longitude < bottomRightCoordinate.longitude || location.longitude > bottomRightCoordinate.longitude) {

    // Coordinate fits into the region

    }

但是不知道是不是精确计算了矩形区域,请高手帮忙解决。谢谢

解决方案

MKCoordinateRegion region = self.mapView.region;

CLLocationCoordinate2D location = user.gpsposition.coordinate;
CLLocationCoordinate2D center   = region.center;
CLLocationCoordinate2D northWestCorner, southEastCorner;

northWestCorner.latitude  = center.latitude  - (region.span.latitudeDelta  / 2.0);
northWestCorner.longitude = center.longitude - (region.span.longitudeDelta / 2.0);
southEastCorner.latitude  = center.latitude  + (region.span.latitudeDelta  / 2.0);
southEastCorner.longitude = center.longitude + (region.span.longitudeDelta / 2.0);

if (
    location.latitude  >= northWestCorner.latitude &&
    location.latitude  <= southEastCorner.latitude &&

    location.longitude >= northWestCorner.longitude &&
    location.longitude <= southEastCorner.longitude
    )
{
    // User location (location) in the region - OK :-)
    NSLog(@"Center (%f, %f) span (%f, %f) user: (%f, %f)| IN!", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta, location.latitude, location.longitude);

}else {

    // User location (location) out of the region - NOT ok :-(
    NSLog(@"Center (%f, %f) span (%f, %f) user: (%f, %f)| OUT!", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta, location.latitude, location.longitude);
}

我找到解决方法了。

时间: 2024-08-04 00:05:09

ios-检测MKCoordinateRegion的相关文章

IOS检测指定路径的文件是否存在

  本文给大家分享的是在IOS开发中检测指定文件是否存在的方法,给大家汇总了4种,十分实用,小伙伴们根据自己的需求自由选择吧. 代码如下: - (NSString *)dataPath:(NSString *)file { NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"badge"];

iOS 检测版本更新

如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bundle version中获取: [cpp] view plaincopy NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];       CFShow(infoDic);              NSString *appVersion = 

IOS检测指定路径的文件是否存在_IOS

复制代码 代码如下: - (NSString *)dataPath:(NSString *)file  {      NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"badge"];      BOOL bo = [[NSFileManager defaultManager] creat

【iOS7的一些总结】11、iOS中的事件

1.原理       在应用程序成功启动后,应用程序就开始由外部事件进行驱动.应用程序不断获取事件,并作出响应并更新用户界面,然后等待下一个事件.这对事件进行循环响应的整个机制,称为主要事件循环,由一个全局应用程序对象(一个UIApplication实例)进行管理.       在事件循环中最重要的事件之一就是用户与设备的交互操作.在iOS设备中,用户对设备的操作可以通过多种方式,如点击屏幕或者晃动设备等.iOS操作系统会解析这些操作,并将响应通知应用做出相应.对这些操作进行更加自然和直观的相应

iOS应用的crash日志的分析基础

Outline如何获得crash日志如何解析crash日志如何分析crash日志     1. iOS策略相关     2. 常见错误标识     3. 代码bug 一.如何获得crash日志 当一个iOS应用程序崩溃时,系统会创建一份crash日志保存在设备上.这份crash日志记录着应用程序崩溃时的信息,通常包含着每个执行线程的栈调用信息(低内存闪退日志例外),对于开发人员定位问题很有帮助. 如果设备就在身边,可以连接设备,打开Xcode - Window - Organizer,在左侧面板

assistivetouch-ios中如何检测AssistiveTouch是否打开

问题描述 ios中如何检测AssistiveTouch是否打开 ios中如何检测AssistiveTouch是否打开 ios中如何检测AssistiveTouch是否打开 ios中如何检测AssistiveTouch是否打开 ios中如何检测AssistiveTouch是否打开 解决方案 检测iOS WebApp是否运行在全屏模式iOS检测QQ是否安装iOS检测QQ是否安装 解决方案二: Apple hasn't provided any official APIs for that eithe

javascript检测移动设备横竖屏_javascript技巧

如何判断 移动设备提供了两个对象,一个属性,一个事件: (1)window.orientation   属于window对象上一个属性:共有三个值 :0为竖屏模式(portrait),90为向左反转变为横屏模式(landscape),-90为向右反转变为横屏模式(landscape),最后180就是设备上下互换还是竖屏模式. (2)orientationchange     是一个event,在设备旋转时,会触发此事件,如同PC上使用的resize事件. 这两个搭配起来使用,很容易就能获得设备的

微信小程序 wx.uploadFile无法上传解决办法_JavaScript

微信小程序 wx.uploadFile无法上传解决办法 微信安卓客户端无法使用wx.uploadFile上传文件的问题有不少开发者都遇到. 我也因为一直不能解决,硬着头皮提交审核最后被拒(抱着审核者最好用iOS检测的心态,不巧审核我应用的用的是安卓),才尝试使用第三方的手段解决. 最终我是用了七牛第三方存储的方式,将文件直接上传至七牛的储存的空间上再回调使用. 当然像又拍云,万象优图这些第三方存储源都可以采用这种思路. 首先是将七牛的https上传域名放进小程序的域名名单中. 这里我使用的是七牛

iOS下的实际网络连接状态检测

序言 网络连接状态检测对于我们的iOS app开发来说是一个非常通用的需求.为了更好的用户体验,我们会在无网络时展现本地或者缓存的内容,并对用户进行合适的提示.对绝大部分iOS开发者来说,从苹果示例代码改变而来的各种Reachablity框架是实现这个需求的普遍选择,比如这个库.但事实上,基于此方案的所有实现,都无法帮助我们检测真正的网络连接状态,它们能检测的只是本地连接状态:这种情况包括但不限于如下场景: 1.现在很流行的公用wifi,需要网页鉴权,鉴权之前无法上网,但本地连接已经建立: 2.