jjzjj

ios - 显示可见 map View 边界内的 MKAnnotationViews

coder 2023-09-07 原文

我正在查询我的服务器,根据用户在 map View 中的可观察部分,查找距离用户最近的位置列表。

我不太确定如何执行此操作,但我是否应该发送 map View 可见区域的中心纬度和经度,然后根据最近的半径搜索结果?

我知道 MKMapView 有两个属性,分别是 regionvisibleMapRect。我应该使用其中之一吗?如果是,根据我的问题,哪一个更合适?

编辑 当您搜索附近的位置时,我希望实现与苹果 map 和 Yelp 应用程序相同的功能,它会根据 map View 的可见部分向您显示相关内容。

编辑 2 我见过很多人将他们的 mapview 的可见部分分成象限,最常见的是 NW、NE、SW 和 SE。但是,我仍然不完全确定他们为什么要这样做。我想知道查询后端的最佳方法,其中包含每个位置的纬度和经度,以查找 mapview 上存在的位置。

我在 stackoverflow 上到处查看,发现了一个类似的问题 here .但是,它没有回答我的问题,也没有提到 visibleMapRect 因为它已经超过 5 年了。

非常感谢任何帮助。

最佳答案

因此,您需要为服务器调用提供这 2 个坐标 bottomleft 和 topright 4 个点。

  1. 如果位置的纬度 > bottomleft.lat 并且如果位置的纬度 <>
  2. 如果 long of location > bottomleft.long 并且如果 long of location <>

iOS 代码看起来像这样使用这四个变量作为参数进行服务器调用

fun viewDidLoad() {
   super.viewDidLoad()
   self.mapView.delegate = self
}


func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
   // Using region
   var span: MKCoordinateSpan = mapView.region.span
   var center: CLLocationCoordinate2D = mapView.region.center

   // This is the farthest Lat point to the left
   var farthestLeft = center.latitude - span.latitudeDelta * 0.5
   // This is the farthest Lat point to the Right
   var farthestRight = center.latitude + span.latitudeDelta * 0.5
   // This is the farthest Long point in the Upward direction
   var farthestTop = center.longitude - span.longitudeDelta * 0.5
   // This is the farthest Long point in the Downward direction
   var farthestBottom = center.longitude + span.longitudeDelta * 0.5
   var SWCoord = MKCoordinateForMapPoint(farthestBottom, farthestLeft)
   var NECoord = MKCoordinateForMapPoint(farthestTop, farthestRight)


   // Using visibleMapRect
   var mapRect = mapView.visibleMapRect
   // This is the top right Coordinate
   var NECoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMaxX(mapRect), y: mapRect.origin.y)
   // This is the bottom left Coordinate
   var SWCoord = getCoordinateFromMapRectanglePoint(mapRect.origin.x, y: MKMapRectGetMaxY(mapRect))
   // Not needed but could be useful
   // var NWCoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMinX(mapRect), y: mapRect.origin.y)
   // var SECoord = getCoordinateFromMapRectanglePoint(MKMapRectGetMaxX(mapRect), y: MKMapRectGetMaxY(mapRect))


}

func getCoordinateFromMapRectanglePoint(x: Double, y: Double) -> CLLocationCoordinate2D {
   var mapPoint = MKMapPointMake(x, y)
   return MKCoordinateForMapPoint(mapPoint)
}

如果还有什么问题可以评论

关于ios - 显示可见 map View 边界内的 MKAnnotationViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35246441/

有关ios - 显示可见 map View 边界内的 MKAnnotationViews的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  4. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  5. ruby - 即时确定方法的可见性 - 2

    我正在编写一个方法,它将在一个类中定义一个实例方法;类似于attr_accessor:classFoocustom_method(:foo)end我通过将custom_method函数添加到Module模块并使用define_method定义方法来实现它,效果很好。但我无法弄清楚如何考虑类(class)的可见性属性。例如,在下面的类中classFoocustom_method(:foo)privatecustom_method(:bar)end第一个生成的方法(foo)必须是公共(public)的,第二个(bar)必须是私有(private)的。我怎么做?或者,如何找到调用我的cust

  6. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  7. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  8. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  9. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  10. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

随机推荐