我有一个CLLocation点(lat / long),我想知道它是否在一个确定的MKCoordinateRegion中。我想知道以前在哪个区域是CLLocation点。
感谢。
我找到了解决方案。
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);
}
答案 0 :(得分:1)
有类似的问题。答案正确 - How to check if MKCoordinateRegion contains CLLocationCoordinate2D without using MKMapView?
祝你好运:)
答案 1 :(得分:0)
这似乎是正确但倒退。区域西北角的纬度将大于中心,因为当您向北移动时纬度会增加。经度是正确的。因此这里计算的角落是切换的,西北角的卡纳实际上是西南角,而东南角则是东北角。但代码有效,因为if语句也是向后的。它应该更像是
CLLocationCoordinate2D newMapCenter = self.mapView.centerCoordinate;
CLLocationCoordinate2D startingCenter = self.startingRegion.center;
CLLocationCoordinate2D northWestCorner, southEastCorner;
northWestCorner.latitude = startingCenter.latitude + (self.startingRegion.span.latitudeDelta / 2.0);
northWestCorner.longitude = startingCenter.longitude - (self.startingRegion.span.longitudeDelta / 2.0);
southEastCorner.latitude = startingCenter.latitude - (self.startingRegion.span.latitudeDelta / 2.0);
southEastCorner.longitude = startingCenter.longitude + (self.startingRegion.span.longitudeDelta / 2.0);
if (newMapCenter.latitude <= northWestCorner.latitude && newMapCenter.latitude >= southEastCorner.latitude && newMapCenter.longitude >= northWestCorner.longitude && newMapCenter.longitude <= southEastCorner.longitude) {
// still in original region
NSLog(@"same region");
}
else
{
// new region
NSLog(@"new region");
}
所以答案是正确的,但也是错误的。但它有效,所以我猜它更正确。