2

CLLocationポイント(緯度/経度)があり、それが決定された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);
}
4

2 に答える 2

1

同様の質問がありました。正解です-MKMapViewを使用せずにMKCoordinateRegionにCLLocationCoordinate2Dが含まれているかどうかを確認するにはどうすればよいですか?

幸運を :)

于 2013-02-17T00:47:38.210 に答える
0

これは正しいようですが、逆です。北に移動すると緯度が高くなるため、地域の北西の角の緯度は中心よりも大きくなります。経度は正しいです。したがって、ここで計算されたコーナーは切り替えられます。northwestCornerは実際には南西のコーナーであり、southeastCornerは実際には北東のコーナーです。ただし、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");
}

したがって、答えは正しいですが、間違っています。しかし、それは機能するので、私はそれがより正しいと思います。

于 2014-01-31T17:04:53.707 に答える